callback 在asp.net應用執行個體

來源:互聯網
上載者:User

由使用者觸發

 callback的一般使用方法還算簡單,直接參照msdn的協助和範例就足夠了。但是想要真正用好、用精,或者想開發一些基於callback機制的web組件,那麼,就要先深入瞭解callback的實現機制了。在本文中,teddy將和您一起解析callback的整個調用、反饋機制,相信對於協助您更好的使用callback,將能有一定的益處
  
<body>
<form id="a1" runat=server>
<div>
<input id="txtusername" type="text" />
<input id="btncallback" type="button" value="callback" onclick="<%= clientscript.getcallbackeventreference(this,"document.getelementbyid('txtusername').value","oncallback",null) %>" />
</div>
</form>
<p id="result">
</p>
<div>
<input id="btnjtest" type="button" value="button" />
</div>
</body>
  1. 在button的 onclick 中寫入:
    onclick="<%= clientscript.getcallbackeventreference(this,"document.getelementbyid('txtusername').value","oncallback",null) %>" 
  2. 在網頁特效中編寫相應的函數
        
    function oncallbacknobtn()
    {
    callserver(document.getelementbyid('txtusername').value,"");
    }
  3. 在 xxx.asp教程x.cs 中繼承 icallbackeventhandler  並實現其方法。
        
    public partial class webpage_callbackbtn : system.web.ui.page, icallbackeventhandler
    {
    protected string struserinfo; //callback最終得到的資訊
    protected void page_load(object sender, eventargs e)
    {
    }

    #region icallbackeventhandler 成員

    public string getcallbackresult()
    {
    return struserinfo;
    }

    public void raisecallbackevent(string eventargument) //服務端的處理函數
    {
    if (eventargument == "") return;
    system.data.sqlclient.sqlconnection conn = new system.data.sqlclient.sqlconnection();
    conn.connectionstring = system.web.configuration.webconfigurationmanager.connectionstrings["nowthwindconnectionstring"].connectionstring;

    sqlcommand cmd = new sqlcommand();
    cmd.commandtype = commandtype.text;
    cmd.parameters.add("@firstname", sqldbtype.nvarchar, 10)
    .value = eventargument;
    cmd.commandtext = "select employeeid, lastname from employees where firstname=@firstname";
    cmd.connection = conn;

    sqldatareader reader;
    connectionstate previousconnectionstate = conn.state;

    try
    {
    if (conn.state == connectionstate.closed)
    {
    conn.open();
    }

    reader = cmd.executereader();

    using (reader)
    {
    while (reader.read())
    {
    // process sprocresults datareader here.
    struserinfo += reader[0];
    }
    }
    struserinfo += "###";
    }
    finally
    {
    if (previousconnectionstate == connectionstate.closed)
    {
    conn.close();
    }
    }
    }

    #endregion
    }

    raisecallbackevent() 負責接收 client 端的javascript 所傳送過來的參數,以此參數查詢資料庫教程中的資料,最後由 getcallbackresult() 將結果傳回 client端的javascript,最後將結果顯示出來。
  4. 完成  o(∩_∩)o~ 現在在 textbox 中輸入 nancy 則會顯示 1### 。如果輸入的名字在資料庫中沒有則不顯示,(我的只是完成了《聖殿祭司的asp.net教程》中的這一節的一部分)
②自動觸發。
  1. 如上 3. 在 xxx.aspx.cs 中繼承 icallbackeventhandler  並實現其方法。
  2. 在 javascript中添加
        
    <script type="text/javascript">
    function dosearch(){
    var txtfirstname= document.getelementbyid("txtusername");
    callserver(txtfirstname.value,"");
    }

    function receiveserverdata(txtuserinfo)
    {
    results.innertext=txtuserinfo;
    }

    var int=self.setinterval('dosearch()',5000);
    </script>
  3. 在 aspx.cs 中動態註冊 javascript
        
    protected void page_load(object sender, eventargs e)
    {
    string cbreference = page.clientscript.getcallbackeventreference(this,"arg", "receiveserverdata", null);
    //page.clientscript.getcallbackeventreference(this,"arg", "receiveserverdata", null);
    string callbackscript;
    callbackscript = "function callserver(arg,context){" + cbreference + "};";
    //string callbackscript = "function callserver(arg,context){webform_docallback('__page',arg,receiveserverdata,null,null,false)};";
    page.clientscript.registerclientscriptblock(this.gettype(), "callserver123", callbackscript, true);
    }
  4. 完成

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.