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>
- Write in the onclick of the button:
Onclick = "<% = clientscript. getcallbackeventreference (this," document. getelementbyid ('txtusername'). value "," oncallback ", null) %>"
- Compile corresponding functions in webpage special effects
Function oncallbacknobtn ()
{
Callserver (document. getelementbyid ('txtusername'). value ,"");
}
- 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.
- 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.
- Above 3. Inherit icallbackeventhandler in xxx. aspx. cs and implement its method.
- 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>
- 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 );
}
- Complete