How to clear all textboxes on a page
I have a page with a whole lot of asp.net textboxes asp:TextBox
. I want to have a clear button which will clear the text from all the text开发者_开发技巧Boxes. The textBoxes are all within their own usercontrol. How could this be done?
<input type='Reset' value='clear'/>
Will reset all the text fields inside that particular form when clicked.
jQuery is your friend:
$("#theButton").click(function() {
$("[type=text]").val("");
});
You can use <input type="reset" />
to do this..
Altenately you can assign a cssclass to each textbox and use jQuery to clear them.
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;
}
}
}
}
on the clear button event use this
textBox1.Clear();
and for a label you could use this
label1.Text = "";
it's that simple.
Using this method, we can clear the text stored in a textbox easily
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);
}
}
精彩评论