一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

Asp.net回調(diào)技術(shù)Callback學(xué)習(xí)筆記

.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>無標(biāo)題頁</title> <script type="text/Javascript"> //向服務(wù)器傳遞參數(shù) function DoSearch(){ var firstName=document.getElementById("TextBox1").value; CallServer(firstName,""); } //得到服務(wù)器的數(shù)據(jù) function ReceiveServerData(txtUserInfo){ Results.innerHTML=txtUserInfo; } //設(shè)置每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) { //獲取一個(gè)對(duì)客戶端函數(shù)的引用 string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); //動(dòng)態(tài)注冊(cè)回調(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 = "員工編號(hào):" + reader["id"].ToString() + "<br>"; txtUserInfo += "員工姓名:" + reader["name"].ToString() + "<br>"; txtUserInfo += "地址:" + reader["address"].ToString() + "<br>"; txtUserInfo += "服務(wù)器查詢時(shí)間:" + DateTime.Now.ToString(); } else { if (string.IsNullOrEmpty(txtFirstName)) { txtUserInfo = "請(qǐng)輸入姓名"; } else { txtUserInfo = "查無此人"; } } comm.Dispose(); reader.Dispose(); conn.Dispose(); } } //得到回調(diào)的結(jié)果,返回給客戶端 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>無標(biāo)題頁</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.csusing 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("員工編號(hào):" + reader["id"].ToString() + "<br>"); txtUserInfo.Append("員工姓名:" + reader["name"].ToString() + "<br>"); txtUserInfo.Append("地址:" + reader["address"].ToString() + "<br>"); txtUserInfo.Append("查詢時(shí)間:" + DateTime.Now.ToString()); } else { if (txtFirstName == string.Empty) { txtUserInfo.Append("請(qǐng)輸入姓名"); } 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>無標(biāo)題頁</title> <script type="text/Javascript"> //客戶端執(zhí)行的方法 //下面的方法是接收并處理服務(wù)器方法返回的結(jié)果 function Success(args,context){ message.innerHTML=args; } //下面的方式是當(dāng)接收服務(wù)器方法處理的結(jié)果發(fā)生異常時(shí)調(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 //實(shí)現(xiàn)ICallbackEventHandler接口 { String result = String.Empty; protected void Page_Load(object sender, EventArgs e) { //獲取當(dāng)前頁的ClientScriptManager的引用 ClientScriptManager csm = Page.ClientScript; /*獲取回調(diào)的引用.會(huì)在客戶端生成WebForm_DoCallback方法, * 調(diào)用它來達(dá)到異步調(diào)用.這個(gè)方法是微軟寫的方法,會(huì)被發(fā)送 到客戶端*/ /*注意這里的"Success"和Error兩個(gè)字符串分別是客戶端代碼中 *定義的兩個(gè)Javascript函數(shù)*/ //下面的方法最后一個(gè)參數(shù)的意義:true表示執(zhí)行異步回調(diào),false標(biāo)志執(zhí)行同步回調(diào) String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true); String callbackScript = "function CallServerMethod(args,context){/n"+ reference+";/n }"; //向當(dāng)前頁面注冊(cè)Javascript腳本代碼 csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true); } #region ICallbackEventHandler 成員 /// <summary> /// 返回回調(diào)方法執(zhí)行結(jié)果的方法 /// </summary> public string GetCallbackResult() { return result; } /// <summary> /// 在服務(wù)器端運(yùn)行回調(diào)方法 /// </summary> public void RaiseCallbackEvent(string eventArgument) { if (eventArgument.ToLower().IndexOf("admin")!=-1) { result =eventArgument+ "不能作為用戶注冊(cè)."; } else { result = eventArgument + "可以注冊(cè)."; } } #endregion }

AspNet技術(shù)Asp.net回調(diào)技術(shù)Callback學(xué)習(xí)筆記,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲第一视频在线播放 | 一区二区三区在线看 | 欧美日韩一区二区三在线 | 99热精品成人免费观看 | 美女视频永久黄网站免费观看韩国 | 91频视| 一区二区三区免费观看 | 日本人妖系列 | 五月婷婷国产 | 久99久热只有精品国产男同 | 国产一级在线观看www色 | 国产在视频线精品视频www666 | 国产成人激情 | 一级做a爰在线就看 | 美女一级毛片无遮挡内谢 | 久久精品加勒比中文字幕 | 欧美一级视频免费 | 亚洲精品69| 久久婷婷五色综合夜啪 | 国产精品视频一区二区三区经 | 97午夜影院 | 狠狠色伊人亚洲综合成人 | 中文字幕一视频97色伦 | 91国自产精品中文字幕亚洲 | 国内精品免费久久久久妲己 | 91在线精品播放 | 九九精品在线播放 | 一区二区三区在线观看免费 | 夜色精品国产一区二区 | 免费播放美女一级毛片 | 国产福利毛片 | 亚洲伊人久久一次 | 国产激情一级毛片久久久 | 在线亚洲日产一区二区 | 九九成人免费视频 | 99在线视频免费 | 美女久久 | 色哟哟视频在线观看网站 | 2021最新国产成人精品免费 | 看全色黄大色大片免费 | 国内在线精品 |