Enable ASP. NET ajax to support the browser's history navigation-Part 2

Source: Internet
Author: User

Previous ArticleArticle"Let ASP. net Ajax supports the history navigation-Part 1 in the browser. Previously, the history control is used, although ASP is applicable. NET 2.0 and later. net version of the web page, but must install the future suite once more, or do more reference Microsoft. web. preview. DLL actions are inconvenient. This post describes ASP. NET 3.5 SP1/Vs 2008 SP1 practices and directly references the history navigation function built in the new scriptmanager control to achieve the same effect as the previous article.

As Dino Esposito mentioned in this article:
Http://dotnetslackers.com/articles/aspnet/AFirstLookAtASPNETExtensions35HistoryPoints.aspx

On a website with vs 2008 SP1 installed (equivalent to. Net 3.5 SP1 installed), enable the built-in history navigation function of scriptmanager and create the scriptmanager1_navigate event processing function, as shown below:

Scriptmanager
< ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server " Enablehistory = " True "  
Onnavigate = " Scriptmanagerpolicnavigate " >
</ ASP: scriptmanager >

After that, you only need to perform navigate in the URL (for example, in the example of downloading this post, after the gridview in updatepanel changes pages by page number, and then click "Previous Page" in the browser ), this event is triggered.
Scriptmanager...:. navigate event:
Occurs when the user clicks the browser's back or forward button.

Next,ProgramPersonnel must create code in code-behind and add history point as appropriate. In the example of downloading this post, it is added to the history point in the gridview1_pageindexchanged event processing function. After that, when users perform URL navigate, for example, in this example, click the page number of the gridview, the new scriptmanager control will be behind the URL, add a string of seemingly garbled user States. As mentioned in the previous article of this post, the navigating history of the browser will be "serialized" and encrypted before being appended to the URL, it is used by the browser for identification.

----------------------------------------
Example of ASP. NET 3.5 + SP1 in this postCodeDownload point:
Http://files.cnblogs.com/WizardWu/081005.zip
(In this example, the northwind database of vs 2008 SP1 + SQL server is required)
----------------------------------------

This example is different from the traditional Ajax + gridview webpage:

1. When the gridview is changed, the "Previous Page and next page" button in the upper-left corner of IE (or Firefox) is automatically enabled; if it is a traditional Ajax page, it will not.

2. If you switch to the second page (or any other page) of the gridview, if you go to another page and then return to the previous page in the upper-left corner of the browser, the gridview will still stop on the second page. If it is a traditional Ajax page, it will not jump back to the first page of the gridview.

3. After the gridview is changed, if you add the page to the browser's bookmark, it will record the page in the gridview; traditional Ajax Web pages are not recorded.

4. When used with the formview and detailsview controls, when the status of these two controls is changed (insert, edit, readonly mode ), the number of pages and State can also be recorded (I did not test this version ).

5. When used with the ASP. NET wizard control, the page number and State can also be recorded (this is what Dino said, I have not tried this part of the editor ).

Default. aspx
< Form ID = "Form1" Runat = "Server" >
< Div >
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" Enablehistory = "True"  
Onnavigate = "Scriptmanagerpolicnavigate" >
</ ASP: scriptmanager >

< ASP: updatepanel ID = "Updatepanel1" Runat = "Server" >
< Contenttemplate >

< ASP: gridview ID = "Gridview1" Runat = "Server" Allowpaging = "True"  
Allowsorting = "True" Autogeneratecolumns = "False" Datakeynames = "Orderid"  
Performanceid = "Sqlperformance1" Onpageindexchanged = "Gridview1_pageindexchanged" >
< Columns >
< ASP: boundfield Datafield = "Orderid" Headertext = "Orderid" Insertvisible = "False"  
Readonly = "True" Sortexpression = "Orderid"   />
< ASP: boundfield Datafield = "Customerid" Headertext = "Customerid"  
Sortexpression = "Customerid"   />
</ Columns >
</ ASP: gridview >

< ASP: sqldatasource ID = "Sqlperformance1" Runat = "Server"  
Connectionstring = "<% $ Connectionstrings: northwindconnectionstring %>"  
Selectcommand = "Select [orderid], [customerid] from [orders]" >
</ ASP: sqldatasource >

</ Contenttemplate >
</ ASP: updatepanel >

</ Div >
</ Form >

 

Default. aspx. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;

Public Partial Class_ Default: system. Web. UI. Page
{
Private Static StringPagekey= "P";

Protected VoidPage_load (ObjectSender, eventargs E)
{}

Protected   Void Scriptmanagerpolicnavigate ( Object Sender, historyeventargs E)
{
If (E. State. Count <=   0 )
{
// Setup default state
Gridview1.pageindex =   0 ;
Return ;
}

StringKey=E. State. allkeys [0];
StringState=String. empty;

If (String. Equals (Key, pagekey ))
{
State = E. State [Key];
Int Pageindex = Int32.parse (State
Gridview1.pageindex = pageindex;
}< BR >}

Protected VoidGridview1_pageindexchanged (ObjectSender, eventargs E)
{
StringState=(SenderAsGridview). pageindex. tostring ();
Scriptmanager. getcurrent (This). Addhistorypoint (pagekey, State );
}

}

 
---------------------------------------------------------------------------------
 
Reference file:
 
 [1] msdn library, "scriptmanager. navigate event (system. Web. UI )」:
Http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.navigate.aspx
 
[2] msdn library, "scriptmanager...:. addhistorypoint method 」:
Http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.addhistorypoint.aspx
 
[3] Dino esposito, 「 A first look at ASP. NET extension3.5-history points 」:
Http://dotnetslackers.com/articles/aspnet/AFirstLookAtASPNETExtensions35HistoryPoints.aspx
 

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.