Callback application instance in asp.net

Source: Internet
Author: User

Triggered by the user

The general usage of callback is simple. It is sufficient to directly refer to the help and example of msdn. However, if you really want to make good use of, use fine, or develop some web components based on the callback mechanism, you must first have a deep understanding of the callback implementation mechanism. In this article, teddy will work with you to parse the entire call and feedback mechanism of callback. I believe it will be helpful to help you better use 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. Write in the onclick of the button:
    Onclick = "<% = clientscript. getcallbackeventreference (this," document. getelementbyid ('txtusername'). value "," oncallback ", null) %>"
  2. Compile corresponding functions in webpage special effects
        
    Function oncallbacknobtn ()
    {
    Callserver (document. getelementbyid ('txtusername'). value ,"");
    }
  3. In xxx. asp tutorial x. cs, inherit icallbackeventhandler and implement its method.
        
    Public partial class webpage_callbackbtn: system. web. ui. page, icallbackeventhandler
    {
    Protected string struserinfo; // The final information obtained by callback
    Protected void page_load (object sender, eventargs e)
    {
    }

    # Region icallbackeventhandler member

    Public string getcallbackresult ()
    {
    Return struserinfo;
    }

    Public void raisecallbackevent (string eventargument) // The processing function of the server.
    {
    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 previusconnectionstate = conn. state;

    Try
    {
    If (conn. state = connectionstate. closed)
    {
    Conn. open ();
    }

    Reader = cmd.exe cutereader ();

    Using (reader)
    {
    While (reader. read ())
    {
    // Process sprocresults datareader here.
    Struserinfo + = reader [0];
    }
    }
    Struserinfo + = "###";
    }
    Finally
    {
    If (previusconnectionstate = connectionstate. closed)
    {
    Conn. close ();
    }
    }
    }

    # Endregion
    }

    Raisecallbackevent () is responsible for receiving the parameters transmitted by the client-side javascript, querying the data in the database tutorial using this parameter, and finally returning the results to the client-side javascript by getcallbackresult (), and finally displaying the results.
  4. Complete o (& cap; _ & cap;) o ~ If you enter nancy in textbox, 1 ### is displayed ###. If the input name is not in the database, it is not displayed. (all I have done is part of this section in asp.net tutorial of the temple priest)
② Automatic trigger.
  1. Above 3. Inherit icallbackeventhandler in xxx. aspx. cs and implement its method.
  2. Add in javascript
        
    <Script type = "text/javascript">
    Function dosearch (){
    Var txtfirstname = document. getelementbyid ("txtusername ");
    Callserver (txtfirstname. value ,"");
    }

    Function exploreserverdata (txtuserinfo)
    {
    Results. innertext = txtuserinfo;
    }

    Var int = self. setinterval ('dosearch () ', 5000 );
    </Script>
  3. Dynamically register javascript in aspx. cs
        
    Protected void page_load (object sender, eventargs e)
    {
    String cbreference = page. clientscript. getcallbackeventreference (this, "arg", "eseserverdata", null );
    // Page. clientscript. getcallbackeventreference (this, "arg", "eseserverdata", null );
    String callbackscript;
    Callbackscript = "function callserver (arg, context) {" + cbreference + "};";
    // String callbackscript = "function callserver (arg, context) {webform_docallback ('_ page', arg, eseserverdata, null, null, false )};";
    Page. clientscript. registerclientscriptblock (this. gettype (), "callserver123", callbackscript, true );
    }
  4. Complete
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.