清除页面的所有内容 [英] Clearing all the feilds of a page
本文介绍了清除页面的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我想清除页面上文本框中的所有值.我使用了以下代码.它既不起作用,也不给我任何错误.
clear(Page);
Hi all!!
I want to clear all the values from the textbox on my page. I used the following code. neither it''s working nor it''s giving me any error.
clear(Page);
private void clear( Control c)
{
foreach ( Control ctrl in c.Controls )
{
if (ctrl is TextBox)
{
TextBox tb = ctrl as TextBox;
tb.Text = "";
}
else
if (ctrl is DropDownList)
{
DropDownList ddl = ctrl as DropDownList;
ddl.SelectedIndex=0;
}
else
if (ctrl is CheckBox)
{
CheckBox chk = ctrl as CheckBox;
chk.Checked = false;
}
}
}
推荐答案
尝试以下代码:
Try this code:
private void Clear(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
Clear(c);
}
else
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;
}
}
}
}
并这样称呼:
and call it like this:
Clear(this);
希望对您有所帮助:)
hope it helps :)
这篇关于清除页面的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文