ASP.Net : Need to run javascript on update panel load completed

前端 未结 4 1496
攒了一身酷
攒了一身酷 2021-01-11 18:43

I need to run a javascript function when the update panel is loaded completely(I want to scroll), and not on initial page load.

Please suggest.

Thanks

相关标签:
4条回答
  • 2021-01-11 19:23

    If you are using AJAX then the only way i have found yet to give an alert to a user on return to the Asynchronous post back is to add an “end request” handler to the PageRequestManager.

    In this way you can tell the request manager to run a javascript function on returning from a Asynchronous post back event of AJAX.

    Code for doing this is :

    function load()
    
    {
       Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    }
    

    where “EndRequestHandler” will be the name of your javascript function you want to call. Call the above function in Onload event of tag:

    <body onload=”load()”>
    
    function EndRequestHandler()
    
    {
    
              alert(“You record has been saved successfully”);
    
    }
    

    Now If you want to give a different message based on your logic in server side code (code behind) then you can use a server side Hidden Field:

    <input id=”hdnValue” type=”hidden” runat=”server”  value=”" />
    

    Set its value in server side code on Asychronous Post Back:

    Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateSample.Click

        If condition Then
    
        hdnValue.value = “do this”
    
        Else     
    
        hdnValue.value = “do that” 
    
        End If 
    
    End Sub
    

    Now you can check the value of this Hidden Field in your Client Side EndRequestHandler function and give a different alert to user based on its value:

    function EndRequestHandler()
    {
         if (document.getElementById(‘<%= hdnValue.ClientID %>’).value == “do this”)
    
         { 
              alert(“You record has been saved successfully”);
         } 
         else
         {
              alert(“There is an error”);
         }
     }
    
    0 讨论(0)
  • 2021-01-11 19:29

    you can use below code with if jquery is used

    This is to show saved message and hide that message after 5 seconds after update panel is updated

    function pageLoad() {
                window.Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
            }
            function EndRequestHandler()
            {
                window.setTimeout(function () {
                    var label = window.$get('<%= lblMsg.ClientID%>');
                    if (label != null) { label.style.display = 'none'; }
                }, 5000);
            }
    
    0 讨论(0)
  • 2021-01-11 19:34

    This is the way to get the end Event after the update.

    <script type="text/javascript">
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(EndRequest);
    
        function EndRequest(sender, args) {
        }
    </script>
    
    0 讨论(0)
  • Untested

    <script type="text/javascript">
      var app = Sys.Application;
      app.add_init(ApplicationInit);
    
      function ApplicationInit(sender) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (!prm.get_isInAsyncPostBack())
        {
            prm.add_pageLoaded(PageLoaded);
        }
      }
    
      function PageLoaded(sender, args) {
        //Do something
      }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题