Results 1 to 8 of 8

Thread: Web Form Controls Iteration

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    26

    Web Form Controls Iteration

    Hi all,

    How to iterate through all or specifice controls on a Web Form using VB?

    Thanks

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Web Form Controls Iteration

    You can iterate through the Page.Controls ControlCollection.

    Code:
    For Each x AS Control In Page.Controls
    'Do Stuff
    Next
    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    26

    Re: Web Form Controls Iteration

    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

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Web Form Controls Iteration

    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.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    26

    Re: Web Form Controls Iteration

    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

  6. #6
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Web Form Controls Iteration

    VB Code:
    1. For i = 0 To MyForm.Controls.Count - 1
    2.     dim ct as Control
    3.     ct = MyForm.Controls(i)
    4.  
    5.     If ct.GetType Is GetType(TextBox) Then
    6.                 dim txt As String
    7.                 txt = CType(ct, TextBox).Text
    8.  
    9.             End If
    10. Next
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    26

    Re: Web Form Controls Iteration

    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:
    1. Dim i As Integer
    2.         Dim x As string
    3.         Dim txt As String
    4.         Dim ct As Control
    5.         For i = 0 To MyForm.Controls.Count - 1
    6.             ct = MyForm.Controls(i)
    7.             x = ct.GetType.ToString
    8.             Select Case x
    9.                 Case "System.Web.UI.WebControls.TextBox"
    10.                       txt = CType(ct, TextBox).Text
    11.                       Response.Write("TextBox value is: " & txt)
    12.                 Case "System.Web.UI.WebControls.DropDownList"
    13.                       txt = CType(ct, DropDownList).SelectedItem.Text
    14.                       Response.Write("DropDownList value is: " & txt)
    15.                 Case "System.Web.UI.WebControls.ListBox"
    16.                       txt = CType(ct, ListBox).SelectedItem.Text
    17.                       Response.Write("ListBox value is: " & txt)
    18.                 Case "System.Web.UI.WebControls.Button"
    19.                       txt = CType(ct, Button).Text
    20.                       Response.Write("Button value is: " & txt)
    21.                 Case "System.Web.UI.WebControls.RadioButton"
    22.                 Case "System.Web.UI.WebControls.CheckBox"
    23.                 Case "System.Web.UI.HtmlControls.HtmlInputCheckBox"
    24.                 Case "System.Web.UI.HtmlControls.HtmlInputButton"
    25.             End Select
    26.         Next


    MrAli

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    26

    Re: Web Form Controls Iteration

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width