How do you loop through a web forms control collection?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BJ

    How do you loop through a web forms control collection?

    I am trying to loop through the control collection of my ASP.NET web
    form in order to access the contents of each field.
    It seems as if there is a parent control (Literal?) in ASP.NET.
    Attached are my failed attemps as well as the
    hardcoded lines that do work.

    TIA


    Private Sub cmdReset_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles cmdReset.Click

    'Dim ctrl As Control
    'Dim txtBox As TextBox
    'Dim crtls As ControlCollecti on = Me.CreateContro lCollection

    'For Each ctrl In Me.Controls
    ' If ctrl.GetType Is GetType(WebCont rols.TextBox) Then
    ' DirectCast(ctrl , TextBox).Text = ""
    ' ElseIf ctrl.GetType Is GetType(WebCont rols.CheckBox) Then
    ' ElseIf ctrl.GetType Is GetType(WebCont rols.DropDownLi st)
    Then
    ' Else
    ' 'do nothing
    ' End If
    'Next

    'Dim myEnumerator As IEnumerator = Me.Controls.Get Enumerator()
    'While (myEnumerator.M oveNext())

    ' Dim myObject As Object = myEnumerator.Cu rrent
    ' If (myObject.GetTy pe().Equals(Get Type(TextBox))) Then
    ' DirectCast(myOb ject, TextBox).Text = ""
    ' End If

    'End While

    'For Each ctrl In Me.Controls
    ' If ctrl.GetType.To String =
    GetType(WebCont rols.TextBox).T oString Then
    ' DirectCast(ctrl , TextBox).Text = ""
    ' End If
    'Next

    'For Each ctrl In Me.CreateContro lCollection()
    ' If ctrl.GetType.To String =
    GetType(WebCont rols.TextBox).T oString Then
    ' DirectCast(ctrl , TextBox).Text = ""
    ' End If
    'Next

    'For Each ctrl In crtls
    ' If ctrl.GetType.To String =
    GetType(WebCont rols.TextBox).T oString Then
    ' DirectCast(ctrl , TextBox).Text = ""
    ' End If
    'Next

    Me.txtDateNeede d.Text = ""
    Me.txtName.Text = ""
    Me.txtPhone.Tex t = ""
    Me.txtProductNa me.Text = ""
    Me.txtPurchaseQ ty.Text = ""
    Me.txtRecieved. Text = ""

    Me.ckbAuthList. Checked = False
    Me.ckbOneTimePu rchase.Checked = False
    Me.ckbStocked.C hecked = False

    End Sub

  • Cor Ligthert [MVP]

    #2
    Re: How do you loop through a web forms control collection?

    BJ,

    Looping through the controls on a webform need a simple trick

    Have a look how we do it.



    I hope this helps,

    Cor

    "BJ" <julie6232000@y ahoo.com> schreef in bericht
    news:1127878760 .792279.298780@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    >I am trying to loop through the control collection of my ASP.NET web
    > form in order to access the contents of each field.
    > It seems as if there is a parent control (Literal?) in ASP.NET.
    > Attached are my failed attemps as well as the
    > hardcoded lines that do work.
    >
    > TIA
    >
    >
    > Private Sub cmdReset_Click( ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles cmdReset.Click
    >
    > 'Dim ctrl As Control
    > 'Dim txtBox As TextBox
    > 'Dim crtls As ControlCollecti on = Me.CreateContro lCollection
    >
    > 'For Each ctrl In Me.Controls
    > ' If ctrl.GetType Is GetType(WebCont rols.TextBox) Then
    > ' DirectCast(ctrl , TextBox).Text = ""
    > ' ElseIf ctrl.GetType Is GetType(WebCont rols.CheckBox) Then
    > ' ElseIf ctrl.GetType Is GetType(WebCont rols.DropDownLi st)
    > Then
    > ' Else
    > ' 'do nothing
    > ' End If
    > 'Next
    >
    > 'Dim myEnumerator As IEnumerator = Me.Controls.Get Enumerator()
    > 'While (myEnumerator.M oveNext())
    >
    > ' Dim myObject As Object = myEnumerator.Cu rrent
    > ' If (myObject.GetTy pe().Equals(Get Type(TextBox))) Then
    > ' DirectCast(myOb ject, TextBox).Text = ""
    > ' End If
    >
    > 'End While
    >
    > 'For Each ctrl In Me.Controls
    > ' If ctrl.GetType.To String =
    > GetType(WebCont rols.TextBox).T oString Then
    > ' DirectCast(ctrl , TextBox).Text = ""
    > ' End If
    > 'Next
    >
    > 'For Each ctrl In Me.CreateContro lCollection()
    > ' If ctrl.GetType.To String =
    > GetType(WebCont rols.TextBox).T oString Then
    > ' DirectCast(ctrl , TextBox).Text = ""
    > ' End If
    > 'Next
    >
    > 'For Each ctrl In crtls
    > ' If ctrl.GetType.To String =
    > GetType(WebCont rols.TextBox).T oString Then
    > ' DirectCast(ctrl , TextBox).Text = ""
    > ' End If
    > 'Next
    >
    > Me.txtDateNeede d.Text = ""
    > Me.txtName.Text = ""
    > Me.txtPhone.Tex t = ""
    > Me.txtProductNa me.Text = ""
    > Me.txtPurchaseQ ty.Text = ""
    > Me.txtRecieved. Text = ""
    >
    > Me.ckbAuthList. Checked = False
    > Me.ckbOneTimePu rchase.Checked = False
    > Me.ckbStocked.C hecked = False
    >
    > End Sub
    >[/color]


    Comment

    • BJ

      #3
      Re: How do you loop through a web forms control collection?

      Thank you very much!!!!!

      Comment

      • BJ

        #4
        Re: How do you loop through a web forms control collection?

        The answer is below. The first example shows how to reset all
        textboxes, checkboxes, or dropdownlist on container form1. If you have
        a panel that is a sub container with in form1, all textboxes,
        checkboxes, and dropdownlist within this control is not modified. I
        can now isolate a section of my master page and reset all data entry
        fields usnig a generic reset function passing the containers id.

        Thank you again Cor.

        Private Sub cmdReset_Click( ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles cmdReset.Click
        Dim frm As Control = Me.FindControl( "Form1") 'Form1 is the HTML
        form Id
        Dim pnl As Control = Me.FindControl( "panel1")

        For Each ctrl As Control In frm.Controls
        If TypeOf ctrl Is TextBox Then
        DirectCast(ctrl , TextBox).Text = ""
        ElseIf TypeOf ctrl Is CheckBox Then
        DirectCast(ctrl , CheckBox).Check ed = False
        ElseIf TypeOf ctrl Is DropDownList Then
        DirectCast(ctrl , DropDownList).S electedIndex = 0
        End If
        Next

        For Each ctrl As Control In pnl.Controls
        If TypeOf ctrl Is TextBox Then
        DirectCast(ctrl , TextBox).Text = ""
        ElseIf TypeOf ctrl Is CheckBox Then
        DirectCast(ctrl , CheckBox).Check ed = False
        ElseIf TypeOf ctrl Is DropDownList Then
        DirectCast(ctrl , DropDownList).S electedIndex = 0
        End If
        Next

        Comment

        Working...