I\'m using VB 2010 Express and have a label named \"lblTitle\" in my form. The next code doesn\'t work and I know it , but how can I do something like this?
Dim
Seems like the best way to do this is to make a string representing the name of the control, then cast it as a control. This saves you having to make a variable, as well. Your method might appear to work, but I'm not sure it wouldn't fail once you run it. Also, it would throw an error in the IDE if you were trying to access a property more specific to the type of control, and not something common like Text. See my closely related question:
accessing multiple form controls using a variable for the name
After a deep google search , the answer seems to be easy as that:
Dim bla = "lblName"
Me.Controls(bla).Text = "Dan"
while lblName
is the label's name in the form.
Thanks guys
DirectCast(Page.FindControl("lblTitle"), Label).Text = "some new value"
Try this:
Dim myLabel As Label = DirectCast(Page.FindControl("lblTitle"), Label)
myLabel.Text = "some new value"
You can do this assuming you are using windows forms:
DirectCast(Me.Controls.Find("lblTitle", True)(0), Label).Text = "Hello world"