使用ASP.NET 3.5 Extensions管理浏览器历史:使用服务器端

我们知道当我们使用ASP.NET AJAX的一些方便的服务端控件如UpdatePanel,我们的浏览器不能保存这些异步浏览的页面状态,而ASP.NET3.5 Extensions给我们提供了一个解决方法,下面演示一下使用实例:

一:简单示例

1. 首先下载安装ASP.NET 3.5 Extensions

2.新建一个ASP.NET3.5 Extensions Web Application

image

3. 向页面拖动一个ASP.NET3.5 Extensions下的ScriptManger和UpdatePanel

image

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

EnableHistory默认是false,要设为true, EnablestateHash就是地址栏是否加密

image

5. 修改Default.aspx.cs

image

当我们需要保存信息时,添加一个历史点,保存供还原时使用的一些信息,然后当点击后退按钮时,会执行ScriptManager_Navigate来使用我们保存的信息。

6.效果

image image

7.原理

我们看一下页面的源码,发现如果我们EnableHistory="true",会自动给我们页面添加一个Iframe,当我们后退,前进时这些还原点是更改Iframe.

image

二、分页示例:

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

image

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 getset; }
        
public int age getset; }
        
public string Address getset; }
    }

}
 

 

3.效果:

image

本文示例代码下载:http://www.cnblogs.com/Files/cnblogsfans/AjaxHistory.rar

我的这篇博客里写了使用ASP.NET 3.5 Extensions管理浏览器历史:使用客户端


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值