c# - Call Webmethod in Usercontrol.cs from Usercontrol.ascx javascript -
i have usercontrol , in have javascript function makes call webmethod.
<%@ control language="c#" autoeventwireup="true" codefile="leftmenu.ascx.cs" inherits="usercontrols_leftmenu" %> <script type="text/javascript"> function getrealtimecount() { pagemethods.countofmenus('', '', getcountofmenus_success, getcountofmenus_fail); }
my webmethod code is
[system.web.services.webmethod] public static string countofmenus(string startdate, string enddate) { //code here }
but when run code, gives me javascript error, countofmenus undefined
. know error because cant find method in current page want access method in usercontrol.cs. cant write webmethod in every page have lots of pages usercontrol used. there way through can call method of usercontrol.cs in javascript?
i solved below method
javascript :
function getrealtimecount(startdate, enddate) { var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } var url = "default.aspx?method=countofmenus&sd=" + startdate + "&ed=" + enddate; xmlhttp.open("get", url, false); xmlhttp.send(null); document.getelementbyid("count").innerhtml = xmlhttp.responsetext; }
code behind:
protected void page_load(object sender, eventargs e) { if (request.querystring["method"] == "countofmenus") { response.cache.setcacheability(httpcacheability.nocache); getcount(request.querystring["sd"], request.querystring["ed"]); } } private void getcount(string startdate, string enddate) { response.clear(); // code count response.write(count.tostring()); response.end(); }
the below link got these solutions has many other options call c# methods javascript
http://www.morgantechspace.com/2014/01/call-server-side-function-from-javascript-in-asp-net.html
Comments
Post a Comment