While doing asynchronous postback page losings gridviewscroll script?

半城伤御伤魂 提交于 2020-01-16 18:09:06

问题


While doing asynchronous postback page losings gridviewscroll script even after i did script register on code behind. (I have grid in update panel.. This happen when clicking "add new row" in asp.net grid view.)

I have tried with the following three methods.

ScriptManager.RegisterStartupScript(UpdatePanel_Objective, this.GetType(), UpdatePanel_Objective.UniqueID, "gridviewScroll();", true);

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "gridviewScroll();", true);

StringBuilder sb = new StringBuilder();
sb.Append("<script src='js/jquery-1.11.1.min.js'></script>");
sb.Append("<script src='js/jquery-ui.min.js'></script>");
sb.Append("<script src='js/gridviewScroll.min.js'></script>");
sb.Append("<script src='js/gridviewScroll.js'></script>");
sb.Append("<script type = 'text/javascript'>");
sb.Append("$('#<%=Objective.ClientID%>').gridviewScroll({height: 500,freezesize: 2,headerrowcount: 2});");
sb.Append("</script>");
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", sb.ToString(), true);

None of the above is worked. Please advise. Thanks.


回答1:


You need to trigger gridviewScroll again, even after an async PostBack. Even if you do not see it, the UI is updated and all bindings made with jQuery are lost and have to be rebound. So use the EndRequest functionality.

<script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_endRequest(function () {
        createGridviewScroll()
    });

    $(document).ready(function () {
        createGridviewScroll();
    });

    function createGridviewScroll() {
        //do your thing
    }
</script>

PS doing this sb.Append("$('#<%=Objective.ClientID%>') does not do much because you won't get the correct ClientID that way. Use

sb.Append("$('#" + Objective.ClientID + "').gridviewScroll({height: 500,freezesize: 2,headerrowcount: 2});");


来源:https://stackoverflow.com/questions/49188425/while-doing-asynchronous-postback-page-losings-gridviewscroll-script

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