c# - How do I call a JavaScript function from my code behind, the function being on a separate .js file? -
this question has answer here:
- calling javascript code behind 2 answers
code behind:
protected void page_load(object sender, eventargs e) { page.clientscript.registerclientscriptblock(gettype(), "mykey", "createnotificationtree(" + userid + ")", true); } .js file:
function createnotificationtree(userid) { var data = new kendo.data.hierarchicaldatasource({ transport: { read: { url: "../api/notifications/byuserid/" + userid, contenttype: "application/json" } }, schema: { model: { children: "notifications" } } }); $("#treeview").kendotreeview({ datasource: data, loadondemand: true, checkboxes: { checkchildren: true }, datatextfield: ["notificationtype", "notificationdesc"] }); } markup (do need this?):
<script type="text/javascript"> $(document).ready(createnotificationtree(userid)); </script> this not working. treeview not loaded on page.
in script block not assigning createnotificationtree called on $(document).ready, call right there. correct way is:
<script type="text/javascript"> $(document).ready(function () { createnotificationtree(<%= this.userid.tostring() %>); }); </script> then don't need registerclientscriptblock in page_load.
the second option not use $(document).ready, call registerstartupscript (which writes script @ bottom of page, when dom elements available) instead of registerclientscriptblock in page_load.
Comments
Post a Comment