|
-
March 4th, 2002, 08:55 AM
#1
Enumerate all controls in ASP.NET Form
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?
-
March 8th, 2002, 10:49 PM
#2
Re: Enumerate all controls in ASP.NET Form
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
-
May 24th, 2002, 01:17 AM
#3
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
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|