下面是个例子:
public partial class atlas_CallBackByPrama : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
2{
3 public string _CallbackResult = null;
4
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 //定义客户端的参数
8 string cbReferenc = Page.ClientScript.GetCallbackEventReference(this, "arg", "getUserInfoFromServer", "context");
9
10 //定义客户端的脚本
11 string cbScript = "function UserCallBackInfo(arg,context)"
12 + "{" + cbReferenc + ";}";
13
14 //注册客户端脚本
15 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserCallBack", cbScript, true);
16
17 }
18
19
20 //查询函数,简单写了一下没有异常处理
21 private void strUserInfo(string strInVale)
22 {
23 //数据连接
24 SqlConnection myConn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=");
25 myConn.Open();
26
27 //数据库commadn
28 SqlCommand myCmd = new SqlCommand("select * from authors where au_lname like '%" + strInVale + "%'", myConn);
29
30 //声明DataReader对象
31 SqlDataReader dr = null;
32
33 //读取数据
34 dr = myCmd.ExecuteReader();
35
36 if (dr.Read())
37 {
38 _CallbackResult = dr["au_lname"].ToString() + "-" + dr["au_lname"].ToString() + " 电话:" + dr["phone"].ToString();
39 }
40
41 //关闭对象
42 dr.Close();
43
44 myConn.Close();
45 }
46
47 ICallbackEventHandler 成员#region ICallbackEventHandler 成员
48 //返回客户端结果
49 public string GetCallbackResult()
50 {
51 return _CallbackResult;
52 //throw new Exception("The method or operation is not implemented.");
53 }
54 //回应客户端事件
55 public void RaiseCallbackEvent(string eventArgument)
56 {
57 //这里是接受客户端的参数
58 strUserInfo(eventArgument);
59 //throw new Exception("The method or operation is not implemented.");
60 }
61
62 #endregion
63}
64
客户代码:
< title > 参数(CallBack)样例 </ title >
< script type ="text/javascript" >
//提取服务端值
function getUserInfo()
{
var vKey = document.form1.tbSearchKey.value;
//服务端定义的函数,这里加了参数
UserCallBackInfo(vKey,"");
}
//客户端赋值
function getUserInfoFromServer(arg,context)
{
document.form1.tbUserInfo.value = arg;
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
输入您要查询的名字,如 White、Green < br />
< asp:TextBox ID ="tbSearchKey" runat ="server" ></ asp:TextBox >
< input id ="Button1" type ="button" value ="查 询" onclick ="getUserInfo()" />< br />
查询结果是: < br />
< asp:TextBox ID ="tbUserInfo" runat ="server" Width ="278px" ></ asp:TextBox >
</ div >
</ form >
</ body >
样例代码下载: 带参样例下载