Results 1 to 3 of 3

Thread: Resetting an ASP.NET webform

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    1

    Resetting an ASP.NET webform

    Karl Moore provides the following code to clear a windows form. I am having trouble adapting it to ASP.NET.

    Any help would be appreciated.

    Karl Moore's code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ResetForm(Me)
    End Sub

    Public Sub ResetForm(ByVal FormToReset As Form)
    ' Resets the main data entry controls on the passed FormToReset
    Dim objControl As Control
    ' Loop around every control on the form and run the reset method
    For Each objControl In FormToReset.Controls
    ResetControl(objControl)
    Next
    End Sub

    Public Sub ResetControl(ByVal ControlToReset As Control)
    ' Resets the core control, then loops and
    ' resets any sub controls, such as Tab pages
    Dim intCount As Integer
    ClearControl(ControlToReset)
    If ControlToReset.Controls.Count > 0 Then
    For intCount = 1 To ControlToReset.Controls.Count
    ResetControl(ControlToReset.Controls(intCount - 1))
    Next
    End If
    End Sub

    Public Sub ClearControl(ByVal ControlToClear As Control)
    ' Clears the value of a particular control -
    ' you may wish to extend this to suit your exact needs
    If InStr(ControlToClear.Tag, "skip", CompareMethod.Text) = 0 Then
    If TypeOf (ControlToClear) Is System.Windows.Forms.TextBox Then
    ControlToClear.Text = "" ' Clear TextBox
    ElseIf TypeOf (ControlToClear) Is System.Windows.Forms.CheckBox Then
    Dim objCheckBox As System.Windows.Forms.CheckBox = ControlToClear
    objCheckBox.Checked = False ' Uncheck CheckBox
    ElseIf TypeOf (ControlToClear) Is System.Windows.Forms.ComboBox Then
    Dim objComboBox As System.Windows.Forms.ComboBox = ControlToClear
    objComboBox.SelectedIndex = -1 ' Deselect any ComboBox entry
    End If
    End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Select sample item in combo box
    ComboBox1.SelectedIndex = 1
    End Sub

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Resetting an ASP.NET webform

    I don't have the IDE open, but you should probably be using Page.Controls() (?) instead of Formname

  3. #3
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Resetting an ASP.NET webform

    I just use the simple javascript reset.

    In the page_Load:
    me.btnReset.attributes.add("onClick", "document.forms[0].reset()")

    Works for me.

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