Asp.net回調技術Callback學習
.aspx:javascript
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>無標題頁</title>
-
- <script type="text/javascript">
-
- //向服務器傳遞參數
- function DoSearch(){
- var firstName=document.getElementById("TextBox1").value;
- CallServer(firstName,"");
- }
-
- //獲得服務器的數據
- function ReceiveServerData(txtUserInfo){
- Results.innerHTML=txtUserInfo;
- }
-
- //設置每1秒執行一次
- setInterval("DoSearch()",1000);
- </script>
-
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <br />
- <span id="Results" style=" width:500px;"></span>
- </div>
- </form>
- </body>
- </html>
.aspx.cshtml
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Data.SqlClient;
-
- public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
- {
- protected string txtUserInfo;
-
-
- protected 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.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true);
- }
-
-
- public void RaiseCallbackEvent(string txtFirstName)
- {
- if (txtFirstName != null)
- {
- String connString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
-
- SqlConnection conn = new SqlConnection(connString);
-
- conn.Open();
-
- SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);
-
- comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;
-
- SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
- if (reader.Read())
- {
- txtUserInfo = "員工編號:" + reader["id"].ToString() + "<br>";
- txtUserInfo += "員工姓名:" + reader["name"].ToString() + "<br>";
- txtUserInfo += "地址:" + reader["address"].ToString() + "<br>";
- txtUserInfo += "服務器查詢時間:" + DateTime.Now.ToString();
- }
- else
- {
- if (string.IsNullOrEmpty(txtFirstName))
- {
- txtUserInfo = "請輸入姓名";
- }
- else
- {
- txtUserInfo = "查無此人";
- }
- }
-
- comm.Dispose();
- reader.Dispose();
- conn.Dispose();
- }
- }
-
-
- public string GetCallbackResult()
- {
- return txtUserInfo;
- }
-
-
- }
簡化版(偷懶一下):java
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>無標題頁</title>
-
- <script type="text/javascript">
- function OnCallBack(txtUserInfo,context){
- Results.innerHTML=txtUserInfo;
- }
- </script>
-
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <input id="Button2" type="button" value="button"
- onclick="<%=Page.ClientScript.GetCallbackEventReference(this, "document.getElementById('TextBox1').value", "OnCallBack",null)%>" />
- <br />
- <span id="Results" style="pink; width: 500;"></span>
- </div>
- </form>
- </body>
- </html>
.aspx.cssql
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Data.SqlClient;
- using System.Text;
- public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
- {
- protected StringBuilder txtUserInfo;
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- public string GetCallbackResult()
- {
- return txtUserInfo.ToString();
- }
-
- public void RaiseCallbackEvent(string txtFirstName)
- {
- txtUserInfo = new StringBuilder();
- String connString = ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
- SqlConnection conn = new SqlConnection(connString);
- conn.Open();
- SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);
- comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;
- SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
- if (reader.Read())
- {
- txtUserInfo.Append("員工編號:" + reader["id"].ToString() + "<br>");
- txtUserInfo.Append("員工姓名:" + reader["name"].ToString() + "<br>");
- txtUserInfo.Append("地址:" + reader["address"].ToString() + "<br>");
- txtUserInfo.Append("查詢時間:" + DateTime.Now.ToString());
- }
- else
- {
- if (txtFirstName == string.Empty)
- {
- txtUserInfo.Append("請輸入姓名");
- }
- else
- {
- txtUserInfo.Append("查無此人");
- }
- reader.Dispose();
- comm.Dispose();
- conn.Dispose();
- }
-
-
- }
- }
示例3:服務器
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>無標題頁</title>
- <script type="text/javascript">
- //客戶端執行的方法
- //下面的方法是接收並處理服務器方法返回的結果
- function Success(args,context){
- message.innerHTML=args;
- }
-
- //下面的方式是當接收服務器方法處理的結果發生異常時調用的方法
- function Error(){
- message.innerHTML="發生了異常!";
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 用戶名:<input type="text" id="txtUserName" onblur="CallServerMethod(txtUserName.value,null)" />
- <span id="message"></span>
- <br />
- 密碼:<input type="password" size="10" maxlength="20" id="txtPwd" />
- </div>
- </form>
- </body>
- </html>
- public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler
- {
-
- String result = String.Empty;
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- ClientScriptManager csm = Page.ClientScript;
-
-
-
- String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true);
- String callbackScript = "function CallServerMethod(args,context){\n"+
- reference+";\n }";
-
- csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true);
- }
-
- #region ICallbackEventHandler 成員
-
-
-
-
- public string GetCallbackResult()
- {
- return result;
- }
-
-
-
-
- public void RaiseCallbackEvent(string eventArgument)
- {
- if (eventArgument.ToLower().IndexOf("admin")!=-1)
- {
- result =eventArgument+ "不能做爲用戶註冊.";
- }
- else
- {
- result = eventArgument + "能夠註冊.";
- }
- }
-
- #endregion
- }
歡迎關注本站公眾號,獲取更多信息