how to loop throught all label and button controls of myPage
I would like to loop throught all label and button controls of myPage and modifie their text property (in fact I want to translate the text depending on the language of the user).
I tried this :
VB Code:
For Each myControl As Control In Page.Controls
Dim test As String = myControl.ID
ListBox1.Items.Add(myControl.ID)
Next
but this does not give me the list of all controls.
I thought that I could loop through all controls, test for each if it is a control or button and then change the property .text to a new value depending on their old value.
Would to nice if anybody could give me a littel help...
Thank you in advance!
Fabian
Re: how to loop throught all label and button controls of myPage
You will need a recursive function.
Here is what you need.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Clear()
For Each ctl As Control In Me.Controls
GetControls(ctl)
Next
End Sub
Private Function GetControls(ByVal ctl As Control)
For Each ct As Control In ctl.Controls
If ct.GetType Is GetType(Label) Then
Dim lbl As Label
lbl = CType(ct, Label)
Me.ListBox1.Items.Add("ID:" & lbl.ID & " Text:" & lbl.Text)
lbl.Text = "New Text"
End If
If ct.Controls.Count > 0 Then
GetControls(ct)
End If
Next
End Function
Re: how to loop throught all label and button controls of myPage
juste perfect - exactly what I was looking for ... and even prepared for consumption. I had nothing to do.
Thank you very much!
Fabian