[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop through text boxes on page but textboxes have varying ID's

Posted on 2005-05-03
17
Medium Priority
?
388 Views
Last Modified: 2010-04-16
I have an array of strings called temp [6,7].

Each subscript will hold the text for a text box from the page.

the text boxes are named like this:

ChestPressSet1Rep   ChestPressSet1Wt
ChestPressSet2Rep   ChestPressSet2Wt
ChestPressSet3Rep   ChestPressSet3Wt

These 6 textboxes are the first row for the array.

This pattern is repeated 6 times for : ShoulderPress
                                                      TBarRow
                                                      BicepsCurl
                                                      TricepsCurl
                                                      Squat

NOTE: SetxRep (where x is 1, 2 or 3) and SetyWt(where y is 1,2 or 3) is consistent.

I want to extract the text from the text boxes in an efficient way.
I have tried various looping methodology to go through all the text boxes and add the text values to the specific temp location.

I can just simply do :temp[x,y]=TextBoxName.Text and it works but i just want my code to look a little professional.
I realise a lot of substringing and string checking is going to be involved in this.

heres my code for the loop:
/************************************************//
//   CHEST INFORMATION IS BEING PUT INTO ARRAY    //
//***********************************************//
      int a=1;
      int b=2;
      TextBox tB=null;
      temp[0,0]=Label4.Text;
      foreach(Control cc in Page.Controls)
            {
            if (cc.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                  {
                  tB=(TextBox)cc;
                  if(((tB.ID).ToString()).EndsWith("Reps"+a.ToString())&& a<=5)
                        {
                        temp[0,a]=tB.Text.Trim();
                        a=a+2;
                        }
                              
            if((tB.ID).EndsWith("Wt"+b.ToString())&&b<=6)
            {
            temp[0,b]=tB.Text.Trim();
                      b=b+2;
            }
      }
      
                  //************************************************//
                  //   SHOULDER INFORMATION IS BEING PUT INTO ARRAY //
                  //***********************************************//
                  temp[1,0]=Label11.Text;
                  TextBox tB2=null;
                  int x=1;
                  int y=2;
                  foreach(Control cc in Page.Controls)
                  {
                        if (cc.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                        {
                              tB2=(TextBox)cc;
                              if((tB2.ID).EndsWith("Reps"+x.ToString())&& x<=5)
                              {
                                    temp[0,x]=tB2.Text.Trim();
                                    x=x+2;
                              }
                              
                              if((tB.ID).EndsWith("Wt"+y.ToString())&& y<=6)
                              {
                                    temp[0,y]=tB.Text.Trim();
                                    y=y+2;
                              }
                        }
                  }
0
Comment
Question by:deersuper
  • 8
  • 4
  • 3
  • +1
17 Comments
 
LVL 9

Expert Comment

by:BurntSky
ID: 13919636
I agree there has to be a better way, but to answer well, I'll need a little more information.  How is the aspx page set up?  Are you creating the TextBox controls dynamically?  Or are they static?  Are the TextBox controls in tables?

Also, I don't mean to be nitpicky, but there are a couple things I don't like about the code you posted (other than the fact that it needs to be completely rewritten):

1. cc.GetType().ToString() == "System.Web.UI.WebControls.TextBox" is NOT a good idea.  Avoid hardcoded strings AT ALL COSTS.  What happens if in some (albeit unlikely) framework update the TextBox class gets moved from that specific namespace?  Your code breaks.  The better way to do this comparison is if(cc.GetType() == typeof(TextBox)) { ... }

2. Also, you can cut down some of your explicit casts (which generally aren't a good idea) by doing this:

foreach(Control cc in Page.Controls)
{
    TextBox tb = cc as TextBox;  // notice the implicit cast
    if(tb != null)
    {
        ...
    }
}

But anyway, those are just a few design issues to keep in mind, irrelevant to the topic at hand.
0
 
LVL 2

Author Comment

by:deersuper
ID: 13919666
Text boxes are all static and are not in tables
and I like your first suggestion. makes a lot of sense.

I dont understand what you mean by page set up but if you mean how many controls I have and all that sort of thing
then I have the Textboxes i mentioned, one button control to insert all the information via a method call to another class
and some label controls. Apart from that nothing.

0
 
LVL 2

Author Comment

by:deersuper
ID: 13919712
int i=0;
foreach(Control c in Page.Controls)
{
if(c.GetType()==(typeof(TextBox)))
      {
            i++;
      }
}
PrintArray(i);

}

void PrintArray(int i)
{
Response.Write(i);
}

The above code , as per your suggestion, is checking the control type. now logically speaking i should show me the number
of text boxes but when I print it using response I get 1.
which shows that the text boxes are not being recognised.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13919749
as an extension to burntsky ...

the control relationship needs to be recursive(as usercontrols etc are controls within controls)  ...

private int CountControls(ControlCollection controls) {
    int i=0;
    foreach(Control c in controls)
     {
      if(c.GetType()==(typeof(TextBox)))
         {
            i++;
          }
      if(c.Controls.Count > 0) {
          i += CountControls(c.Controls);
      }
     }
     return i;
}

Response.Write(CountControls(Page.Controls));

excuse the poorly formatted code (just typing in here)
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13919793
Yeah, sorry, I should have mentioned it needs to be recursive.  The Page.Controls collection doesn't contain every control at it's first level.  It contains collections of collections of collections (and so on) of controls.
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13919825
When I was asking about how the page is set up, I meant how are the controls on the ASPX page layed out?  Are they all in rows?  Or are they spread out and surrounded by a series of questions/text/etc?  Have you thought about throwing everything into a DataGrid instead of using the TextBoxes?
0
 
LVL 2

Author Comment

by:deersuper
ID: 13919978
datagrid for the while is not an option.
basically I am working on the domain (logic layer) and have left the presentatin layer for later
so the text boxes are only by them selves all arranged in 6 rows with each row having 6 boxes
0
 
LVL 9

Accepted Solution

by:
BurntSky earned 1000 total points
ID: 13920228
Okay, so your array (which, by the way is 6x6 so a[5,5]) looks like the following, correct?

a[0,0-5]: ChestPressSet1Rep, ChestPressSet2Rep, ChestPressSet3Rep, ChestPressSet1Wt, ChestPressSet2Wt, ChestPressSet3Wt
a[1,0-5]: ShoulderPressSet1Rep, ShoulderPressSet2Rep, ShoulderPressSet3Rep, ShoulderPressSet1Wt, ShoulderPressSet2Wt, ShoulderPressSet3Wt
... and so on.

Assuming you're flexible on the page layout, I'd suggest the following (this will also offer more flexibility):

ASPX page:
...
<asp:Table id="Table1" runat="server">
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="TextBox1" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox2" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox3" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox4" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox5" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox6" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox7" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox8" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox9" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox10" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox11" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox12" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox13" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox14" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox15" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox16" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox17" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox18" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox19" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox20" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox21" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox22" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox23" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox24" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox25" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox26" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox27" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox28" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox29" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox30" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
      <asp:TableRow>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox31" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox32" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox33" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox34" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox35" Runat="server" />
            </asp:TableCell>
            <asp:TableCell>
                  <asp:TextBox ID="Textbox36" Runat="server" />
            </asp:TableCell>
      </asp:TableRow>
</asp:Table>
...


Codebehind:

private void Page_Load(object sender, System.EventArgs e)
{
      string[,] s = new string[this.Table1.Rows[0].Cells.Count, this.Table1.Rows.Count];
      for(int i=0; i<this.Table1.Rows.Count; i++)
      {
            for(int j=0; j<this.Table1.Rows[i].Cells.Count; j++)
            {
                  s[i,j] = ((TextBox)this.Table1.Rows[i].Cells[j].Controls[0]).ID;
                  Response.Write(s[i,j] + " ");
            }
            Response.Write("<br />");
      }
}
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13920239
Except change that .ID to .Text (I used .ID to test it cause I didnt feel like typing in all the textboxes.)
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13923904
If you have TextBox with the ID's in some pattern like below one
TextBox1
TextBox2
..
..
TextBox25

then Y not to use the find Control method

for (int i = 1; i < 26; i++)
{
  System.Web.UI.WebControls.TextBox ctrl = this.FindControl ("TextBox" +i.ToString()) as System.Web.UI.WebControls.TextBox;
  if (ctrl != null)
    {
            // Do your processing in here
    }
}

Cheers
Jatinder
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13923915
Because FindControl is a recursive method that searches every Control within that instance of the Page class.  So every time you call the method it creates a ton of overhead.  Just because it's fewer lines of code doesn't always mean it's more efficient =)
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13923961
You are right.
Surely it will be slow but how much i think not more then second in any case.
you can try it running on your PC and check the timing difference.
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13923981
Yes, that's true.  On any reasonably equipped machine you'll never notice the speed difference, but little things like this are what add up to big issues when you're developing very large applications.  A second here, a second there and before you know it the application just crawls along.  It's always a good idea to choose the most reasonably efficient method.
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13923988
That's what i am trying to say when you are writing a large application they run on good hardware.
So you have to choose out in terms of miliseconds and a good readable code.
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13924033
<flame>

First of all, I don't know what kind of tiny applications you write, but applications I am required to develop are so extensive that if I even let a millisecond or two slide in every method, the application would take 10 minutes to load up.

I don't think you understand what I was getting at.  Sure, his application may be small enough that he won't care about a few milliseconds, or even more, but professional software development is all about mantaining good design standards.  It's good to do things the RIGHT way, even if it means a few more lines of code.

And you made a horrible comparison.  Choosing between milliseconds and readable code?  Are you implying my code is not readable?  What, are you new?  You can't read a simple nested for loop?

I'm sorry, but I just don't see your logic.

</flame>
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13924145
in agreement with BurnySky ...


Using a single hand written recursive method which returns ALL controls is far more efficient than recursing for each individually


as for code readability ... return it as a controlcollection ...

controlcollection textboxes = GetTextBoxes()
//loop through the collection

is no worse than a loop with find control.
0
 
LVL 2

Author Comment

by:deersuper
ID: 13938035
Thanks Burnt Sky .... this works good !

I see my question started a little discussion ..... hehe !

Thanks for the extensively written code.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question