清除asp.net表单中的所有字段

kbv*_*hnu 3 c# asp.net

I have an asp.net form. In this formI want to clear all the data what I entered in textbox,dropdownlist etc. So how can I do without going to each textbox and set the value.

like TextBox1.Text=""; etc. How can i clear all values of a form ?

nav*_*een 21

使用此功能

private void ClearInputs(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        else if (ctrl is DropDownList)
            ((DropDownList)ctrl).ClearSelection();

        ClearInputs(ctrl.Controls);
    }
}
Run Code Online (Sandbox Code Playgroud)

并称之为

ClearInputs(Page.Controls);
Run Code Online (Sandbox Code Playgroud)

要么

重定向到同一页面

Response.Redirect(Request.Url.PathAndQuery, true);
Run Code Online (Sandbox Code Playgroud)


Ste*_*e B 6

您可以在客户端使用重置按钮:

<input type="reset" />
Run Code Online (Sandbox Code Playgroud)

另一种方法是重定向到当前url:

Page.Response.Redirect(Page.Request.Url.RawUrl)
Run Code Online (Sandbox Code Playgroud)

该技术将用户重定向到当前页面,就好像他刚刚到达.但是,只有当您的页面没有构建一些回发时才会出现这种情况(因为视图状态将被重置).

至少,您可以"遍历"整个控制树并清除您选择的控件的值,例如基于控件的类型.