Enable/disable all controls on a web page

D

disaia

I saw the C# solution:

for each Control c in Page.Controls
if c is WebControl --> ((WebControl)c).Enabled = False / true /
variable

But could figure out the VB syntax.

The closest I got was this:

Dim ctl As Control

For Each ctl In Page.Controls
if typeof(ctl) is TextBox then ((TextBox)c).Enabled =
False
Next

But I get a syntax error. Anybody know how to correct this?

If there is a more elegant solution, I'd surely like to know.

Thanks,
 
K

Karl Seguin [MVP]

For Each c As Control In Page.Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Enabled = True
End If
Next

My guess is that your casting was the syntax error, use ctype in vb.net

Karl
 
D

disaia

I'm getting closer!

The syntax error description was just that "Syntax error".

"CType" did correct the error. Thanks Karl!

I implemented your code as you've written it. Except set enabled to
false.

My function now looks like this:

Private Sub DisableAllFields()

For Each c As Control In Page.Controls
If TypeOf (c) Is TextBox Then
CType(c, TextBox).Enabled = False
End If
Next

End Sub

No errors and compiles fine, but there are 20 controls on my page and
it only loops through 3 times.

Here are the values of 'c' as it loops through:

1. [System.Web.UI.ResourceBasedLiteralControl]
2. [System.Web.UI.HtmlControls.HtmlForm]
3. [System.Web.UI.LiteralControl]


Something tells me I'm not looping through the correct collection.
Either that or I am running this in my Page_Load sub and the controls
have not been instantiated yet. Any ideas?

Thanks,
 
D

disaia

I got it! Finally.

Page.Controls is a collection of three collections. I am interested in
the second collection, [System.Web.UI.HtmlControls.HtmlForm], which I
can reference like this Page.Controls(1).

Here's the final working version of my function:

Private Sub DisableAllFields()

For Each c As Control In Page.Controls(1).Controls

If TypeOf (c) Is TextBox Then
CType(c, TextBox).Enabled = False
End If

Next

End Sub





I'm getting closer!

The syntax error description was just that "Syntax error".

"CType" did correct the error. Thanks Karl!

I implemented your code as you've written it. Except set enabled to
false.

My function now looks like this:

Private Sub DisableAllFields()

For Each c As Control In Page.Controls
If TypeOf (c) Is TextBox Then
CType(c, TextBox).Enabled = False
End If
Next

End Sub

No errors and compiles fine, but there are 20 controls on my page and
it only loops through 3 times.

Here are the values of 'c' as it loops through:

1. [System.Web.UI.ResourceBasedLiteralControl]
2. [System.Web.UI.HtmlControls.HtmlForm]
3. [System.Web.UI.LiteralControl]


Something tells me I'm not looping through the correct collection.
Either that or I am running this in my Page_Load sub and the controls
have not been instantiated yet. Any ideas?

Thanks,
For Each c As Control In Page.Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Enabled = True
End If
Next

My guess is that your casting was the syntax error, use ctype in vb.net

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top