Looping through all controls on a webform

  • Thread starter Thread starter Hai Nguyen
  • Start date Start date
H

Hai Nguyen

I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?
 
not sure this is the *best* way, but it works:

if(ctr.GetType().ToString() == "System.Windows.Forms.TextBox")
 
I assume by not working, you mean not all TextBoxes on the form are changed. basically, you are not checking grand-children and grand-grand-children and so forth. you need to recursively loop through the entire control hierarchy. frankly, it's too messy for my liking

----- Hai Nguyen wrote: ----

I have another question. I'm trying to loop through all the textboxes on
web application. The snippet is belo

//foreach(WebControl ctr in Page.Controls
foreach(Control ctr in Page.Controls

if(ctr is TextBox

TextBox t = (TextBox)ctr
t.BackColor = Color.AliceBlue
t.ReadOnly = false



Would you please tell me why it does not work
 
Scott C. Reynolds said:
not sure this is the *best* way, but it works:

if(ctr.GetType().ToString() == "System.Windows.Forms.TextBox")

That won't work any better than the code given (worse, as it's a webapp
- the controls *certainly* won't be System.Windows.Forms.TextBoxes) and
it'll be much slower too. It also won't pick up subclasses of TextBox.
 
And here it says right in the subject: "webform".

Just ignore me. I'm not that bright!
 
Hai Nguyen said:
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below

//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}

Would you please tell me why it does not work?



Because you need to reference the controls of the HTML Form. Here is
an example:

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.HtmlControls.HtmlForm frmAccountingCodes;

private void Page_Load(object sender, System.EventArgs e)
{
foreach (Control c in frmAccountingCodes.Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox t = (TextBox)c;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
}
}

And of course you must name your <form> tag the name used in the
codebehind. For this code you would have to use:

<form id="frmAccountingCodes" method="post" runat="server">

Hope this helps!
 
TC said:
Because you need to reference the controls of the HTML Form. Here is
an example:

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.HtmlControls.HtmlForm frmAccountingCodes;

private void Page_Load(object sender, System.EventArgs e)
{
foreach (Control c in frmAccountingCodes.Controls)
{
if (c.GetType() == typeof(TextBox))
{
TextBox t = (TextBox)c;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
}
}

And of course you must name your <form> tag the name used in the
codebehind. For this code you would have to use:

<form id="frmAccountingCodes" method="post" runat="server">

Hope this helps!


I forgot to clarify that it is not always the <form> control that you
will reference. As Daniel stated earlier, there is a control hierarcy
on every page (Parent -> Child -> Grandchild etc.). So you would want
to reference the parent container. For example, if your TextBoxes
were inside of an <asp:panel> control, then you would loop through the
ControlCollection of the panel.

Again, hope this helps!

-Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top