I have a usercontrol and in that I have a javascript function which makes a call to 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 I run the code, it gives me javascript error, CountOfMenus is undefined
. I know the error is because it cant find the method in the current page but I want it to access method in the usercontrol.cs. I cant write the webmethod in every page as I have lots of pages where the usercontrol is used. Is there any way through which I can call the method of usercontrol.cs in javascript?
I have a usercontrol and in that I have a javascript function which makes a call to 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 I run the code, it gives me javascript error, CountOfMenus is undefined
. I know the error is because it cant find the method in the current page but I want it to access method in the usercontrol.cs. I cant write the webmethod in every page as I have lots of pages where the usercontrol is used. Is there any way through which I can call the method of usercontrol.cs in javascript?
3 Answers
Reset to default 3I solved this by below method
Javascript :
function GetRealTimeCount(StartDate, EndDate) {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for 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 to get count
Response.Write(Count.ToString());
Response.End();
}
The below link from where I got these solutions has many other options to call C# methods from javascript
http://www.morgantechspace./2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html
when your JS code calls a PageMethod using "PageMethods." , the call does not reach the page method if it was defined in the control. The page methods in the page are only callable.
I suggest another approach, using Http Handler which is also efficient. Try follow this post:
Call HttpHandler from javascript
Also, the following post might be useful:
http://www.undisciplinedbytes./2010/03/ajax-call-using-an-asp-net-http-handler/
Do you have ScriptManager in your UserControl Page?
if not you have to add it and set EnablePageMethods="true"
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745133753a4613100.html
评论列表(0条)