Jquery with Update Panel

I have a problem when using jquery context menu and update panels. I am writing javascript context menu in the RenderBeginTag of a Customtextbox control using htmlTextWriter. everything works fine, I can right-click on each text field, and a menu will appear.
but when i triger partial postback using asp.net updatepanel, the menu will not be displayed. it seems that the binding between jquery and html is lost when a partial message occurs.
is there a better way to host dynamic javascript code other than RenderBeginTag? how can i solve this problem?

+3
source share
2 answers

You're right, an updated panel will remove your javascript bindings.

In your postback update block, reregister the corresponding javascript.

Sort of:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(typeof(Page), "ReApplyJavascript", "<script type=text/JavaScript>YourJavascriptInitMethod();</script>", false);

If this does not work. You may need to use:

   ScriptManager.RegisterStartupScript(Page, typeof(Page), "ReApplyJavascript", "<script type=text/JavaScript>YourJavascriptInitMethod();</script>", false);
+3
source

You need to reinitialize the menu after updating UpdatePanel.

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

function InitializeRequest(sender, args) {      
}

function EndRequest(sender, args) {
     // Here initialize the menou
}
</script>
+1
source

Source: https://habr.com/ru/post/1758014/


All Articles