Page post back after JavaScript code executed on server control click

July 21, 2006 at 7:50 am 12 comments

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.

Advertisements

Entry filed under: ASP.NET Tips.

Maintaining the Scroll Position after post back Nested Gridview to show Master/Details relationship in ASP.NET 2.0

12 Comments Add your own

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

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

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

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

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

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

    Add this page directive

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

    <%@ Page MaintainScrollPositionOnPostback="true" %>

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

    cqkgjytmwg cqkgjytmwg cqkgjytmwgcqkgjytmwg
    cqkgjytmwgcqkgjytmwgcqkgjytmwg cqkgjytmwg

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

    flat flat buy apartament

    Reply
  • 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!

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

    fhgggdgfff

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

    Spread your love around!

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

    nice

    Reply
  • 12. Cialis  |  March 6, 2010 at 8:54 pm

    dunecz Excellent article, I will take note. Many thanks for the story!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Top Clicks

  • None

Calendar

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

Recent Posts

Feeds

Blog Stats

  • 93,065 hits

%d bloggers like this: