Click to See Complete Forum and Search --> : Enumerate all controls in ASP.NET Form
Invoker
March 4th, 2002, 07:55 AM
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?
mikescham
March 8th, 2002, 09:49 PM
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
mgla
May 24th, 2002, 01:17 AM
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
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.