How to clear all form fields from code-behind?

前端 未结 5 1406
日久生厌
日久生厌 2021-01-03 01:06

HTML has an input button type to reset all fields in a form to their initial state in one step: .

Is there a similar

5条回答
  •  迷失自我
    2021-01-03 02:03

    Using form.Controls.Clear() is not such a good approach because it will clear the entire form and you will even lose all the buttons on the form.

    Instead if you just want to clear all the form fields like text fields and radio buttons I would recommend you try the following: If you have a Reset button “Button1” then on click call a function reset();

    In the reset function:

       protected void resetButton_Click(object sender, EventArgs e)
       {
          TextBox1.Text=""; //set equal to empty string to all fields
       }
    

    Or redirect to same page by terminating the previous page

        protected void resetButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Test2.aspx", true);
        } 
    

提交回复
热议问题