使用ASP.NET 3.5 Extensions管理瀏覽器曆史:使用伺服器端

來源:互聯網
上載者:User

我們知道當我們使用ASP.NET AJAX的一些方便的服務端控制項如UpdatePanel,我們的瀏覽器不能儲存這些非同步瀏覽的頁面狀態,而ASP.NET3.5 Extensions給我們提供了一個解決方案,下面示範一下使用執行個體:

一:簡單樣本

1. 首先下載安裝ASP.NET 3.5 Extensions

2.建立一個ASP.NET3.5 Extensions Web Application

3. 向頁面拖動一個ASP.NET3.5 Extensions下的ScriptManger和UpdatePanel

4. 修改Default.aspx,注意黃色部分

EnableHistory預設是false,要設為true, EnablestateHash就是地址欄是否加密

5. 修改Default.aspx.cs

當我們需要儲存資訊時,添加一個曆史點,儲存供還原時使用的一些資訊,然後當點擊後退按鈕時,會執行ScriptManager_Navigate來使用我們儲存的資訊。

6.效果

7.原理

我們看一下頁面的源碼,發現如果我們EnableHistory="true",會自動給我們頁面添加一個Iframe,當我們後退,前進時這些還原點是更改Iframe.

二、分頁樣本:

1.修改頁面Default.aspx如下:

2. 修改頁面Default.aspx.cs如下:

我們添加一個List來提供資料來源,完整代碼如下:

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.效果:

本文範例程式碼下載:http://files.cnblogs.com/cnblogsfans/AjaxHistory.rar

我的這篇部落格裡寫了使用ASP.NET 3.5
Extensions管理瀏覽器曆史:使用用戶端

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.