第一种方法:[通过服务器事件]
方法实现:
//在后台代码实现中,如果遇到容器则需要递归调用,
//这就是为什么上面的代码中会有 foreach 循环中再次调用自己的原因.
public void ClearTextBox(Control c)
{
if (c.Controls != null)
{
foreach (Control x in c.Controls)
{
if (x is TextBox)
{
((TextBox)x).Text = "";
}
arry.Add(x.ToString());
FindButton(x);
}
}
}
方法调用:
protected void btnClear_Click(object sender, EventArgs e)
{
ClearTextBox(this);
}
第二种方法:[JavaScript方法]
//将TextBox的TextMode设置成TextMode="MultiLine"时,生成的html标记TextBox为Textarea标签而非type为text的input标签,
//所以这个时候 TextBox的数据无法清除
function ClearTextBox()
{
for (i = 0; i < window.document.forms[0].elements.length; i++)
{
if (window.document.forms[0].elements[i].type == "text")
{
window.document.forms[0].elements[i].value = "";
}
}
}
给服务器按钮添加onclick事件;
protected void Page_Load(object sender, EventArgs e)
{
btnClear.Attributes.Add("onclick", "ClearTextBox();");
}