Hi,
I need to enumerate all controls in a ASP.NET Form and make them read-only. In old VB6 I do it by following code:
Dim t As Control
For Each t In Form1.Controls
If TypeOf t Is TextBox Then
t.Locked = True
End If
Next
But how to do it in ASP.NET?
Printable View
Hi,
I need to enumerate all controls in a ASP.NET Form and make them read-only. In old VB6 I do it by following code:
Dim t As Control
For Each t In Form1.Controls
If TypeOf t Is TextBox Then
t.Locked = True
End If
Next
But how to do it in ASP.NET?
The Page object available from within your web form code behind has a controls collection. I'm not sure if you can set the controls to read only but you can disable them. You'll need to cast to WebControl because enumerating controls returns type Control.
You can do the cast in a try catch block or us the as keyword:
int total = Page.Controls.Count;
for (int i = 0; i<total; i++)
{
WebControl c = Page.Controls[i] as WebControl;
if (c!=null)
{
// do your work here
}
}
or something like that
here's another way which I think is better:
foreach (WebControl ctl in Page.Controls)
{
TextBox tBox = (TextBox) ctl;
if (tBox != null)
{
\\ whatever you want to do
}
}