Pages

Men

rh

3/23/2013

How to clear the data in all controls using a function in VB.Net


Public Sub ClearControls(ByVal webCtrl As System.Web.UI.Control)

        Dim Ctl As System.Web.UI.Control



        For Each Ctl In webCtrl.Controls


            If TypeOf Ctl Is System.Web.UI.WebControls.TextBox Then

                CType(Ctl, System.Web.UI.WebControls.TextBox).Text = vbNullString


            ElseIf TypeOf Ctl Is System.Web.UI.WebControls.DropDownList Then


                CType(Ctl, System.Web.UI.WebControls.DropDownList).SelectedIndex = 0


            ElseIf TypeOf Ctl Is System.Web.UI.WebControls.CheckBox Then


                CType(Ctl, System.Web.UI.WebControls.CheckBox).Checked = False


            End If



            If Ctl.Controls.Count > 0 Then

                ClearControls(Ctl)    'This is what makes it recursive
            End If
        Next
    End Sub

No comments :

Post a Comment