Implementation and Analysis of ASP. NET 2.0 client callback (2)

Source: Internet
Author: User
This program is used to read the emlpoyees information of the northwind database. Therefore, you must first ensure that the northwind database exists.
Is the content of the emlpoyees table.

Enter the username to be searched in the text box, and then click "Callback". A client callback occurs. This is the display result of the user.

Display information that does not exist for the user:

Background code: clientcallbackssimple. aspx. csusing system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;

Public partial class clientcallbackssimple: system. Web. UI. Page,
System. Web. UI. icallbackeventhandler
{
Protected string struserinfo; // Save the read user information
// Initiate a callback event
Public void raisecallbackevent (string txtfirstname)
{
If (txtfirstname! = NULL)
{
Sqlconnection conn = new sqlconnection ("Data Source = localhost; persist Security info = true; user id = sa; Pwd = sa; database = northwind ");
Conn. open ();

Sqlcommand cmd = new sqlcommand ("select employeeid, firstname, city, address from employees where firstname = @ firstname", Conn );
Cmd. Parameters. Add ("@ firstname", sqldbtype. nvarchar, 10). value = txtfirstname;
Sqldatareader DR = cmd. executereader ();

If (dr. Read ())
{
Struserinfo = "employee code:" + Dr ["employeeid"] + "\ r \ n ";
Struserinfo + = "name:" + Dr ["firstname"] + "\ r \ n ";
Struserinfo + = "City:" + Dr ["city"] + "\ r \ n ";
Struserinfo + = "Address:" + Dr ["Address"]. tostring (). Replace ("\ r \ n", "") + "\ r \ n ";
Struserinfo + = "Server Query time:" + datetime. Now. tolongtimestring ();
}
Else
{
If (string. isnullorempty (txtfirstname ))
{
Struserinfo = "enter your name ";
}
Else
{
Struserinfo = "No such person found ";
}
}
Cmd. Dispose ();
Dr. Dispose ();
Conn. Dispose ();
}
}

// Return the callback result
Public String getcallbackresult ()
{
Return struserinfo; // return basic employee information
}
}

Code Description: In the raisecallbackevent () method, an input data from the text box on the foreground page is passed as its parameter, that is, the user name to be queried from the database. Line 28-34 provides the function of reading user details and saving user information in a string struserinfo. If the user cannot be found, some error messages are returned. For details, see Code 36-45. The callback result of the getcallbackresult () method, that is, the string that saves the user information.

Front-end code: clientcallbackssimple. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "clientcallbackssimple. aspx. cs"
Inherits = "clientcallbackssimple" %>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> client callback program for reading database information </title>
<SCRIPT type = "text/JavaScript">
Function oncallback (struserinfo, context)
{
Results. innertext = struserinfo;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Name: <input id = "txtusername" type = "text"/>
<Input id = "btncallback" type = "button" value = "Callback" onclick = "<% =
Clientscript. getcallbackeventreference (this, "document.form1.txt username. Value ",
"Oncallback", null) %> "/>
<Br/>
<Div id = "Results" style = "background-color: Silver"> </div>
</Div>
</Form>
</Body>
</Html>

Code Description: The biggest difference between this program and the first program is the slight difference on the front-end page. As shown in code 17-19, the clientscript. getcallbackeventreference () method for sending callback is directly written in the Click Event of the button. This is also a feasible and simple method. Clientscript. the three parameters of the getcallbackeventreference () method are "oncallback", indicating that the callback result is returned to the oncallback () Script Function of the client after the callback is complete, the callback result struserinfo is displayed on the page as a parameter of the function, as shown in code 9. Clientscript. the four parameters of the getcallbackeventreference () method are "null", but the oncallback () Script Function still needs to retain the "context" parameter, because this is the fixed format of the client function that accepts the callback result.

  Summary:

Note that all asynchronous technologies, such as the callback client callback discussed in this article, and the new Atlas framework launched by Microsoft, do not use the traditional PostBack. Therefore, when the client presents the data returned by the server, the browser cannot see a transient green state bar, And the asynchronous process only transmits and accepts a small amount of data, instead of the entire viewstate passed in the PostBack process, the execution performance of the program is greatly improved. It is hoped that the reader can patiently understand and practice the two examples above, and can understand the essence of client callback only through his own practice.

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.