jQuery is not working after partial postback

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 03:51:45
k25

If you want to execute some javascript after partial postback, you can follow either of these methods:

  • Assuming you have a <asp:ScriptManager /> in your page you can include a function called pageLoad()

      <script type="text/javascript" language="javascript">
       function pageLoad()
       {
        // JS to execute during full and partial postbacks
       }
      </script>
    
  • Or check out this link that explains about executing javascript during partial postbacks

http://csharperimage.jeremylikness.com/2009/06/inject-dynamic-javascript-into-aspnet.html

  • Another way is to use the Sys.Application.add_init handler as given below:

    Sys.Application.add_init(appl_init);

    function appl_init() {
        var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
         pgRegMgr.add_beginRequest(StartHandler);
         pgRegMgr.add_endRequest(EndHandler);
    }
    function StartHandler() {
        // your js
    }
    function EndHandler() {
        // your js
    }
    

Hope this helps!

To call this function after partial postback, what u need to do is re-register external js file. The following code does this.

private void RegisterClientStartupScript()
{
    string path = Page.ResolveUrl("~/Script/slider.js");
    ScriptManager sManager = ScriptManager.GetCurrent(this.Page);
    if (sManager != null && sManager.IsInAsyncPostBack)
    {            
        ScriptManager.RegisterClientScriptInclude(
           this.updSpeech, typeof(string),"include-js",
           path); 
         ScriptManager.RegisterStartupScript(this.updSpeech, this.updSpeech.GetType(), "SliderScript",
            "FunctionName();", true); 

    }
    else
    {
        this.Page.ClientScript.RegisterClientScriptInclude("SliderScript", path);
    }
}    

Call this function on every page load i.e outside (!isPostback). This code gets reference to external js file and in case it is a partial postback,it registers it with the scriptmanager. In case it is a normal postback it registers it with clientScript.

EDIT

Try giving the jquery function a name and then call ScriptManager.RegisterStartupScript().see the above code and replace FunctionName() with the name of ur jquery function. In the above code this.updSpeech is the updatepanel.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!