English 中文(简体)
• 如何应对伙伴关系中的多重反馈。 净额4.0 C#? Example
原标题:How to handle multiple callbacks in ASP.Net 4.0 C#? Example

我不得不在纽特活动一页和名单上的其他一页上处理两个回响。

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

<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>

法典

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

    }
}

对第二次背靠背事件提出评论意见的守则是,本法典中的问题在于,即,我只能提出一次回击,有人可以告诉我,在法典上需要作哪些改动,以便可以提出两次追索事件。

最佳回答

这是个人意见的一部分:在你的C#代码中不写Script本。 这将得到<very messy and very硬拷贝。

如果你真的需要获得这一信息,你就应当使用伙伴关系。 NET Web Service(a .asmx文档)。 你们可以使用 Java或 j本或 j(我更喜欢 j。

如果我有这样的话:

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

我要说的是,使用以下法律:

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

注:success is deprecate din the recent edition of jQuery,但我无法找到在线done的良好实例。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签