【发布时间】:2011-03-22 04:05:45
【问题描述】:
我有一个包含大量 asp.net 文本框 asp:TextBox 的页面。我想要一个清除按钮,它将清除所有文本框中的文本。文本框都在它们自己的用户控件中。这怎么可能?
【问题讨论】:
标签: asp.net user-controls textbox
我有一个包含大量 asp.net 文本框 asp:TextBox 的页面。我想要一个清除按钮,它将清除所有文本框中的文本。文本框都在它们自己的用户控件中。这怎么可能?
【问题讨论】:
标签: asp.net user-controls textbox
<input type='Reset' value='clear'/>
单击时将重置该特定表单内的所有文本字段。
【讨论】:
jQuery 是你的朋友:
$("#theButton").click(function() {
$("[type=text]").val("");
});
【讨论】:
您可以使用<input type="reset" /> 来执行此操作..
您也可以为每个文本框分配一个 cssclass 并使用 jQuery 来清除它们。
【讨论】:
protected void btnClear_Click(object sender, EventArgs e)
{
ClearControls();
}
private void ClearControls()
{
foreach (Control c in Page.Controls)
{
foreach (Control ctrl in c.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Text = string.Empty;
}
}
}
}
【讨论】:
在清除按钮事件上使用这个
textBox1.Clear();
对于标签,您可以使用它
label1.Text = "";
就这么简单。
【讨论】:
使用这种方法,我们可以轻松清除文本框中存储的文本
protected void Reset_Click(object sender, EventArgs e)
{
ClearInputs(Page.Controls);
}
void ClearInputs(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Text = string.Empty;
ClearInputs(ctrl.Controls);
}
}
【讨论】: