一个一个控件的清空非常麻烦,所以写了一个方法将一类的控件清空:
protected void btnClear_Click(object sender, EventArgs e)
{
foreach (Control ctl in this.Controls)
{
this.txtClear(ctl);
}
}
private void txtClear(Control ctls)
{
if(ctls.HasControls())
{
foreach (Control ctl in ctls.Controls)
{
txtClear(ctl);
}
}
else
{
if (ctls.GetType().Name == "TextBox")
{
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctls.ID);
tb.Text = "";
}
else if (ctls.GetType().Name == "DropDownList")
{
DropDownList Ddlist = new DropDownList();
Ddlist = (DropDownList)this.FindControl(ctls.ID);
Ddlist .SelectedIndex = 0;
}
}
}
如果大家有其它什么好的方法,希望可以拿出来分享
共同交流 共同学习
作者:zeke
出处:http://zhf.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。