|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Iterate through page controlsHi 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 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. Avon wrote:
> For Each t As TextBox In Page.Controls Not every control on the page is a TextBox.> 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 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 .... but an "ASP.masterpages_maindesign_master" /isn't/ a TextBox, so > 'System.Web.UI.WebControls.TextBox'. 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. Thanks Phil,
I missed out the DirectCast statement in my example. ;-) Regards, Cerebrus.
[DATE - newbie] change and set back
RS232 - Recive = Send???? GetDirectories Performance Debug.Wrile does not appear on the Output Window GetCurrentProcess.Id vs GetCurrentProcessId API Trying to execute something stored in variable Can't delete an image that is in a picture box Impersonation help with SQL 2000 needed How to put a Word/Excel file into a database Job title: $300 flat for job in perl, cgi, Visual Basic/Asp.Net, whatever online project |
|||||||||||||||||||||||