How to clear all form fields from code-behind?

前端 未结 5 1402
日久生厌
日久生厌 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 01:51

    using the manual approach of String.Empty for each and every Textbox or any other field will be cumbersome, also by using Response.Redirect(); it will be difficult to show any confirmation message or same. So, on reading so many blogs i have found a reliable approach so far:

        Public void reset(Control control)
        {
          foreach (Control x in control.Controls)
          {
            if (x is TextBox)
            {
                (x as TextBox).Text = String.Empty;
            }
            else if (x is DropDownList)
            {
                (x as DropDownList).SelectedIndex = 0;
            } 
            .
            .
            reset(x);
          }
        }
    
    
              
    

    use this code as reset(this); in your page wherever you want to reset or clear the values. At end of the if conditions do not forget to use the function recursively using the same

    `Control` object x.

提交回复
热议问题