skip to Main Content

I can not call javascript function after page load inside update panel:

Here is my UpdatePanel:

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode ="Conditional"  runat="server" >
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ButtonLoadGridView" />                  
    </Triggers>                
 <ContentTemplate>`

My Button:

protected void ButtonLoadGridView_Click(object sender, EventArgs e)
{
    GridView1.DataSource = "dsData";
    GridView1.DataBind();
}

Here is javascript function:

window.onload = function ti() {     
    document.getElementById('<%= Label_Clients.ClientID %>').scrollIntoView({ behavior: 'smooth' });
    alert("You data is loaded');
}

I have tried this:

<script>      
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(windows.onload;
</script>

The problem is that after loading gridview on button click will not call javascript windows.onload function just only when use PostBackTrigger instead of AsyncPostbackTrigger please help me!

2

Answers


  1. The simples solution would be to create a separate function that is called on page load and on UpdatePanel load.
    ps you have a double and single quote in the alert() part.

    <script>
        var prm = Sys.WebForms.PageRequestManager.getInstance();
    
        //this code is executed after the UpdatePanel has finished loading.
        prm.add_endRequest(function () {
            MyFunction();
        });
    
        //normal window.onload
        window.onload = function ti() {
            MyFunction();
        }
    
        //the javascript you want executed on page load and updatepanel
        function MyFunction() {
            document.getElementById('<%= Label_Clients.ClientID %>').scrollIntoView({ behavior: 'smooth' });
            alert('You data is loaded');
        }
    </script>
    
    Login or Signup to reply.
  2. ok, so with a update panel, then only "part of" the page goes through the standard page life cycle.

    So, your idea to "attach" the event using GetInstance looks to be the correct road.

    I would try this syntax:

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    //this code is executed after the UpdatePanel has finished loading.
    prm.add_endRequest(MyFunction);
    

    The above should trigger/run your "MyFunction".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search