Posted: 5/20/2010
Hi friends,
I need to find all the textboxes in a page in c#. I need to disable one by one, how can I do it ?
shawn said: Hi friends, I need to find all the textboxes in a page in c#. I need to disable one by one, how can I do it ?
Hi Shawn,
Try following code:
public static void DisableTextBoxes(Control parent) { foreach (Control c in parent.Controls) { if (c.GetType() == typeof(TextBox)) { ((TextBox)(c)).Enabled = false; } if (c.HasControls()) { DisableTextBoxes(c); } } }
To call the above method:
protected void Page_Load(object sender, EventArgs e) { DisableTextBoxes(Page); }
Posted: 5/21/2010
In 2.0
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { DisableAllTextBoxes(Page); } private static void DisableAllTextBoxes(Control parent) { foreach (Control c in parent.Controls) { var tb = c as TextBox; if (tb != null) { tb.Enabled = false; } DisableAllTextBoxes(c); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="txtTest" runat="server"></asp:TextBox> <br /> <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label> <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox> <asp:DropDownList runat="server"> </asp:DropDownList> </form> </body> </html>
In 3.5
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { DisableAllTextBoxes(Page); } private static void DisableAllTextBoxes(Control parent) { foreach (Control c in parent.Controls) { if (c is TextBox) { var tb = c as TextBox; tb.Enabled = false; } DisableAllTextBoxes(c); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="txtTest" runat="server"></asp:TextBox> <br /> <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label> <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox> <asp:DropDownList runat="server"> </asp:DropDownList> </form> </body> </html>
One more way in 3.5
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { DisableAllTextBoxes(Page); } private static void DisableAllTextBoxes(Control parent) { if (parent != null) foreach (Control c in parent.Controls) { if (c is TextBox) { using (var tb = c as TextBox) { tb.Enabled = false; } } DisableAllTextBoxes(c); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="txtTest" runat="server"></asp:TextBox> <br /> <asp:Label ID="lblTest" runat="server" Text="Test"></asp:Label> <asp:TextBox ID="txtTest2" runat="server"></asp:TextBox> <asp:DropDownList runat="server"> </asp:DropDownList> </form> </body> </html>