"Hai Nguyen" <ha******@neo.tamu.edu> wrote in message news:<#m**************@TK2MSFTNGP11.phx.gbl>...
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!