Refer to an object dynamically

Mondeo

Centurion
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have several literal controls on my page, Literal1, Literal2, Literal3 etc

I have a function which takes an index

Private Sub UpdateLiteral(byval index as int16)
...
End Sub

In that sub I want to update the corresponding literal to the index that comes in, I could do:

Select Case index
Case 1
Literal1.Text = "new value"
Case 2
Literal2.Text = "new value"
Case 3
Literal3.Text = "new value"
End Select

But theres a lot more than 3 literals, how can I dynamically generate the object name that I want to manipulate?

Thanks
 
You could use the FindControl method of the page to locate the correct control

Don't have VS handy but the code would be something like
Visual Basic:
dim l as Literal
l = FindControl("Literal" & index.ToString())
l.Text = "New Value"
 
Back
Top