"Hai Nguyen" <ha******@neo.t amu.edu> wrote in message news:<#m******* *******@TK2MSFT NGP11.phx.gbl>. ..
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below
//foreach(WebCont rol 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.P age
{
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected System.Web.UI.W ebControls.Text Box TextBox2;
protected System.Web.UI.W ebControls.Text Box TextBox3;
protected System.Web.UI.H tmlControls.Htm lForm frmAccountingCo des;
private void Page_Load(objec t sender, System.EventArg s e)
{
foreach (Control c in frmAccountingCo des.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="frmAccounti ngCodes" method="post" runat="server">
Hope this helps!