Reset Input Text Boxes or other Controls
Category: Asp .Net AJAX / Javascript / CSS
This came in handy when I was using the ModalPopupExtender to pop up a Panel to add a new record. After I had added one item, and hit add again, it would still show the old data. So, I needed a way to clear the text boxes with javascript.
First add the following javascript function in the head, or referenced js file. If you are using master pages, it can go in there.
function clearTheInputs(inputType)
{
tags = document.getElementsByTagName("input");
for(i = 0; i < tags.length; i++) {
if(tags[i].type==inputType) {
tags[i].value = "";
}
}
}
Now, to call this simply do something like this. You could add this in the designer but I'll do it from the codebehind just to demonstrate how it would be done from cs.
protected void Page_Load(object sender, EventArgs e)
{
LinkButtonAddTemplate.Attributes.Add("onclick", "javascript:clearTheInputs('text')");
}