Reset Input Text Boxes or other Controls

Posted on: Tue Oct 21 17:29:49 -0700 2008. Updated on: Tue Oct 21 17:39:43 -0700 2008.
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')");
}

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.