Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to clear form but it's not working

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
When the user clicks the reset button I want to clear all form elements. It doesn't work...any thoughts?

Here's the C# code:
Code:
protected void btnReset_Click(object sender, EventArgs e)
        {
            txtSupplier.Text = "";
            txtPmtType.Text = "";
            txtFromDate.Text = "";
            txtToDate.Text = "";
            txtFromDlrAmt.Text = "";
            txtToDlrAmt.Text = "";

            ddlOpUnit.SelectedValue = "";

            if (rblPmtType.SelectedItem != null)
            {
                rblPmtType.SelectedItem.Selected = false;
            }
            if (rblInvMthd.SelectedItem != null)
            {
                rblInvMthd.SelectedItem.Selected = false;
            }
            if (rblPmtStatus.SelectedItem != null)
            {
                rblPmtStatus.SelectedItem.Selected = false;
            }
            if (rblInvPmtDate.SelectedItem != null)
            {
                rblInvPmtDate.SelectedItem.Selected = false;
            }
            if (rblDlrAmts.SelectedItem != null)
            {
                rblDlrAmts.SelectedItem.Selected = false;
            }
        }
 
Trace through and find out why. Maybe there is code in an event that is populating them after you clear them.
 
Unfortunately this code is deployed as a webpart in a Sharepoint environment...which means I can't easily debug it. Thanks though.
 
why not do this client side with javascript. form.reset(); or something like that.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jmeckley, you're right. So much easier that way. I didn't know that existed or I would've tried that a long time ago.

Thanks!
 
Actually, upon further review, this works fine unless the form has been submitted. After a submit and reload, the javascript doesn't clear anything. So back to the drawing board.
 
on the submit call response.redirect() and redirect back to itself.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
This works for me all the time try to fit it to your needs.

Public Class emptycontrols
Public Shared Sub EmptyTextBoxes(ByVal parent As Control, ByVal tb As TextBox)
For Each c As Control In parent.Controls 'LOOP THROUGHN ALL CONTROLS
If c.GetType() Is GetType(TextBox) Then 'IF ITS A TEXTBOX THEN EMPTY IT
CType(c, TextBox).Text = String.Empty
tb.Focus()
ElseIf c.GetType Is GetType(DropDownList) Then 'ELSE IF ITS A DROPDOWN LIST SET SELECTED VALUE TO -1
CType(c, DropDownList).SelectedIndex = 0
End If
If c.GetType Is GetType(ListBox) Then 'ELSE IF ITS A DROPDOWN LIST SET SELECTED VALUE TO -1
CType(c, ListBox).SelectedIndex = 0
End If
If c.GetType Is GetType(Label) Then
CType(c, Label).Visible = False

End If
If c.GetType Is GetType(CustomValidator) Then
CType(c, CustomValidator).Visible = False
End If
If c.GetType Is GetType(GridView) Then
CType(c, GridView).Visible = False
End If
If c.HasControls Then 'CALL ITSELF (GET ALL OTHER ELEMENTS IN OTHER CONTAINERS)
EmptyTextBoxes(c, tb)
End If
Next
End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top