• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    Asp.net回調技術Callback學習筆記

    來源:懂視網 責編:小采 時間:2020-11-27 22:38:57
    文檔

    Asp.net回調技術Callback學習筆記

    Asp.net回調技術Callback學習筆記:.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> &l
    推薦度:
    導讀Asp.net回調技術Callback學習筆記:.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> &l

    .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"> 
    
    //向服務器傳遞參數 
    function DoSearch(){ 
    var firstName=document.getElementById("TextBox1").value; 
    CallServer(firstName,""); 
    } 
    
    //得到服務器的數據 
    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) 
    { 
    //獲取一個對客戶端函數的引用 
    string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); 
    //動態(tài)注冊回調函數 
    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(); 
    } 
    } 
    
    //得到回調的結果,返回給客戶端 
    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ā)生異常時調用的方法 
    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 //實現ICallbackEventHandler接口 
    { 
    
    String result = String.Empty; 
    
    protected void Page_Load(object sender, EventArgs e) 
    { 
    //獲取當前頁的ClientScriptManager的引用 
    ClientScriptManager csm = Page.ClientScript; 
    /*獲取回調的引用.會在客戶端生成WebForm_DoCallback方法, 
    * 調用它來達到異步調用.這個方法是微軟寫的方法,會被發(fā)送 
    到客戶端*/ 
    /*注意這里的"Success"和Error兩個字符串分別是客戶端代碼中 
    *定義的兩個javascript函數*/ 
    //下面的方法最后一個參數的意義:true表示執(zhí)行異步回調,false標志執(zhí)行同步回調 
    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> 
    /// 返回回調方法執(zhí)行結果的方法 
    /// </summary> 
    public string GetCallbackResult() 
    { 
    return result; 
    } 
    
    /// <summary> 
    /// 在服務器端運行回調方法 
    /// </summary> 
    public void RaiseCallbackEvent(string eventArgument) 
    { 
    if (eventArgument.ToLower().IndexOf("admin")!=-1) 
    { 
    result =eventArgument+ "不能作為用戶注冊."; 
    } 
    else 
    { 
    result = eventArgument + "可以注冊."; 
    } 
    } 
    
    #endregion 
    }

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    Asp.net回調技術Callback學習筆記

    Asp.net回調技術Callback學習筆記:.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> &l
    推薦度:
    標簽: 回調 callback aspn
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产乱人伦偷精品视频免下载| 在线电影国产精品| 国产精品一区二区av| 国产精品免费观看视频| 成人精品视频99在线观看免费| 69SEX久久精品国产麻豆| 国产系列高清精品第一页 | 少妇人妻偷人精品免费视频| 亚洲精品你懂的| 国产精品涩涩涩视频网站| 久久97久久97精品免视看| 免费91麻豆精品国产自产在线观看| 国产精品午夜久久| 99re热这里只有精品视频中文字幕| 国产精品成人99久久久久| 高清在线亚洲精品国产二区| 老司机亚洲精品影院无码| 亚洲精品国产福利一二区| 久热精品人妻视频| 国亚洲欧美日韩精品| 国产精品无码免费专区午夜| 亚洲天堂久久精品| 久久99国产精品99久久| 999成人精品视频在线| 久久久久久夜精品精品免费啦| AAA级久久久精品无码区| 国产精品一级片| 国产成人精品久久二区二区| WWW国产精品内射老师| 国产精品三级在线观看无码| 国产网红无码精品视频| 久久99精品久久久久久久不卡 | 久久亚洲精品成人AV| 在线涩涩免费观看国产精品 | 人妻少妇精品视中文字幕国语| 国产精品久久久久9999高清| 久久精品亚洲日本波多野结衣| 九九久久精品国产| 国产亚州精品女人久久久久久| 国产精品无码久久综合| 无码精品黑人一区二区三区|