Hi all,
How to iterate through all or specifice controls on a Web Form using VB?
Thanks
Printable View
Hi all,
How to iterate through all or specifice controls on a Web Form using VB?
Thanks
You can iterate through the Page.Controls ControlCollection.
DJCode:For Each x AS Control In Page.Controls
'Do Stuff
Next
Thanks for your reply.
I did this. How to get the value of the found textbox control.
For i = 0 To MyForm.Controls.Count - 1
x = MyForm.Controls(i).GetType.ToString
Select Case x
Case "System.Web.UI.WebControls.TextBox"
'/ The following line will print the ID of the control.
Response.Write(MyForm.Controls(i).ID)
Case "System.Web.UI.WebControls.DropDownList"
Case "System.Web.UI.WebControls.Button"
Case "System.Web.UI.WebControls.RadioButton"
Case "System.Web.UI.WebControls.CheckBox"
Case "System.Web.UI.HtmlControls.HtmlInputCheckBox"
Case "System.Web.UI.HtmlControls.HtmlInputButton"
End Select
Next
This loop will only work if you have no container in your Page, if you have containers then you will need recursing function. I have posted an example here, that should be exactly what you are looking for.
http://www.vbforums.com/showthread.php?t=341665
Instead of "Label" change it to "TextBox" and the rest will be same..
Hope it helps.
Hi,
I posted my running code for iterating through the contols. It's working perfect. Except, I dont know how to get the value of the found textbox control.
Thanks
VB Code:
For i = 0 To MyForm.Controls.Count - 1 dim ct as Control ct = MyForm.Controls(i) If ct.GetType Is GetType(TextBox) Then dim txt As String txt = CType(ct, TextBox).Text End If Next
Great thanks to you Danial
For other visitors who may need this code, here is the full code to iterate through the web form controls and getting the value of the found control. Other types of controls are also listed for you to test and print.
VB Code:
Dim i As Integer Dim x As string Dim txt As String Dim ct As Control For i = 0 To MyForm.Controls.Count - 1 ct = MyForm.Controls(i) x = ct.GetType.ToString Select Case x Case "System.Web.UI.WebControls.TextBox" txt = CType(ct, TextBox).Text Response.Write("TextBox value is: " & txt) Case "System.Web.UI.WebControls.DropDownList" txt = CType(ct, DropDownList).SelectedItem.Text Response.Write("DropDownList value is: " & txt) Case "System.Web.UI.WebControls.ListBox" txt = CType(ct, ListBox).SelectedItem.Text Response.Write("ListBox value is: " & txt) Case "System.Web.UI.WebControls.Button" txt = CType(ct, Button).Text Response.Write("Button value is: " & txt) Case "System.Web.UI.WebControls.RadioButton" Case "System.Web.UI.WebControls.CheckBox" Case "System.Web.UI.HtmlControls.HtmlInputCheckBox" Case "System.Web.UI.HtmlControls.HtmlInputButton" End Select Next
MrAli
Hi all,
Is it possible that I save the above function in a CLASS.VB so any web form can call it for iteration??
Note: web forms have different names.