Bruno
Bruno

Reputation: 884

Javascript event on page postback

Is there any javascript event which is triggered on postback?

If not, how can I run client side code immediately after or before a page postback?

Upvotes: 19

Views: 25066

Answers (7)

Michel Ayres
Michel Ayres

Reputation: 5985

Take a look at: Run javascript function after Postback

I solved my problem using this:

<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function (s, e) {
        alert('Postback!');
    });
</script>

there are a lot of options too, like

$('#id').live('change', function (){});

$(document).ready(function () {});
ClientScriptManager.RegisterStartupScript(this.GetType(), "AKey", "MyFunction();", true);

and keep going. depends on what you need.

PageRequestManager events: https://learn.microsoft.com/en-us/previous-versions/aspnet/bb398976(v=vs.100)

Upvotes: 5

jeremysawesome
jeremysawesome

Reputation: 7254

I believe what you are looking for is the Sys.WebForms.PageRequestManager beginRequest Event

Excerpt:

The beginRequest event is raised before the processing of an asynchronous postback starts and the postback is sent to the server. You can use this event to call custom script to set a request header or to start an animation that notifies the user that the postback is being processed.

Code Sample: (From the link)

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args)
    {
        var elem = args.get_postBackElement();
        ActivateAlertDiv('visible', 'AlertDiv', elem.value + ' processing...');
    }
    function EndRequestHandler(sender, args)
    {
        ActivateAlertDiv('hidden', 'AlertDiv', '');
    }
    function ActivateAlertDiv(visstring, elem, msg)
    {
        var adiv = $get(elem);
        adiv.style.visibility = visstring;
        adiv.innerHTML = msg;                     
    }
</script>

I hope that helps. The PageRequestManager class seems to be little known about and little utilized.

Upvotes: 5

Jim Schubert
Jim Schubert

Reputation: 20357

The Page.ClientScript object has a RegisterOnSubmitStatement This fires after any input submits the form. This may or may not be what you're looking for, but I've used it for notifying the user of unsaved changes in editable forms.

The advantage to using this over RegisterStartupScript is that with RegisterOnSubmitStatement, if a user navigates away and back using the browser, whatever script you've injected using RegisterStartupScript could possibly fire again, whereas RegisterOnSubmitStatement will only run if the user has submitted the form.

Upvotes: 3

Jeff Sternal
Jeff Sternal

Reputation: 48583

There isn't a javascript event triggered when a page loads after a postback, but you can add javascript to your html template (.aspx file) and only run it if the page was posted, like this:

<script type='text/javascript'>
    var isPostBack = '<%= this.IsPostBack%>' == 'True';
    if (isPostBack) {
        alert('It's a PostBack!');
    }
</script>

If you want to customize the javascript to run only under particular conditions (not just any postback), you can create a page-level variable (protected or public) in your page's class and do something similar:

var userClickedSubmit = '<%= this.UserClickedSubmit%>' == 'True';
if (userClickedSubmit) {
    // Do something in javascript
}

(Nothing against ClientScript.RegisterStartupScript, which is fine - sometimes you want to keep your javascript in the page template, sometimes you want to keep it in your page class.)

Upvotes: 1

TFD
TFD

Reputation: 24534

The onsubmit event on the form tag

When using jQuery it's like this

$("#yourformtagid").submit(function () {
 ...
}

Upvotes: 1

Germ
Germ

Reputation: 6540

You could add the javascript in your page load like this...

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", 
    "alert('hello world');", true);

OR

Page.ClientScript.RegisterStartupScript(this.GetType(), "alertScript",
   "function Hello() { alert('hello world'); }", true);

Upvotes: 4

Vivin Paliath
Vivin Paliath

Reputation: 95498

Use AJAX, with an event handler for the onComplete.

Upvotes: 1

Related Questions