主頁 > 知識庫 > Asp.net回調(diào)技術Callback學習筆記

Asp.net回調(diào)技術Callback學習筆記

熱門標簽:電銷語音機器人型號參數(shù) 騰訊地圖標注手機 太原400電話上門辦理 百度地圖怎樣做地圖標注 征途美甲店地圖標注 柳州電銷機器人公司 浦發(fā)電話機器人提醒還款 400電話如何申請取消 昆明語音電銷機器人價格

.aspx:

%@ 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"> 

//向服務器傳遞參數(shù) 
function DoSearch(){ 
var firstName=document.getElementById("TextBox1").value; 
CallServer(firstName,""); 
} 

//得到服務器的數(shù)據(jù) 
function ReceiveServerData(txtUserInfo){ 
Results.innerHTML=txtUserInfo; 
} 

//設置每1秒執(zhí)行一次 
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>
[/code]
.aspx.cs
[code]
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) 
{ 
//獲取一個對客戶端函數(shù)的引用 
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); 
//動態(tài)注冊回調(diào)函數(shù) 
string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + "};"; 
//引發(fā)callbackScript 
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true); 
} 

//引發(fā)Callback事件處理 
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(); 
} 
} 

//得到回調(diào)的結果,返回給客戶端 
public string GetCallbackResult() 
{ 
return txtUserInfo; 
} 


}

簡化版(偷懶一下):

%@ 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.cs

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"> 
//客戶端執(zhí)行的方法 
//下面的方法是接收并處理服務器方法返回的結果 
function Success(args,context){ 
message.innerHTML=args; 
} 

//下面的方式是當接收服務器方法處理的結果發(fā)生異常時調(diào)用的方法 
function Error(){ 
message.innerHTML="發(fā)生了異常!"; 
} 
/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>
[code]
public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler //實現(xiàn)ICallbackEventHandler接口 
{ 

String result = String.Empty; 

protected void Page_Load(object sender, EventArgs e) 
{ 
//獲取當前頁的ClientScriptManager的引用 
ClientScriptManager csm = Page.ClientScript; 
/*獲取回調(diào)的引用.會在客戶端生成WebForm_DoCallback方法, 
* 調(diào)用它來達到異步調(diào)用.這個方法是微軟寫的方法,會被發(fā)送 
到客戶端*/ 
/*注意這里的"Success"和Error兩個字符串分別是客戶端代碼中 
*定義的兩個javascript函數(shù)*/ 
//下面的方法最后一個參數(shù)的意義:true表示執(zhí)行異步回調(diào),false標志執(zhí)行同步回調(diào) 
String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true); 
String callbackScript = "function CallServerMethod(args,context){\n"+ 
reference+";\n }"; 
//向當前頁面注冊javascript腳本代碼 
csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true); 
} 

#region ICallbackEventHandler 成員 

/// summary> 
/// 返回回調(diào)方法執(zhí)行結果的方法 
/// /summary> 
public string GetCallbackResult() 
{ 
return result; 
} 

/// summary> 
/// 在服務器端運行回調(diào)方法 
/// /summary> 
public void RaiseCallbackEvent(string eventArgument) 
{ 
if (eventArgument.ToLower().IndexOf("admin")!=-1) 
{ 
result =eventArgument+ "不能作為用戶注冊."; 
} 
else 
{ 
result = eventArgument + "可以注冊."; 
} 
} 

#endregion 
}
您可能感興趣的文章:
  • ASP.NET預備知識學習筆記
  • 那些年,我還在學習asp.net(二) 學習筆記
  • 那些年,我還在學asp.net(一) 學習筆記
  • asp.net Linq to Xml學習筆記
  • ASP.NET MVC學習筆記
  • asp.net自定義控件代碼學習筆記
  • asp.net基礎學習之前端頁面布局
  • asp.net基礎學習之控件的使用方法
  • ASP.NET與MySQL數(shù)據(jù)庫簡明圖示入門教程
  • 值得收藏的asp.net基礎學習筆記

標簽:新疆 天門 江蘇 德陽 陽泉 張家界 蘭州 白山

巨人網(wǎng)絡通訊聲明:本文標題《Asp.net回調(diào)技術Callback學習筆記》,本文關鍵詞  Asp.net,回調(diào),技術,Callback,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Asp.net回調(diào)技術Callback學習筆記》相關的同類信息!
  • 本頁收集關于Asp.net回調(diào)技術Callback學習筆記的相關信息資訊供網(wǎng)民參考!
  • 推薦文章