Although the ajaxtoolkit is easy to use, there may be some problems. The problem I encountered was that the pages of the control in the ajaxtoolkit sometimes failed to be refreshed, or the buttons in the gridview on the page sometimes do not respond. Search for bugs in ajaxtoolkit on the Internet, and some people have encountered similar problems. Therefore, I decided to write my own Ajax code to achieve the refreshing effect of the page. In this process, it is important and representative to implement the refreshing page of The gridview. The following describes the implementation process.
The background code is as follows:
// To convert a gridview to HTML, you must load the following verifyrenderinginserverform. The method body is empty.
Public override void verifyrenderinginserverform (Control)
{
// Base. verifyrenderinginserverform (control );
}
Protected void page_load (Object sender, eventargs E)
{
// The first page of data in the gridview is displayed when you access the page for the first time.
If (! Ispostback & request. querystring. Count = 0)
{
Session ["page"] = "1 ";
Mysqlconnection mcon = new mysqlconnection ("Server = localhost; database = test; uid = root; Pwd = 123 ");
Mcon. open ();
Mysqlcommand mcom = new mysqlcommand ("select * from test limit" + 0 + "," + "100", mcon );
Mysqldataadapter MA = new mysqldataadapter (mcom );
Dataset DS = new dataset ();
Ma. Fill (DS );
Gridview1.datasource = Ds. Tables [0];
Databind ();
Mcon. Close ();
Mcon. Dispose ();
Mcom. Dispose ();
Ma. Dispose ();
DS. Dispose ();
}
// Determine whether the data on the new page is displayed on the next page or the previous page when sending the message back.
Else
{
If (request. querystring. Count> 0)
{
Int page = int32.parse (session ["page"]. tostring ());
If (request. querystring ["ID"]. Contains ("Next") // "Next" indicates the next page
{
Page ++;
}
Else if (request. querystring ["ID"]. Contains ("pre") // "pre" is the previous page
{
Page --;
}
Session ["page"] = page;
Int beginnum = (page-1) * 100;
Mysqlconnection mcon = new mysqlconnection ("Server = localhost; database = test; uid = root; Pwd = 123 ");
Mcon. open ();
Mysqlcommand mcom = new mysqlcommand ("select * from test limit" + beginnum + "," + "100", mcon );
Mysqldataadapter MA = new mysqldataadapter (mcom );
Dataset DS = new dataset ();
Ma. Fill (DS );
Gridview1.datasource = Ds. Tables [0];
Databind ();
// Focus on the following. The gridview uses stringwriter to output its HTML content to the HTML output stream.
System. Io. stringwriter stringwrite = new stringwriter ();
System. Web. UI. htmltextwriter writer = new system. Web. UI. htmltextwriter (stringwrite );
Gridview1.rendercontrol (writer );
String result = stringwrite. tostring ();
Response. Write (result );
Response. End (); // This field indicates that the output content ends here and the previous content is sent to the output stream. Therefore, the output result is only
// Hmtl of the gridview, if response. End () is omitted (). How to output the hmtl code of the page, and re-intercept it on the browser side
// The HTML code of the gridview is inconvenient and inefficient, because you only need to update the content of the gridview.
Databind ();
Mcon. Close ();
Mcon. Dispose ();
Mcom. Dispose ();
Ma. Dispose ();
DS. Dispose ();
}
}
}
The Ajax of the browser code is very simple. Just replace innerhtml of the Div where the gridview is located with the returned value.