concatenate two strings to make a textbox variable?

I'm drawing a blank on this one.

I have more than 100 textboxes.  They each have id's such as "textbox1, textbox2, textbox3, textbox4," etc.

I want to loop through results of a SQL query, dynamically assigning the values for each of these textboxes:

for (int j=0;j<121;j++)
     strTxtBoxTmp = "txtBox" + (j+1);
    (strTxtBoxTmp).Text = astrMySymbols[j].ToString();

This won't work.  Is there a way to do this without manually writing out all the 100+ lines of textbox values?  Seems like I could just easily do this with a FOR loop.

Who is Participating?
Babycorn-StarfishConnect With a Mentor Commented:
Hi there,

this is a bit like carl_tawn's solution but for .Net 1.1

for(int i = 1; i < 4; i++)
      TextBox t = (TextBox)this.FindControl("textBox" + i);
         //do something with textbox t

hope this helps


Send Full Function

and might be declarion of your Textbox is problem

Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
With C# 2.0 you can use something like:

            for (int j = 0; j <= 121; ++j)
                TextBox t = (TextBox)this.Controls.Find("txtBox" + (j + 1).ToString(), true)[0];
                t.Text = "Value: " + astrMySumbols[j].ToString();          
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

cacklebunnyAuthor Commented:
I'm not exactly sure what you need, but here's a further example:

On my ASPX page, I have something like the following:

<asp:textbox maxlength="4" width="60" height="30" runat="server" style="font-size:20px;font-weight:bold" id="txtBox1" />

<asp:textbox maxlength="4" width="60" height="30" runat="server" style="font-size:20px;font-weight:bold" id="txtBox2" />

Now just imagine the above repeated more than a hundred times.

In my script, I have what is already noted above.

cacklebunnyAuthor Commented:
Hi Carl:

That doesn't seem to work. I get the following error:

 'System.Web.UI.ControlCollection' does not contain a definition for 'Find'

I only used three textboxes so please change the loop values.

cacklebunnyAuthor Commented:
Bingo!  Thanks, Babycorn.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.