简体   繁体   English

从Usercontrol.ascx javascript调用Usercontrol.cs中的Webmethod

[英]Call Webmethod in Usercontrol.cs from Usercontrol.ascx javascript

I have a usercontrol and in that I have a javascript function which makes a call to webmethod. 我有一个usercontrol,并且我有一个javascript函数,它调用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 我的webmethod代码是

[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 . 但是当我运行代码时,它给了我javascript错误, 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. 我知道错误是因为它无法在当前页面中找到该方法但我希望它访问usercontrol.cs中的方法。 I cant write the webmethod in every page as I have lots of pages where the usercontrol is used. 我不能在每个页面中编写web方法,因为我有很多使用usercontrol的页面。 Is there any way through which I can call the method of usercontrol.cs in javascript? 有什么办法可以在javascript中调用usercontrol.cs的方法吗?

I solved this by below method 我通过以下方法解决了这个问题

Javascript : 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 从我获得这些解决方案的下面的链接有许多其他选项来从javascript调用C#方法

http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html

when your JS code calls a PageMethod using "PageMethods." 当您的JS代码使用“PageMethods”调用PageMethod时。 , 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. 我建议使用另一种方法,使用Http Handler也很有效。 Try follow this post: 试试看这篇文章:

Call HttpHandler from javascript 从javascript调用HttpHandler

Also, the following post might be useful: 此外,以下帖子可能有用:

http://www.undisciplinedbytes.com/2010/03/ajax-call-using-an-asp-net-http-handler/ http://www.undisciplinedbytes.com/2010/03/ajax-call-using-an-asp-net-http-handler/

Do you have ScriptManager in your UserControl Page? 你的UserControl页面中有ScriptManager吗? if not you have to add it and set EnablePageMethods="true" 如果不是,你必须添加它并设置EnablePageMethods="true"

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM