Processing all controls on a page

B

Bogdan Zamfir

Hi,

I want to process all controls on a page, to check for a specific attribute
(a specific custom attribute I added).

I try with the following code
Dim oCtrl As WebControls.WebControl

For Each oCtrl In Page.Controls

If oCtrl.Attributes("bzDisable") = "1" Then

oCtrl.Enabled = false

End If

Next

bzDisable is a custom attribute I added to Textbox control in .ASPX page

and it gives me this error:
Exception Details: System.InvalidCastException: Specified cast is not valid.

Can anyone tell me what I did wrong?

Thank you.
Bogdan
 
J

John Saunders

Bogdan Zamfir said:
Hi,

I want to process all controls on a page, to check for a specific attribute
(a specific custom attribute I added).

I try with the following code
Dim oCtrl As WebControls.WebControl

For Each oCtrl In Page.Controls

If oCtrl.Attributes("bzDisable") = "1" Then

oCtrl.Enabled = false

End If

Next

Page.Controls is a collection of Control objects, not of WebControl objects.
 
K

Kevin Spencer

Okay, first of all, starting at Page level is going to return a bunch of
LiteralControls with the HTML for the page in them, and your WebForm, which
is NOT the Page, but the form in the Page. If you want to get all the
Controls on the Form, use:

Dim objControl As Control
For Each objControl in Page.FindControl("Form1").Controls

Then you need to ascertain that the Control is a WebControl. If the Form has
any HTML in it, it will also be converted to LiteralControls. However, if
you start with a type of Control, you can use Reflection if necessary, and
cast it to whatever type you need to look at.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 
J

James Zhuo

Hi

See below

Kevin Spencer said:
Okay, first of all, starting at Page level is going to return a bunch of
LiteralControls with the HTML for the page in them, and your WebForm, which
is NOT the Page, but the form in the Page. If you want to get all the
Controls on the Form, use:

Dim objControl As Control
For Each objControl in Page.FindControl("Form1").Controls

Then you need to ascertain that the Control is a WebControl. If the Form has
any HTML in it, it will also be converted to LiteralControls. However, if
you start with a type of Control, you can use Reflection if necessary, and
cast it to whatever type you need to look at.

1. What is Reflection and what is it used for?
2. What is LiteralControls and how does that differentiate from our normal
web controls?

Cheers

J
 
K

Kevin Spencer

Hi James,
1. What is Reflection and what is it used for?

The System.Reflection namespace is a set of classes that are used to
discover information about .Net classes and assemblies. You can find out
anything you want to about any class or assembly using these classes.
2. What is LiteralControls and how does that differentiate from our normal
web controls?

A LiteralControl is a Control which contains only Text. It can contain HTML
or any other type of plain text. When a Page is parsed, all HTML in the Page
that is not part of a Control is converted to LiteralControls.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top