I recently came across an issue where JQuery bindings no longer functioned after a partial postback and stumbled upon some code that was helpful in most cases.
Javascript (JQuery):
Sys.Application.add_load(startJQuery);
startJQuery() {
//do JQ here
}
And alternatively..
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(){});
This code will not always work however, as was the case in my particular scenario, so I resolved using an alternate method.
RegisterStartupScript is great functionality, and can be useful for loading javascript dynamically on an as-needed basis.
The example below, based on prior code selects the correct JS file to use, then loads it using the registerstartupscript function. This is all within a code block which calls an update panel
C#:
int scriptnumber = 1;
string FilePath = String.Format("~/Scripts/Script_{0}.js",scriptnumber.ToString());
System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(FilePath));
jqueryfileoutput = sr.ReadToEnd();
upnlBodyContent.Update();
ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptname",
"<script type=\"text/javascript\">" + jqueryfileoutput.ToString().Trim() + "</script>", false);
Leave a comment