简体   繁体   English

如何处理ASP.Net 4.0 C#中的多个回调? 例

[英]How to handle multiple callbacks in ASP.Net 4.0 C#? Example

I have to handle two callbacks in one page one on button event and other on List. 我必须在一页中处理两个回调,一个在按钮事件上,另一个在列表中。

When button 'showDate' is clicked it display Time and when button 'btnlookup' is clicked it show the respective value of listbox item.. below is my code HTML & .cs file 当单击按钮“ showDate”时,它显示时间;当单击按钮“ btnlookup”时,它显示列表框项目的相应值。.下面是我的代码HTML和.cs文件

<head runat="server">
    <title></title>
    <script language="javascript">
        function ReceiveResults(arg, context) 
        {
            showDate.innerHTML = arg;
        }

        function LookUpStock() {
            var lb = document.getElementById("ListBox1");
            var product = lb.options[lb.selectedIndex].text;
            CallServer2(product, "");
        }

        function ReceiveServerData(rValue) {
            document.getElementById("ResultsSpan").innerHTML = rValue;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span id="showDate">aaa</span>
        </br>
        <input id="btnShowDate" type="button" value="getDate"  onclick="CallServer();"/>

    </div>

        <div>
      <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      <br />
      <br />
      <button type="btnLookUP" onclick="LookUpStock()">Look Up Stock</button>
      <br />
      <br />
      Items in stock: <span id="ResultsSpan" runat="server"></span>
      <br />
    </div>
    </form>
</body>

Code for .cs file .cs文件的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class callback : System.Web.UI.Page, ICallbackEventHandler
{
   protected String returnValue;
   protected String myDate;
   protected System.Collections.Specialized.ListDictionary catalog;


    public String GetCallbackResult()
    {
        //return returnValue;
        return myDate;
    }

    public void RaiseCallbackEvent(String eventArgument)
    {
        myDate = System.DateTime.Now.ToLongTimeString();


        //if (catalog[eventArgument] == null) 
        //{
        //    returnValue = "-1";
        //}
        //else
        //{
        //    returnValue = catalog[eventArgument].ToString();
        //}
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //For Time
        ClientScriptManager cSM = Page.ClientScript;
        String myCBRefrence = cSM.GetCallbackEventReference(this, "arg", "ReceiveResults", "context");
        cSM.RegisterClientScriptBlock(this.GetType(), "CallServer", "function CallServer(arg, context) {" + myCBRefrence + ";}", true);

        //callback back for listbox
            //String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
            //String callbackScript = "function CallServer2(arg, context){ " + cbReference + ";}";
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer2", callbackScript, true);

            //catalog = new System.Collections.Specialized.ListDictionary();
            //catalog.Add("monitor", 12);
            //catalog.Add("laptop", 10);
            //catalog.Add("keyboard", 23);
            //catalog.Add("mouse", 17);

            //ListBox1.DataSource = catalog;
            //ListBox1.DataTextField = "key";
            //ListBox1.DataBind();

    }
}

Code which is comments is for the second callback event, Problem is in this code is that i can only raise one callback, can someone tell me what changes i need to make in code so that i can raise both callback event. 注释的代码用于第二个回调事件,此代码中的问题是我只能引发一个回调,有人可以告诉我需要在代码中进行哪些更改,以便我可以引发两个回调事件。

This is a piece of personal advice: do not write JavaScript in your C# code. 这是一条个人建议:不要在C#代码中编写JavaScript。 This will get very messy and very hard to debug quickly. 这会变得非常混乱和很难快速调试。

If you really need to get this information, you should use an ASP.NET Web Service (a .asmx file). 如果确实需要获取此信息,则应使用ASP.NET Web服务(.asmx文件)。 You can call these Web Services using JavaScript or jQuery (I prefer jQuery myself). 您可以使用JavaScript或jQuery(我更喜欢jQuery)来调用这些Web服务。

If I had a .asmx file like so: 如果我有一个.asmx文件,例如:

[System.Web.Script.Services.ScriptService]
public class ServerTime : System.Web.Services.WebService
{
    [WebMethod]
    public string Get()
    {
        return System.DateTime.Now.ToLongTimeString();
    }
}

I would call that using the following jQuery code: 我会使用以下jQuery代码来称呼它:

$.ajax({ type: "POST", dataType: "json", contentType: "application/json; charset=utf-8",
    url: "ServerTime.asmx/Get",
    success: function (result) {
        $("#showDate").html(result.d);
    }
});

Note that success is deprecate din the latest version of jQuery, but I can't find a good example of done online. 请注意,在最新版本的jQuery中不推荐使用success ,但是我找不到在线done的好例子。

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

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