473,513 Members | 3,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Textbox Loop with C#

52 New Member
Ok, so what I got is 10 textboxes on a .Net page. What I need to do is loop through them in C# and change the ids. The code I have on the .cs page behind the .aspx page is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void Page_Load(object sender, EventArgs e)
  3.     {
  4.         LoopTextboxes(Page.Controls);
  5.     }
  6.     private void LoopTextboxes(ControlCollection controlCollection)
  7.     {
  8.         foreach (Control control in controlCollection)
  9.         {
  10.             if (control is TextBox)
  11.             {
  12.                 ((TextBox)control).Text = "Text Here";
  13.             }
  14.             if (control.Controls != null)
  15.             {
  16.                 LoopTextboxes(control.Controls);
  17.             }
  18.  
  19.         }
  20.     }
Problem is the code can only change certain properties of the textbox, right? Color, Value, etc. Also, the "foreach" conditional statement doesn't loop. Can someone provide a little help so I can loop through and change the ids?

Thanks
Mar 17 '10 #1
6 8850
Frinavale
9,735 Recognized Expert Moderator Expert
I do not understand your question.
Why would you want to change the IDs of TextBoxes?
If you change the ID of the TextBox then how are you supposed to use it in code later...and know what TextBox you're really accessing?

Please explain why you are trying to do this so that we can help you find an approach that will work for you.


-Frinny
Mar 17 '10 #2
movieking81
52 New Member
The renaming thing was just a idea. What I have are rows/columns of textboxes (30) in a HTML table and each one needs to have it's own identifer so it can updated by the user to a MSSQL database. An example is below. I guess I could do it the long way but that's a lot of typing. I'd rather create a loop and use a variable to create the ID. Is there a better way to do it?

Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td>
  4. <asp:Textbox id="tb1" runat="server" />
  5. </td>
  6. <td>
  7. <asp:Textbox id="tb2" runat="server" />
  8. </td>
  9. <td>
  10. <asp:Textbox id="tb3" runat="server" />
  11. </td>
  12. <td>
  13. <asp:Textbox id="tb4" runat="server" />
  14. </td>
  15. <td>
  16. <asp:Textbox id="tb5" runat="server" />
  17. </td>
  18. <td>
  19. <asp:Textbox id="tb6" runat="server" />
  20. </td>
  21. <td>
  22. <asp:Textbox id="tb7" runat="server" />
  23. </td>
  24. <td>
  25. <asp:Textbox id="tb8" runat="server" />
  26. </td>
  27. <td>
  28. <asp:Textbox id="tb9" runat="server" />
  29. </td>
  30. <td>
  31. <asp:Textbox id="tb10" runat="server" />
  32. </td>
  33. <td>
  34. <asp:Textbox id="tb11" runat="server" />
  35. </td>
  36. </tr>
  37. </table>
  38.  
Thanks for the help
Mar 17 '10 #3
semomaniz
210 Recognized Expert New Member
why dont you create the textbox dynamically when you load the page and add that to a panel control. Then use a for each loop to access the controls inside the panel control to access the textboxes itself.
Mar 18 '10 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Yes there are a lot of better ways to do this.

You could use a pre-built control like the GridView control to dynamically create your table and fill it with dynamically created TextBoxes...or you could use a Repeater control to generate your table and TextBoxes.

The important thing about both of these classes is that they both implement the INamingContainer Interface. This interface creates a new ID namespace within a Page which means that all of the dynamic controls within the class that implements this interface will be given a unique ID within the control when rendered on the page. This unique ID is combined with the ID of the dynamic control.

It's pretty cool stuff but you don't have to go down to that level if you don't want to.

I think, going by your programming style, you'd be most interested in using the Repeater control.

Or, like semomaniz was suggesting, you could dynamically create these controls if you want to. If you're interested in doing this, please read over this short article on how to use dynamic controls in ASP.NET before you begin or you may end up pulling your hair out in frustration with strange problems that are not apparent at first :)

-Frinny
Mar 18 '10 #5
movieking81
52 New Member
The Repeater Control works great, thanks. What I need to do now is include several dropdown list in the repeater, which I've managed to do just fine. However, outside of the repeater I have them filled using XML data, but now since they are in the repeater the names/ids are auto generated. How would I go about filling in the dropdown list with XML data within the repeater? Would this involve the INamingContainer Interface you mentioned above?

Thanks again for the help.
Mar 21 '10 #6
Frinavale
9,735 Recognized Expert Moderator Expert
I don't know how I missed your post Movieking81.
Sorry for the delayed reply.


You should be able to use the Find(controlID) method to get a reference to the DropDownList that you want to work with.

-Frinny
Apr 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8320
by: Phil Stanton | last post by:
I am trying to create a text box in Word the same height as one on a report and then fill it with text. I can "read" the position of the text box from the report OK and create a text box in Word with the correct position, but it's size is identical to the size of the "ungrown" text box on the report. I need to create the text box in Word the...
16
38145
by: Ramsin Savra | last post by:
Hi, What do you suggest to do if I want to do a search in TextBox ? I know that using RichTextBox, we could have Find method to search for a specific word or information but I was wonder if somebody knows some methods for Find and Replace in TextBox. Thanks
2
8755
by: utterberg | last post by:
Can anyone help me with this problem? I dynamically creates several textboxes using a placeholder. This works fine. But is there a way for me to loop through theese textboxes and retrive its value when clicking a button? The code I'm working with: aspx-page <form id="Form1" method="post" runat="server"> <asp:PlaceHolder...
0
3514
by: Paul Rees | last post by:
I am wondering if anyone has encountered a similar problem to the one that I am having. I have an ASP.NET page which has a table and a dropdownlist that was added at design time. The majority of the rows in the table are then generated at run-time based on a SQL query from a Jet database, and similarly the dropdownlist is filled at run-time ...
2
2552
by: Alex Shirley | last post by:
HI I'm trying to iterate through all the textboxes on a webpage and trim them for spaces. i.e. If a user enters " hello world " we correct it to "hello world" So far I've come up with this:
4
8750
by: bwalke | last post by:
I am developing a web form which is going to be used for manufacturing input. The form uses a DataList which list a batch of parts that may range anywhere from 1-9 parts. The DataList contains 6 textboxes pre-fill with data (reason for using datalist) which will capture user input and changes to the textboxes. So the 6 textboxes in columns...
2
6215
by: hamil | last post by:
I have a form with a text box, a thread start button, and a seperate thread. This is a TCP listener (server) example I took out of a book. See the code below. In the loop below, look at the two lines I have marked with a comment of ??????? If I don't write to the text box, I have a fast response. The value "returnedData" is written to the...
2
12562
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's first column is a textbox(TemplateColumn), the other 3 columns are just display(BoundColumn). (1) if the value of the textbox is 0, then i'd like to...
7
9760
by: david | last post by:
I try to use "for" loop to assign textbox control ID to a textbox variable in server side codebehind for a web form. But I met some problem. What I want to do is solving the following-like code by a loop: static code: txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1") txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2") ........
16
5122
by: mj.redfox.mj | last post by:
Can anyone help? I have a textbox which I'm programatically adding by using the following code: txtTest = New TextBox txtTest.ID = "txtLeft" + cntCount.ToString Page.FindControl("tdInput").Controls.Add(txtTest) This successfully creates a textbox called "txtLeft1" in the table
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.