|
-
Sep 29th, 2004, 08:41 PM
#1
Thread Starter
Lively Member
Iterating through form controls
Why can't I see my textboxes when iterating through my WebForm??
This code is only picking up HtmlForm and a couple of Literalcontrols.
VB Code:
foreach (Control t in Page.Controls)
{
if (t is TextBox)
{
t.DataBind();
}
}
I've also tried using 'this.Controls' !!
You may follow, but not to close!
-
Sep 29th, 2004, 08:50 PM
#2
PowerPoster
You have to use recursion:
private void Page_Load .....
{
.....
BindTextboxes(Page);
}
private void BindTextboxes(Control aControl)
{
foreach (Control t in aControl.Controls)
{
if (t is TextBox)
{
t.DataBind();
}
else
BindTextboxes(t);
}
}
-
Sep 29th, 2004, 09:49 PM
#3
Thread Starter
Lively Member
Thanks but I'm not sure I understand how this is going to help uncover my textboxes by iterating through my page??
Also, this code resides within a dropdownlist__SelectedIndexChanged event. Does that make a difference?
You may follow, but not to close!
-
Sep 29th, 2004, 10:26 PM
#4
PowerPoster
I wrote it up without running it, so it may be a little off still, but the concept will allow you to find controls of textbox type.
What happens is those three literal controls that you seen it loop through have child controls in a collection that needs to be looked at as well. By doing it recursively, we can check all the child controls of each control and doing the binding when we find a textbox.
-
Sep 30th, 2004, 07:08 AM
#5
Thread Starter
Lively Member
Well, I've written this..
VB Code:
foreach (Control t in this.Controls)
{
if (t is TextBox)
{
t.DataBind();
}
else
{
if (t.HasControls())
{
foreach(Control ct in t.Controls)
{
if (ct is TextBox)
{
ct.DataBind();
}
}
}
}
}
..and it finds the child controls but they return as a LiteralControl with the names of my textboxes. So I have found my textboxes but why are they coming back as LiteralControls?? I can't test against the names b/c there's to many. What can I do to reveal the true textbox?
Thanks!!!
You may follow, but not to close!
-
Sep 30th, 2004, 10:39 AM
#6
PowerPoster
OK, here is code that works, adapt it to your cause:
Here is how I call the method to find a textbox control by name on the page:
Code:
private void Button1_Click(object sender, System.EventArgs e)
{
TextBox tx = FindTextBoxControl("TextBox1", this);
if(tx != null)
Response.Write(tx.Text);
else
Response.Write("NotFound");
}
And here is the method that finds the control.
Code:
private TextBox FindTextBoxControl(string textName, Control theControl)
{
foreach (Control t in theControl.Controls)
{
if (t is TextBox)
{
if(((TextBox)t).ID == textName)
return (TextBox)t;
}
else
{
if (t.HasControls())
return FindTextBoxControl(textName, t);
}
}
return null;
}
The reason they are nested are because a lot of container controls are literal controls. Literal controls contain child controls. You can actually nest container controls to be very deep. Like a table cell control can hold many textbox, label controls, etc...
You have to recurse the control tree to find what you are looking for.
-
Oct 1st, 2004, 01:00 PM
#7
Thread Starter
Lively Member
Thanks for the help, however, I don't need to find a particular textbox using a distinct name. I just want a way to iterate through all controls on the page, if it's a textbox then bind it. I have 30+ textboxes on my form, so calling a textbox by name is to cumbersome. I just need to identify the control as a textbox and then bind it.
You may follow, but not to close!
-
Oct 1st, 2004, 11:45 PM
#8
PowerPoster
Wow, I gave you all the code to do what you want, you just have to tweak it a bit. I don't think you even want to put the effort into understanding why I am doing it this way. Since you want it all written out for you, call this method when you want to bind all the textboxes passing in the page object to it:
Code:
private void BindTextBoxControls(Control theControl)
{
foreach (Control t in theControl.Controls)
{
if (t is TextBox)
{
t.DataBind();
}
else
{
if (t.HasControls())
BindTextBoxControls(t);
}
}
}
-
Oct 2nd, 2004, 08:29 AM
#9
Thread Starter
Lively Member
I do understand your point with "recursive" engineering!! Although I'm not using recursive programming I am able to iterate through all my controls now. Even though it's finding my textboxes it's not "seeing" them as textbox controls, therefore it skips the binding process.
Try it yourself and surprise me!!
You may follow, but not to close!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|