在开始之前先引进一个接口
ICallbackEventHandler:用于指示控件可以作为服务器的回调事件的目标。
它有两个公有方法 GetCallbackResult 与 RaiseCallbackEvent 分别用于返回表单处理结果 和 处理表单事件。
现在进入开始动手,本节要实现的功能是通过pubs 库中的authors 表的 au_id 字段检索 au_fname字段信息,如下:
1.新建一ASP.NET网站
2.引入接口
在默认页内添加:
<%
...
@ Implements Interface="System.Web.UI.ICallbackEventHandler"
%>
3.实现 ICallbackEventHandler 接口
如:
public
partial
class
_Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
... {
string ICallbackEventHandler.GetCallbackResult()
...{
throw new Exception("The method or operation is not implemented.");
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
...{
throw new Exception("The method or operation is not implemented.");
}
}
... {
string ICallbackEventHandler.GetCallbackResult()
...{
throw new Exception("The method or operation is not implemented.");
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
...{
throw new Exception("The method or operation is not implemented.");
}
}
用于处理客户端返回的信息与返回处理后的结果给客户端,但是这之前客户端与服务器端并不知道对方在做什么,所以这以前得交给客户端脚本处理,效验数据或状态处理。
4.添加客户端脚本
之前页面中有一呈现数据的 Table1
<
script type
=
"
text/javascript
"
>
var valueC = "" ;
function ReceiveServerData(rvalue, context)
... {
var x=document.getElementById('Table1').rows;
var y=x[1].cells;
y[0].innerHTML=valueC;
y[1].innerHTML=rvalue;
}
function SetValue()
... {
valueC=document.getElementById("Text1").value;
}
</ script >
var valueC = "" ;
function ReceiveServerData(rvalue, context)
... {
var x=document.getElementById('Table1').rows;
var y=x[1].cells;
y[0].innerHTML=valueC;
y[1].innerHTML=rvalue;
}
function SetValue()
... {
valueC=document.getElementById("Text1").value;
}
</ script >
添加一按钮和一文本
<
div
>
au_id: < input id ="Text1" type ="text" />
< input id ="Button1" type ="button" value ="查询" onclick ="SetValue();CallServer(valueC)" /></ div >
</ form >
au_id: < input id ="Text1" type ="text" />
< input id ="Button1" type ="button" value ="查询" onclick ="SetValue();CallServer(valueC)" /></ div >
</ form >
到这里我们已经建设起了一座客户端与服务器端之间的直通道路,唯一等待的是验收了。
5.注册客户端脚本
void
Page_Load(
object
sender, EventArgs e)
... {
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg",
"ReceiveServerData", "context");
String callbackScript = "function CallServer(arg, context) {" +
cbReference + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
... {
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg",
"ReceiveServerData", "context");
String callbackScript = "function CallServer(arg, context) {" +
cbReference + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
6.如何处理,为谁处理
SqlConnection conn
=
new
SqlConnection(strconn);
string value ;
public string GetCallbackResult()
... {
return value;
}
public void RaiseCallbackEvent( string eventArgument)
... {
SqlCommand comm = new SqlCommand("SELECT [au_fname] FROM [authors] WHERE ([au_id] = '" + eventArgument + "')", conn);
conn.Open();
SqlDataReader dr= comm.ExecuteReader();
if(dr.Read())
value= dr.GetValue(0).ToString();
conn.Close();
}
string value ;
public string GetCallbackResult()
... {
return value;
}
public void RaiseCallbackEvent( string eventArgument)
... {
SqlCommand comm = new SqlCommand("SELECT [au_fname] FROM [authors] WHERE ([au_id] = '" + eventArgument + "')", conn);
conn.Open();
SqlDataReader dr= comm.ExecuteReader();
if(dr.Read())
value= dr.GetValue(0).ToString();
conn.Close();
}
有你,一切皆有可能!