Page post back after JavaScript code executed on server control click

July 21, 2006

I created a ASP.NET page with two text boxes and a button. I want this button to reset the form(Only to reset the page). There are two ways you can do this, one is going to the click event of the button and add the code to clear the text boxes. Like the one given below

C#

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}

but this technique is very heavy, bcz it need another round trip to the server.

The other way is using the JavaScript. You need to add an attribute to the button in the page load event. Like this

protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add(“onclick”, “JavaScript:document.forms[0].reset(); return false;“);
}

what this code does is that, The button is rendered like this

<input type=”submit” name=”Button1″ value=”Button” onclick=”JavaScript:document.forms[0].reset(); return false;” id=”Button1″ />

so that the page wiill be automatically reset when the user clicks the button (Completely handled by the browser, no round trip to the server).

The code that I bolded (return false;), its very important, other wise the page will be posted back.

Entry Filed under: ASP.NET Tips. .

11 Comments Add your own

  • 1. sindhura  |  August 17, 2006 at 9:07 am

    itz really good..i hav used it
    thank u so much

  • [...] Original post by Rajeesh and software by Elliott Back ASP.NET Tips» [...]

  • 3. albCode  |  November 8, 2006 at 7:27 am

    How can i save scro bar position in page.
    Page belong to masterpage.

  • 4. Renjith  |  November 8, 2006 at 5:54 pm

    Add this page directive

  • 5. Renjith  |  November 8, 2006 at 5:56 pm

    <%@ Page MaintainScrollPositionOnPostback="true" %>

  • 6. cqkgjytmwg  |  November 26, 2007 at 5:08 pm

    cqkgjytmwg cqkgjytmwg cqkgjytmwgcqkgjytmwg
    cqkgjytmwgcqkgjytmwgcqkgjytmwg cqkgjytmwg

  • 7. House appartament  |  December 18, 2007 at 5:46 am

    flat flat buy apartament

  • 8. priya  |  April 11, 2008 at 3:53 pm

    This code is something similar to what I want to do on one of my forms but instead of clearing the form page I want to be able to save the entries inputed in each field then click an “add another” button that refreshes the page displaying the items just entered and the form fields are empty ready for another entry.

    Any help in how to do this would be greatly appreciated.

    thanks!

  • 9. df  |  February 19, 2009 at 12:24 pm

    fhgggdgfff

  • 10. the love guru  |  February 25, 2009 at 12:38 pm

    Spread your love around!

  • 11. reddy  |  June 12, 2009 at 6:27 am

    nice

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Top Posts

Top Clicks

Calendar

July 2006
M T W T F S S
    Aug »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tags

ASP.NET ASP.NET Tips WPF WPF Blog WPF Controls WPF thirdparty controls

a

Recent Posts

2

Fav links

Feeds

Blog Stats