如何从代码隐藏中清除所有表单字段?

How to clear all form fields from code-behind?(如何从代码隐藏中清除所有表单字段?)
本文介绍了如何从代码隐藏中清除所有表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML 有一种输入按钮类型,可以一步将表单中的所有字段重置为其初始状态:<input type="reset" .../>.

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

是否有类似的简单方法可以从代码隐藏中重置 aspx 页面的所有表单字段?还是需要用TextBox1.Text=string.EmptyTextBox2.Text=string.Empty等一一重置所有控件?

Is there a similar simple way to reset all form fields of an aspx page from code-behind? Or is it necessary to reset all controls one by one with TextBox1.Text=string.Empty, TextBox2.Text=string.Empty, etc. ?

提前致谢!

更新:

Context 是一个简单的 Contact/Send us a message" 页面,页面上有 8 个 asp:TextBoxes(用户在其中输入姓名、地址、电话、电子邮件、消息等).然后他点击提交,代码隐藏中的 Onclick 消息处理程序向某个管理员发送一封电子邮件,用户填写的所有表单字段都应该被清空,他会在标签中收到通知(消息已发送 blabla...").我希望清除表单字段以避免用户再次单击提交并再次发送相同的消息.

Context is a simple Contact/"Send us a message" page with 8 asp:TextBoxes on the page (where the user enters the name, address, phone, email, message, etc.). Then he clicks on submit, the Onclick message handler in code-behind sends an email to some administrator, and all the form fields the user filled in should be emptied and he gets a notification in a label ("Message sent blabla..."). I want to have the form fields cleared to avoid that the user clicks again on submit and the same message is sent a second time.

推荐答案

您只需为每种类型的控件编写一个分支,除非其中一个控件有一些特殊的事情需要执行以重置它.

You need only write a fork for each type of control unless one of the control has something special that needs to be done to reset it.

foreach( var control in this.Controls )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;
    ...
}

附加您询问了如何清除隐藏的控件.为此,您应该像这样创建一个递归例程:

ADDITION You asked how to clear controls even ones that are buried. To do that, you should create a recursive routine like so:

private void ClearControl( Control control )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;
    ...

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}

因此,您可以通过传递页面来调用它:

So, you would call this by passing the page:

ClearControls( this );

这篇关于如何从代码隐藏中清除所有表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)