Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 683
Bruno Avatar asked Feb 16 '10 21:02

Bruno


People also ask

How do you postback a page using JavaScript?

How to Raise a Postback from JavaScript? To do this, we need to just call the __doPostBack() function from our javascript code. When the above function is called, it will raise a postback to server.

What is the use of __ doPostBack in JavaScript?

The __doPostBack function contains two arguments, eventTarget and eventArgument. The eventTarget is “Button2” and the eventArgument is “My Argument.” Later, in the C# code behind, I have accessed the eventArgument using the Request. Params collection. The passedArgument variable will contain the value “My Argument.”

What is postback event?

A Postback Event is a string of information that is sent to a network's specific URL that contains information about the post-install event pertinent to the network.

How can I tell if a page is postback in jQuery?

You can use something like document. getElementById("__VIEWSTATE") or the jQuery equivalent. However, if you want to see whether the current page was generated in response to a postback, then you need to insert that data into the page on the server side first.


1 Answers

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.

like image 156
jeremysawesome Avatar answered Oct 05 '22 23:10

jeremysawesome