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

时间:2011-11-23 14:47:39

标签: c# asp.net callback

我必须在一个页面上处理两个回调,一个在按钮事件上,另一个在List上。

单击按钮'showDate'时,显示Time  当点击按钮'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>

.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();

    }
}

注释的代码是针对第二个回调事件,问题在于此代码是我只能引发一个回调,有人可以告诉我需要在代码中进行哪些更改,以便我可以引发两个回调事件。 / p>

1 个答案:

答案 0 :(得分:0)

这是一条个人建议:不要在C#代码中编写JavaScript。这将使非常凌乱,非常很难快速调试。

如果确实需要获取此信息,则应使用ASP.NET Web服务(.asmx文件)。您可以使用JavaScript或jQuery调用这些Web服务(我更喜欢jQuery)。

如果我有这样的.asmx文件:

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

我会使用以下jQuery代码调用它:

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

请注意success弃用了最新版本的jQuery,但我无法在线找到done的好例子。