locked
What's Sys.WebForms.PageRequestManager? RRS feed

  • Question

  • User-134477160 posted

    Hi everyone,

    I meet a question——At my server-side codes, I put a button and a textbox inside the UpdatePanel.

    And at the beginning, I wrote the codes like this following in jQuery to assign values:

    <head runat="server">
        <title>Test</title>
        <script src="Scripts/jquery-2.0.0.js"></script>
        <script>
            $(function ()
            {
                $('#<%=TextBox1.ClientID%>').val("I'm Bill Gates!");
            });
        </script>
    </head>

    It works when first running, but when I click the button inside the update panel, it doesn't work anymore!
    After searching for the web, I know to use Sys.WebForms.PageRequestManager:
    But my question:

    1) Why use this?
    2) What's that? Is that devoted for UpdatePanel or anything related to AJAX refreshing(even if I'm not using UpdatePanel but I just customized a page that will submit the data contents asychronizedly……), will it work properly?

    Many thanks for good explaination!

    Reguards!!!!!!!!!!!!

    Wednesday, May 1, 2013 10:50 PM

Answers

  • User281315223 posted

    As you can see by the definition available within the MSDN documentation for Sys.WebForms.PageRequestManager, it is used to manage the partial-page requests for UpdatePanel controls as well as well as handling other client-side scripting operations.

    1.) Why use this?

    As previously mentioned - the PageRequestManager actually manages and handles all of the information and events relating to performing partial-page requests that are used within Update Panels. It allows you to update specific regions of your page during PostBacks (UpdatePanel areas).

    2. What's that?

    While one of its major functions is handling the requests for Update Panel controls that is not its only function. You can use it to define additional client-side scripts and when they will execute within your page. In the example below from the documentation, you can see several events that are defined within the PageRequestManager instance : 

    <script type="text/javascript" language="javascript">
             //Events being defined by the PageRequestManager
             Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
             Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
             //These functions will be handled by the cooresponding events declared above.
             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>

    So you don't need an UpdatePanel to use some of the functionality that these offer (and these are not required for AJAX refreshing at least if you are using something like jQuery to handle your requests).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2013 11:23 PM

All replies

  • User555306248 posted

    When the UpdatePanel refreshes, it destroys the DOM for those TextBoxes, removing the jQuery event handlers in the process.  Use pageLoad() instead of $(document).ready(), and they will be wired back up after every partial postback.

    http://forums.asp.net/t/1892632.aspx/1

    Wednesday, May 1, 2013 11:16 PM
  • User281315223 posted

    As you can see by the definition available within the MSDN documentation for Sys.WebForms.PageRequestManager, it is used to manage the partial-page requests for UpdatePanel controls as well as well as handling other client-side scripting operations.

    1.) Why use this?

    As previously mentioned - the PageRequestManager actually manages and handles all of the information and events relating to performing partial-page requests that are used within Update Panels. It allows you to update specific regions of your page during PostBacks (UpdatePanel areas).

    2. What's that?

    While one of its major functions is handling the requests for Update Panel controls that is not its only function. You can use it to define additional client-side scripts and when they will execute within your page. In the example below from the documentation, you can see several events that are defined within the PageRequestManager instance : 

    <script type="text/javascript" language="javascript">
             //Events being defined by the PageRequestManager
             Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
             Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
             //These functions will be handled by the cooresponding events declared above.
             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>

    So you don't need an UpdatePanel to use some of the functionality that these offer (and these are not required for AJAX refreshing at least if you are using something like jQuery to handle your requests).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2013 11:23 PM
  • User-134477160 posted

    Many thanks!

    page_load seems to be a client javascript that I only know it can happen at server-side:(

    And let's continue talk about Sys.WebForms.PageRequestManager.

    What's that?

    Is that only used for UpdatePanel?

    If I use jQuery plug-ins or something customized to make a parial page "refresh", will it also work?

    Wednesday, May 1, 2013 11:25 PM
  • User281315223 posted

    As I mentioned in my answer -

    This isn't only used for UpdatePanels (although one of its primary purposes is to handle the partial-page loading for them) and use a jQuery AJAX or POST call will allow you to perform the same partial-page postback without the need for a PageRequestManager object.

    Wednesday, May 1, 2013 11:29 PM
  • User-134477160 posted

    Many thanks!

    However what I mean is:

    If I put like this in jQuery:

    <head runat="server">
        <title>Test</title>
        <script src="Scripts/jquery-2.0.0.js"></script>
        <script>
            $(function ()
            {
                //$('#<%=TextBox1.ClientID%>').val("I'm Bill Gates!");
                var content = Sys.WebForms.PageRequestManager.add_beginrequest(function(){$('#<%=TextBox1.ClientID%>').val("I'm Bill Gates!");});
            });
        </script>
    </head>

    This won't show the real contents in the textbox.

    And I know the reason bcoz this function ONLY applies on partial AJAX-based form.

    So do you mean if I don't use AJAX updatepanel but just use $.get(……), will the add_beginrequest work or be raised?

    Thursday, May 2, 2013 1:17 AM
  • User-134477160 posted

    What's that?

    function pageLoad(sender, args) {
    
                // binding code here, for example
                jQuery(".content").hide();
                //toggle the componenet with class msg_body
                jQuery(".heading").click(function () {
                    jQuery(this).next(".content").slideToggle(500);
                });
            }

    Why can it also be applied to client side? It should be a server event.

    Do u mean any server-side based event can be also applied to client one? And it will be executed at client side?
     

    Thursday, May 2, 2013 1:20 AM
  • User281315223 posted

    When you attempt to call your function - I would recommend that you actually place your jQuery code within a seperate function to see if that makes any difference : 

    <head runat="server">
        <title>Test</title>
        <script src="Scripts/jquery-2.0.0.js"></script>
        <script type='text/javascript'>
            $(function ()
            {
                //You may want to try using the pageloaded event
                Sys.WebForms.PageRequestManager.add_pageloaded(addBillGates);
            
                function addBillGates(){
                       $('#<%= TextBox1.ClientID %>').val("I'm Bill Gates!");
                }
            });
        </script>
    </head>

    I would try using both an external function or possibly using the pageloaded event (which you can see within the documentation) to see if that makes any differences. If neither of those work - I would recommend using the developer tools within your browser to see what kind of errors are being thrown within your console.

    Thursday, May 2, 2013 8:16 AM