Skip to content Skip to sidebar Skip to footer

Stop Page Reload Of An ASP.NET Button

NET application, I have inserted a button that call a Javascript function (OnClientClick event) and a VB.NET function (OnClick event)

Solution 1:

You need to use return statement at two points.

OnClientClick="return jsfunction();"

function jsfunction()
{
  //
  return false;
}

OR, you can return false after the function call like this.

OnClientClick="jsfunction(); return false;"

Note if you want to do postback conditionally then you need to return true or false.

OnClientClick="return jsfunction();"

function jsfunction()
{
  if(conditionForPostBack)
      return true;
  else
      return false;
}

Solution 2:

or you can disable the submit behaviour. By default asp.net renders button as submit button. if you disable submit behaviour it will render button as button type

<asp:Button UseSubmitBehavior="false" OnClientClick="jsfunction() " OnClick="vbfunction" Text="Submit" runat="server" />

But with this code it will not fire server side event "OnClick"


Solution 3:

if you are not going to trigger the button with C# Codebehind function, then you dont need to use asp:Button. Therefore you can use a regular html .

<button id='btn_1' onclick='ajax_function()'>Button</button>

html button is much easier and faster. if you use asp:button, then you should use clientid() function to catch the control to trigger the ajax.


Solution 4:

Searching for the same thing as you i find a patch:

If you call a method server side, you can use AJAX with the update panel, but that didn't worked for me. But you can save what you want in Session, so you have it as far as Session lasts.

// Save at SessionParameter the elementToSave we want.
this.Session["SessionParameter"] = elementToSave;

// Retrieve the info from the Session
ElementYouNeededToSave = (TypeOfTheElement)Session["SessionParameter"];

Hope this will help someone in my situation.


Post a Comment for "Stop Page Reload Of An ASP.NET Button"