How to clear all form fields from code-behind?

前端 未结 5 1428
日久生厌
日久生厌 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:56

    Refer this link for more information

    http://www.freshcodehub.com/Article/3/clear-all-fields-like-textbox-dropdownlist-checkbox-radiobutton-label-after-form-submission-in-aspnet-c

    public void ClearControls(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if ((c.GetType() == typeof(TextBox)))  //Clear TextBox
            {
                ((TextBox)(c)).Text = "";
            }
            if ((c.GetType() == typeof(DropDownList)))  //Clear DropDownList
            {
                ((DropDownList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(CheckBox)))  //Clear CheckBox
            {
                ((CheckBox)(c)).Checked = false;
            }
            if ((c.GetType() == typeof(CheckBoxList)))  //Clear CheckBoxList
            {
                ((CheckBoxList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(RadioButton)))  //Clear RadioButton
            {
                ((RadioButton)(c)).Checked = false;
            }
            if ((c.GetType() == typeof(RadioButtonList)))  //Clear RadioButtonList
            {
                ((RadioButtonList)(c)).ClearSelection();
            }
            if ((c.GetType() == typeof(HiddenField)))  //Clear HiddenField
            {
                ((HiddenField)(c)).Value = "";
            }
            if ((c.GetType() == typeof(Label)))  //Clear Label
            {
                ((Label)(c)).Text = "";
            }
            if (c.HasControls())
            {
                ClearControls(c);
            }
        }
    }
    

提交回复
热议问题