如何在javascript事件中通过参数调用页面方法?

Any*_*are 16 javascript asp.net ajax jquery pagemethods

我的.cs中有这样的方法:

[System.Web.Services.WebMethod]
public static void GetServiceInformation(IInfo x) //IInfo  is an interface
{
    x.l_power = true;
    x.lb_InboxCount = UserTrans.GetInbox(int.Parse(emp_num), 0);
}
Run Code Online (Sandbox Code Playgroud)

现在我想通过javascript方法调用此方法作为页面方法,但它不起作用.

<script type ="text/javascript">

    function GetInfo() {
        PageMethods.GetServiceInformation(this);
    }
   window.onload = setTimeout("GetInfo()", 3000);
</script>
Run Code Online (Sandbox Code Playgroud)
  <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true">
  </telerik:RadScriptManager>
Run Code Online (Sandbox Code Playgroud)

我的.cs:

 public partial class AppMaster : Log, IInfo //My page
    {
        public string Inbox
        {
            get
            {
                return hpl_Inbox.NavigateUrl;
            }

            set
            {
                hpl_Inbox.NavigateUrl = value;
            }
        }
        public string Draft
        {
            get
            {
                return hpl_Draft.NavigateUrl;
            }

            set
            {
                hpl_Draft.NavigateUrl = value;
            }
        }

        public string New
        {
            get
            {
                return hpl_New.NavigateUrl;
            }
            set
            {
                hpl_New.NavigateUrl = value;
            }
        }
        public string Approved
        {
            get
            {
                return hpl_Approved.NavigateUrl;
            }
            set
            {
                hpl_Approved.NavigateUrl = value;
            }
        }
    //------- etc
 }
Run Code Online (Sandbox Code Playgroud)

我的界面:

public interface IInfo
    {
        string Inbox { get; set; }
        string Draft { get; set; }
        string New { get; set; }
        string Approved { get; set; }
        string archive { get; set; }
        string search { get; set; }
        string cand { get; set; }
        string pri { get; set; }
        string power { get; set; }
        string admin { get; set; }
        string help { get; set; }
        bool l_cand { get; set; }
        bool l_pri { get; set; }
        bool l_power { get; set; }
        bool l_admin { get; set; }

        string lb_ApprovedCount { get; set; }
        string lb_InboxCount { get; set; }
        string lb_archive { get; set; }
        string lb_DraftCount { get; set; }

    }
Run Code Online (Sandbox Code Playgroud)

小智 6

function GetServiceInformation(x) {

    $.ajax({
        type: "POST",
        url: "page.aspx/GetServiceInformation",
        data: "{'x':'" + x + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: on_sucess,
        error: on_error
    });
    function on_sucess(data, status) {

        alert(data);

    }
    function on_error(request, status, error) {

        alert(error);
    }

}
Run Code Online (Sandbox Code Playgroud)

对不起,如果它不起作用


Leo*_*Leo 6

根据聊天讨论回答编辑

首先,感谢您澄清您的问题.有点难以理解你试图解决的问题.原因?因为您的代码不够清晰,并且通常在存在设计问题时发生.这实际上是你面临的一个设计问题.首先,我会指出一些错误......

在这个javascript函数中......

function GetInfo() {
        PageMethods.GetServiceInformation(this);
    }
Run Code Online (Sandbox Code Playgroud)

this不是您网页的实例.所以没有必要让你的页面实现一个接口......

public partial class AppMaster : Log, IInfo{}
Run Code Online (Sandbox Code Playgroud)

并期望javascript调用会将System.Web.UI.Page您的类的实例分页(更不用说IInfo接口的实现).你可以盲目地放弃这种方法,因为它是一个永久性的设计问题,甚至都无法发挥作用.

现在,如果你想要的是服务页面,那么做一些进一步的处理,最后使用javascript/ajax异步地将这个处理的结果发送回客户端你有几种方法:

  1. 使用SignalR哪种是我最喜欢的方法(但你已经说过你的解决方案不符合使用要求SignalR)
  2. 使用jQueryajax也是一种非常有效的方法

现在,我将解释第二种方法

使用jQueryAjax

只需像往常一样渲染页面ASP.NET.然后在客户端,当页面加载时,发出ajax请求以开始处理您要显示的信息.您可以在页面加载后立即启动请求以在服务器上进行处理

$(function(){
    $.ajax({
        type: 'POST',
        url: 'AppMaster.aspx/GetServiceInformation',
        data: "{}",
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        success: function(d) {
            //load data received
            },
        error: function() {
            //process the error
            }
    });
});
Run Code Online (Sandbox Code Playgroud)

在成功处理程序中,您需要在Web控件上加载从ajax调用接收的值.然后将您的IInfo界面更改为单独的代码文件中的具体对象.但是,请记住,此类不应该对您的Web控件提供任何引用

public class JSInfo
{
    string Inbox { get; set; }
    string Draft { get; set; }
    string New { get; set; }
    string Approved { get; set; }
    string archive { get; set; }
    string search { get; set; }
    string cand { get; set; }
    string pri { get; set; }
    string power { get; set; }
    string admin { get; set; }
    string help { get; set; }
    bool l_cand { get; set; }
    bool l_pri { get; set; }
    bool l_power { get; set; }
    bool l_admin { get; set; }

    string lb_ApprovedCount { get; set; }
    string lb_InboxCount { get; set; }
    string lb_archive { get; set; }
    string lb_DraftCount { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

然后将您的页面方法更改为...

[System.Web.Services.WebMethod]
public static JSInfo GetServiceInformation()
{
    //you need to get the emp_num from session

    //construct the JSInfo object
    JSInfo info = new JSInfo();

    //get the data from the database
    var data = UserTrans.GetInbox(int.Parse(emp_num), 0);

    //set the properties of the JSInfo...similar to the line below for each property...Draft, New, Approved, etc
    info.Inbox = data.Inbox;

    //return the object to the client
    return info;
}
Run Code Online (Sandbox Code Playgroud)

请注意,您需要emp_numSession聊天讨论中说明此值来自Session变量后获取值.现在,返回到jQueryajax调用的成功处理程序,该调用程序在从服务器收到响应后立即执行.您将在handler参数中收到一个json对象,其中d包含JSInfo您刚刚从服务器发送的类的属性.然后你在页面上设置控件......

success: function(d) {
            $('#id_inbox_control').val(d.Inbox);
            $('#id_draft_control').val(d.Draft);
            $('#id_new_control').val(d.New);

            //and keep doing the same for the rest of the controls
        },
Run Code Online (Sandbox Code Playgroud)

这应该是一个更简洁的解决方案.在这里,我无法涵盖每一个细节.但肯定你会明白这个想法.如果没有,请告诉我是否需要扩展某些内容.


Any*_*are 0

我执行以下操作:

创建新页面并命名它:Counts.aspx

 protected void Page_Load(object sender, EventArgs e)
        {

                        emp_num = int.Parse(Session["empnum"].ToString());
                        Thread.Sleep(3000);
                        string res = GetCounts(emp_num);
                        Response.Write(res);

        }
        /***********************************************************************************************/
        protected string GetCounts(int empNum)
        {

            string outbox = UserTransaction.getoutboxCount(empNum, 0);
            string inbox = UserTransaction.getinboxCount(empNum, 0);
            string archive = UserTransaction.getarchivecount(empNum, 0);
            string draft = UserTransaction.getdraftcount(empNum, 0);
            return outbox + "~" + inbox + "~" + archive + "~" + draft + "~";

        }
Run Code Online (Sandbox Code Playgroud)

在我的主页中:

 <script type="text/javascript">

        function loadXMLDoc() {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            }
            else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var split = xmlhttp.responseText.split('~');

                    var outbox = split[0];
                    var inbox = split[1];
                    var archive = split[2];
                    var draft = split[3];
                    document.getElementById("lbl_DraftCount").innerHTML = draft;
                    document.getElementById("lbl_InboxCount").innerHTML = inbox;
                    document.getElementById("lbl_ApprovedCount").innerHTML = outbox;
                    document.getElementById("lbl_archive").innerHTML = archive;
                }
            }
            xmlhttp.open("GET", "Counts.aspx", true);
            xmlhttp.send();
        }
        loadXMLDoc();


    </script>
Run Code Online (Sandbox Code Playgroud)