1
我必須在一個頁面上處理一個按鈕事件和其他List上的兩個回調。如何在ASP.Net 4.0 C#中處理多個回調?示例
當按鈕「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>
代碼爲.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();
}
}
代碼是評論是第二個回調事件,問題是在這個代碼是我只能養一個回調,可有人告訴我什麼樣的變化,我需要的代碼,以使我可以舉起機器人h回調事件。
我同意你的觀點,但是寫小型函數的web服務會讓事情變得困難,有時我不確定這是否是好的做法。如果我想使用回調在代碼隱藏文件中調用4-5個方法,那麼該怎麼做纔是最好的方法呢?我仍在學習 。讓我改述我的問題假設如果我有一個帶有4個btn的頁面並且每個btn獲取某些信息而無需回傳應該遵循的最佳實踐 – Student
爲小函數編寫Web服務並不困難。這沒有意義。使用這種回調是一種不好的方法。 ASMX WebServices或WCF接口是您應該考慮的唯一解決方案。 – jwiscarson
同意你,所以我會將此標記爲正確答案。謝謝 – Student