473,405 Members | 2,279 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,405 software developers and data experts.

Iterate through page controls

Hi friends,
I need of some help,
I have this code:
For Each t As TextBox In Page.Controls ' here is the error
t.Text = "test"
Next

But I am getting this error:
Unable to cast object of type 'ASP.masterpages_maindesign_master' to type
'System.Web.UI.WebControls.TextBox'.
Please help me to understand this error and solve the problem
Thanks in advance


Apr 23 '06 #1
3 2903
Hi,

Many ways to resolve this, if you understand why the error occurs.
Basically the Page can have a lot of objects and when iterating through
them, the "masterpages_maindesign_master" object is also encountered.
Since it is not a TextBox, the exception results when your code tries
to convert it to a TextBox. So, basically you need to check if it is a
TextBox, before trying to convert it to a TextBox or query it's Text
property.

Instead, you could use :

For Each o As Object In Page.Controls
If TypeOf (o) Is TextBox Then
t.Text = "test"
End if
Next

HTH,

Regards,

Cerebrus.

Apr 24 '06 #2
Avon wrote:
For Each t As TextBox In Page.Controls
t.Text = "test"
Next

But I am getting this error:
Unable to cast object of type 'ASP.masterpages_maindesign_master' to type
'System.Web.UI.WebControls.TextBox'.
Please help me to understand this error and solve the problem
Not every control on the page is a TextBox.

Your code says
For Each t As TextBox In Page.Controls
which tells VB to go and get each control in turn and put it into a
variable (called "t") that can hold a TextBox ...
Unable to cast object of type 'ASP.masterpages_maindesign_master' to type
'System.Web.UI.WebControls.TextBox'.


.... but an "ASP.masterpages_maindesign_master" /isn't/ a TextBox, so
can't be held in a TextBox variable, so the program fails.

Before casting, make sure it's valid to do so, as in

For Each c As Control In Page.Controls
If TypeOf c Is TextBox Then
With DirectCast( c, TextBox )
.Text = "test"
End With
End If
Next

HTH,
Phill W.
Apr 24 '06 #3
Thanks Phil,

I missed out the DirectCast statement in my example. ;-)

Regards,

Cerebrus.

Apr 24 '06 #4

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

Similar topics

2
by: JDavies | last post by:
Hi All I am dynamically Creating DropDownList boxes for a Survey form. These DropdownLists are added to Table Cells programatically. I tried to loop thru the controls on the form but it doesnt...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
1
by: Dan Sikorsky | last post by:
How do you recursively iterate thru each collection on each form on a web page and find each web control? -- Thank you kindly, Dan Sikorsky BAB, BScE, MSC
2
by: Tina | last post by:
I need to find all of the datagrids on a page. How can I iterate through all of the controls on a page to get their name and grab them as an object? thanks, T
2
by: Oleg | last post by:
Let's say I rendered a bunch of controls with the same name and different IDs. On postback, can I somehow iterate through these controls (checkboxes in this case, but could be dropdown controls in...
7
by: Rich | last post by:
Hello, I have a form with 5 textboxes named txt0, txt1, txt2, txt3, tx4. In VB6 I could iterate through these with For i = 0 to 4 debug.print Me.controls("txt" & i).Name Next
1
by: jd | last post by:
Question from an ASP.Net newbie: using ASP.Net 2.0, how can I iterate over all the images in a page? The reason: I need to tweak the URLs for each image (I need to insert an "order id" field so...
0
by: Rob Meade | last post by:
Hi all, I have a page, where, because it had to all be in one page there are a multitude of panels, with the use of a handful of conditions and the ability to show/hide panels I am able to make...
2
by: Luqman | last post by:
I have a Ajax Tab Container on my Webform with One Panel and some Labels and Textboxes. I want to Iterate through all the textboxes and return their IDs. I tried every possibility I could but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.