问题
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