如何清除code-后面的所有表单字段? [英] How to clear all form fields from code-behind?
问题描述
HTML有一个输入按钮类型,以表单中的所有字段重置为初始状态在一步:<输入类型=复位... />
。
HTML has an input button type to reset all fields in a form to their initial state in one step: <input type="reset" ... />
.
有没有类似简单的方法来从code-背后重置aspx页面的所有表单域?或者是否有必要用 TextBox1.Text =的String.Empty
, TextBox2.Text =的String.Empty $ C之一重置所有控制由一个$ C>等?
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. ?
在此先感谢!
更新:
上下文是一个简单的联系人/给我们留言页面8 ASP:文本框的页面(即用户输入的姓名,地址,电话,电子邮件,短信等)上。然后,他点击提交的onclick消息处理程序在code-后面将电子邮件发送到某些管理员,和所有的表单字段填写用户应该被清空,他得到一个通知,一个标签(消息发送布拉布拉。 ..)。我想有清除,以避免用户点击再次提交和相同的邮件发送第二次的表单字段。
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 );
这篇关于如何清除code-后面的所有表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!