C# 提交后清除所有字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2355674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear all fields after submit
提问by Sheery
I have many text box in an asp.net application, and after submitting their values i want to clear all fields when it loads again?
我在 asp.net 应用程序中有很多文本框,在提交它们的值后,我想在再次加载时清除所有字段?
采纳答案by Asad
you need to write and call similar function
after submit
你需要编写,并呼吁类似的function
后submit
public static void EmptyTextBoxes(Control parent)
{
foreach (Control c in parent.Controls) {
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Text = string.Empty;
}
}
}
reference
参考
回答by sashaeve
You may use JavaScript form reset() method or loop throw all textboxes and set Text property to empty string.
您可以使用 JavaScript 表单 reset() 方法或循环抛出所有文本框并将 Text 属性设置为空字符串。
foreach(var control in this.Controls){
TextBox tb = control as TextBox;
if (tb != null)
{
tb.Text = string.Empty;
}
}
回答by Nick Craver
You could do this in the page:
您可以在页面中执行此操作:
foreach (var tb in Controls.OfType<TextBox>())
{
tb.Text = null;
}
If you need to clear a whole hierarchy of them, call this once from the page: ClearTextBoxes(this);
如果您需要清除它们的整个层次结构,请从页面调用一次: ClearTextBoxes(this);
And here's the function:
这是功能:
private void ClearTextBoxes(Control c) {
foreach (var tb in c.Controls.OfType<TextBox>())
{
tb.Text = null;
}
foreach (var control in c.Controls) {
ClearTextBoxes(control); //Recurse down to children
}
}
回答by Jens
public static Control[] FlattenHierachy(Control root)
{
List<Control> list = new List<Control>();
list.Add(root);
if (root.HasControls())
{
foreach (Control control in root.Controls)
{
list.AddRange(FlattenHierachy(control));
}
}
return list.ToArray();
}
private void ClearTextBoxes()
{
Control[] allControls = FlattenHierachy(Page);
foreach (Control control in allControls)
{
TextBox textBox = control as TextBox;
if (textBox != null)
{
textBox.Text = "";
}
}
}
This code will collect all textboxes in a list and set their textproperty to "".
此代码将收集列表中的所有文本框并将其文本属性设置为“”。
回答by marcelo-ferraz
Well You can as well set the field to not use viewstate, which will imply in a boost in performance, small , but a boost nonetheless.
As well, you could do a response.redirect to the same page, or a server.transfer.
If those two solutions doesn't suit you, you might use something of a textbox.text = string.empty, and dropdowlist.clearselection. They might be not as fast as you might want they are a lot more elegant.
好吧,您也可以将该字段设置为不使用 viewstate,这意味着性能的提升,小,但仍然是提升。同样,您可以对同一页面执行 response.redirect,或执行 server.transfer。
如果这两个解决方案不适合您,您可以使用 textbox.text = string.empty 和 dropdowlist.clearselection 之类的东西。它们可能没有您希望的那么快,但要优雅得多。
回答by CJM
COMPLETELY UNTESTED SOLUTION:
完全未经测试的解决方案:
Can't you use ME.ViewState.Clear() in the Init or LoadViewState event handlers?
您不能在 Init 或 LoadViewState 事件处理程序中使用 ME.ViewState.Clear() 吗?
Or it might be Page.Viewstate.Clear() or even Page.ClearChildViewState()...
或者它可能是 Page.Viewstate.Clear() 甚至 Page.ClearChildViewState() ...
Sorry - haven't tried it in anger....
对不起 - 没有在愤怒中尝试过......
回答by Chandan Pasunoori
And this for clearing all controls in form like textbox, checkbox, radioButton
这用于清除表单中的所有控件,如文本框、复选框、单选按钮
you can add different types you want..
你可以添加你想要的不同类型..
private void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
if (c.HasChildren)
{
ClearTextBoxes(c);
}
if (c is CheckBox)
{
((CheckBox)c).Checked = false;
}
if (c is RadioButton)
{
((RadioButton)c).Checked = false;
}
}
}
回答by Shrenik
Create a function called cleardata()
:
创建一个名为 的函数cleardata()
:
void cleardata()
{
textbox1.Clear();
textbox2.Clear();
textbox3.Clear();
textbox4.Clear();
}
And call this function whenever you need it.
并在需要时调用此函数。
回答by fdsafdsafdsafdsafs
This is an old question, but I just wanted to add the following, in case the controls are in a group box, you can do it like this, then :
这是一个老问题,但我只想添加以下内容,如果控件位于组框中,您可以这样做,然后:
foreach (Control c in this.form.Controls)
{
//Tests each control to see if it is a GroupBox
if (c is GroupBox)
{
clearRadioButtons(c.Controls);
clearListBox(c.Controls);
resetDateTime(c.Controls);
clearTextBoxes(c.Controls);
clearComboBoxes(c.Controls);
}
}
public static void clearTextBoxes(Control.ControlCollection controls)
{
//Loops through all controls on form
foreach (Control c in controls)
{
//Tests each control to see if it is a textbox
if (c is TextBox)
{
//Converts to useable format and clears textboxes
var text = (TextBox)c;
text.Clear();
}
}
}
回答by Mohammed Azhar
you can call clear method
你可以调用clear方法
Class-name.clear();
void clear()
{
textbox1.text="";
textbox2.text="";
}