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.