页面代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/JavaScript">
function OnCallback(txtUserInfo, context) {
document.getElementById("Results").innerText = txtUserInfo;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Client-Callback非同步数据查询</h2>
姓名:<input id="txtUserName" type="text" />
<input id="btnCallback" type="button"
value="Callback" onclick="<%= ClientScript.GetCallbackEventReference(this,"document.getElementById('txtUserName').value","OnCallback",null) %>" /><br />
<div id="Results" style="background-color: pink">
</div>
</div>
</form>
</body>
</html>
后台代码:
public partial class WebForm1 : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected string txtUserInfo; //用户基本数据
protected void Page_Load(object sender, EventArgs e)
{
}
public string GetCallbackResult()
{
return txtUserInfo;
}
public void RaiseCallbackEvent(string eventArgument)
{
if (eventArgument != null)
{
//SqlConnection conn = new SqlConnection("data source=.;initial catalog=Northwind;user id=sa;password=missoft");
//conn.Open();
//SqlCommand cmd = new SqlCommand("select EmployeeID,FirstName,City,Address from Employees where FirstName=@FirstName", conn);
//cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = eventArgument;
//SqlDataReader dr = cmd.ExecuteReader();
//if (dr.Read())
//{
// txtUserInfo = "员工编号:" + dr["EmployeeID"] + "\r\n";
// txtUserInfo += "姓名:" + dr["FirstName"] + "\r\n";
// txtUserInfo += "居住城市:" + dr["City"] + "\r\n";
// txtUserInfo += "地址:" + dr["Address"].ToString().Replace("\r\n", "") + "\r\n";
// txtUserInfo += "服务器查询时间:" + DateTime.Now.ToLongTimeString();
//}
//else
//{
if (String.IsNullOrEmpty(eventArgument))
{
txtUserInfo = "请输入姓名";
}
else
{
txtUserInfo = "查无此人!";
}
//}
}
}
}