Get all TextBoxes on the Page
-
Hi I would like to access contents controls on a web page using variables instead of calling them directly and actually don't know how to do it. To make it clear: I have a some TextBox controls with ID 1 to 10 and want to access them in a loop and read their contents Thanks
-
Hi I would like to access contents controls on a web page using variables instead of calling them directly and actually don't know how to do it. To make it clear: I have a some TextBox controls with ID 1 to 10 and want to access them in a loop and read their contents Thanks
Recurse through a parent control's collection for text box controls:
private void ChangeAllTextBoxes(Control parentControl)
{
foreach(Control ctl in parentControl)
{
if(ctl is TextBox)
{
...
}
else if(ctl.HasControls)
{
ChangeAllTextBoxes(ctl);
}
}
} -
Recurse through a parent control's collection for text box controls:
private void ChangeAllTextBoxes(Control parentControl)
{
foreach(Control ctl in parentControl)
{
if(ctl is TextBox)
{
...
}
else if(ctl.HasControls)
{
ChangeAllTextBoxes(ctl);
}
}
}This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }
-
This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }
Ok, so you have to account for my typos.. I didn't have a VS IDE open to write it. It should say parentControl.Controls. Use a bit of common sense and at least try a variaion if my code didn't directly compile. As far as your problem goes... be specific. Your posts are too vague to tell what you're asking for.
-
Ok, so you have to account for my typos.. I didn't have a VS IDE open to write it. It should say parentControl.Controls. Use a bit of common sense and at least try a variaion if my code didn't directly compile. As far as your problem goes... be specific. Your posts are too vague to tell what you're asking for.
Thanks, with parentControl.Controls it works. Sorry, there was a little bit late here and my brain did function very slowly ;-) Now I will try to describe my problem: I was wondering if there is possible to define a string which has a control name as it contents eg. fName = "TextBox1" and then read or change the content of this control, e.g Textbox1.Text using fName variable. I hope now I expressed myself clearer :) Marek
-
This would be fine if not a compiler error "foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible" Nevertheless my problem is little bit different. I want to generate an sql statement using private string MakeQuery() { string fName, fContents; for (int i = 1; i<= 5; i++) { fName = " ,Desc" + i.ToString(); //fContents = ?????; } }
OK. You can do what you want I believe. Starting at the top level of the Page, you need to find the HtmlForm control as thats the top level control to which all controls on a page belong, and then iterate it's child controls being careful to make sure that you test each control you find for child controls and checking them iteratively for children in turn to ensure you pick up all controls and all their iterative children. When you find the bottom level child controls one at a time you then need to check if the control you find is a textbox and do something with it. The below is a top level method looking at all controls on the Page to which it belongs, and processing the HtmlForm control (when found) to iterate it's child controls;
foreach(Control c in this.Controls) { if (c is HtmlForm) { HtmlForm iHtmlForm = (HtmlForm)c; foreach(Control iHtmlFormChild in iHtmlForm.Controls) { if (iHtmlFormChild.HasControls()) { IterateControls(iHtmlFormChild); } else if (iHtmlFormChild is TextBox) { TextBox iTextBox = (TextBox)iHtmlFormChild; //Do something with the instance of the TextBox here } } } }
You'll notice an IterateControls method here, which checks each child of the HtmlForm for Child controls. This is below;private void IterateControls(Control control) { foreach(Control iChildControl in control.Controls) { if (iChildControl.HasControls()) { IterateControls(iChildControl); } else if (control is TextBox) { TextBox iTextBox = (TextBox)control; //Do something with the instance of the TextBox here } } }
And I know this works. Test it by creating a page with a number of TextBox controls on it. Put the first method in the Page_Load event and use the second method as a Private method of the page. Change '//Do something with the instance of the TextBox here' to 'TextBoxID.Text += iTextBox.ID.ToString();' and the Text in the TextBox to which the ID of all other controls is being written should contain all their ID's when the page renders. I'm sure you can take it from there... Rhys A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation... Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die. -
Thanks, with parentControl.Controls it works. Sorry, there was a little bit late here and my brain did function very slowly ;-) Now I will try to describe my problem: I was wondering if there is possible to define a string which has a control name as it contents eg. fName = "TextBox1" and then read or change the content of this control, e.g Textbox1.Text using fName variable. I hope now I expressed myself clearer :) Marek
You can set the ID of a control as follows: myControl.ID ="myCtlID"; then use the .FindControl("myCtlID") method in the parent page/control. It'll return a control that you can cast to a text box and do what you need.