473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Processing all controls on a page

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
Nov 17 '05 #1
4 1944
"Bogdan Zamfir" <ams_soft at pcnet dot ro> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...
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.

--
John Saunders
Internet Engineer
jo***********@surfcontrol.com
Nov 17 '05 #2
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.

"Bogdan Zamfir" <ams_soft at pcnet dot ro> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #3
Hi

See below

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:ee***************@TK2MSFTNGP09.phx.gbl...
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

--
HTH,

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

"Bogdan Zamfir" <ams_soft at pcnet dot ro> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #4
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.

"James Zhuo" <na**************@optusnet.com.au> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl... Hi

See below

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:ee***************@TK2MSFTNGP09.phx.gbl...
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

--
HTH,

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

"Bogdan Zamfir" <ams_soft at pcnet dot ro> wrote in message
news:ur**************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bogdan Zamfir | last post by:
Hi, I want to enable / disable several controls on a page based on a condition in application. But there are several other controls on the page don't want to enable / disable together with...
11
by: Glen Wolinsky | last post by:
This is my first attempt as asynchronous processing. I have created a small test app as proof of concept, but I am having one proglem. In the example (code listed below), my callback routine has...
2
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired...
5
by: Joe Reazor | last post by:
I've got an asp.net page that has approximately 1,440 controls on it. You must think I'm crazy, but we need to have the page organized in this fashion. As you can imagine, the page takes a very...
1
by: Jim in Arizona | last post by:
I'm having dificulty figuring out how to process multiple check boxes on a web form. Let's say I have three check boxes: cbox1 cbox2 cbox3 The only way I can think of to code the...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: tony | last post by:
....can anyone direct me towards a form processor. i am switching from basic ASP to .NET and am a little confused on how the form data is processed. I plan on the data being sent via email, and...
1
by: SyracuseCheer | last post by:
I have a VS.NET 2003 project that's giving me issues. Any assistance would be greatly appreciated! I have a fairly straight-forward system of server controls and web services, where the server...
6
by: DC | last post by:
Hi, our cms (asp.net 2.0) dynamically inserts controls into asp.net pages, sometimes 50 and more per page, and if we run into a performance bottleneck we have a hard time finding out which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.