Use ASP. NET 3.5 Extensions to manage browser history: Use server side

Source: Internet
Author: User

We know that when we use ASP. net ajax, some convenient server-side controls such as UpdatePanel, our browser cannot save the status of these asynchronous browsing pages, whereas ASP. NET3.5 Extensions provides us with a solution. The following example shows how to use it:

I. Simple Example

1. First download and install ASP. NET 3.5 Extensions

2. Create an ASP. NET3.5 Extensions Web Application

3. Drag ScriptManger and UpdatePanel under ASP. NET3.5 Extensions to the page.

4. Modify Default. aspx. Pay attention to the yellow part.

EnableHistory is set to true by default. EnablestateHash indicates whether the address bar is encrypted.

5. Modify Default. aspx. cs

When we need to save the information, add a historical point to save some information for restoration. Then, when we click the back button, we will execute ScriptManager_Navigate to use the saved information.

6. Effect

7. Principles

Let's take a look at the page source code and find that if we EnableHistory = "true", an Iframe will be automatically added to our page. When we move back, these restore points will change Iframe.

Ii. Paging example:

1. Modify the page Default. aspx as follows:

2. Modify the page Default. aspx. cs as follows:

We add a List to provide the data source. The complete code is as follows:

Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Collections. Generic;
// Descirption: demo ajax history
// Created by: Jack Wang

Namespace AjaxHistory
{
Public partial class _ Default: System. Web. UI. Page
{
Private static string historyTime = "historyTime ";
Private List <Student> students = new List <Student> ();
Protected void Page_Load (object sender, EventArgs e)
{
For (int I = 0; I <20; I ++)
{
Students. Add (new Student {Name = "TestName" + I. ToString (), Address = "Street" + I. ToString (), age = I });
}
If (! Page. IsPostBack)
{
This. GetData ();
}
}
Protected void mGetTimeButton_Click (object sender, EventArgs e)
{
This. mResultTimeLabel. Text = DateTime. Now. ToString ();
ScriptManager. GetCurrent (this). AddHistoryPoint (historyTime, this. mResultTimeLabel. Text, DateTime. Now. Second. ToString ());
}

Protected void ScriptManager1_Navigate (object sender, HistoryEventArgs e)
{
// Restore time label
If (! String. IsNullOrEmpty (e. State [historyTime])
{
This. mResultTimeLabel. Text = e. State [historyTime]. ToString ();
}

// Restore gridview result
If (! String. IsNullOrEmpty (e. State ["gridviewResult"])
{
GridView1.PageIndex = Int32.Parse (e. State ["gridviewResult"]);
This. GetData ();
}
}

Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e. NewPageIndex;
// Just need save page index for history
ScriptManager. GetCurrent (this). AddHistoryPoint ("gridviewResult", e. NewPageIndex. ToString (), "gridviewResult" + e. NewPageIndex. ToString ());
This. GetData ();
}

Public void GetData ()
{
GridView1.DataSource = students;
GridView1.DataBind ();
}
}

Public class Student
{
Public string Name {get; set ;}
Public int age {get; set ;}
Public string Address {get; set ;}
}
}

 

3. effect:

Sample Code download: http://files.cnblogs.com/cnblogsfans/AjaxHistory.rar

In my blog, I wrote about using ASP. NET 3.5.
Extensions manages browser history: Use the client

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.