页面无限跳转间如何保存页面状态 (3)

王朝other·作者佚名  2006-01-08
宽屏版  字体: |||超大  

在页面上的使用,定义好了以上这些之后,在页面中该怎样用呢?

首先,在List页面中实现IPageInfo接口:

public class RoleList : System.Web.UI.Page,IPageInfo

然后针对每一个DataGrid实例化一个DataInfo对象:

protected DataInfo dataInfo = new DataInfo("Role");

接着写一些处理DataGrid状态的代码,我是使用的属性:

#region 数据网格状态信息

private System.Collections.Hashtable SearchParams

{

get

{

if (ViewState["SearchParams"] != null)

{

return (Hashtable)ViewState["SearchParams"];

}

else

return null;

}

set

{

ViewState["SearchParams"] = value;

}

}

private System.Collections.Hashtable OtherDataParams

{

get

{

if (ViewState["OtherDataParams"] != null)

{

return (Hashtable)ViewState["OtherDataParams"];

}

else

return null;

}

set

{

ViewState["OtherDataParams"] = value;

}

}

private int CurrentPage

{

get

{

return MyDataGrid.CurrentPageIndex;

}

set

{

MyDataGrid.CurrentPageIndex = value;

MyDataGrid.DataBind();

navigateRole.CurrentPage = MyDataGrid.CurrentPageIndex + 1;

navigateRole.TotalPages = MyDataGrid.PageCount;

}

}

private string SortExpression

{

get

{

return dsSystem.Role.DefaultView.Sort;

}

set

{

dsSystem.Role.DefaultView.Sort = value;

MyDataGrid.DataBind();

navigateRole.TotalPages = MyDataGrid.PageCount;

}

}

private string ItemID

{

get

{

if (MyDataGrid.SelectedIndex >= 0)

{

return MyDataGrid.DataKeys[MyDataGrid.SelectedIndex].ToString();

}

else

return null;

}

set

{

int pageIndex = MyDataGrid.CurrentPageIndex;

bool find = false;

for( int j = 0; j < MyDataGrid.PageCount && find == false; j++)

{

MyDataGrid.CurrentPageIndex = j;

MyDataGrid.DataBind();

for(int i = 0; i < MyDataGrid.Items.Count; i++)

{

if (MyDataGrid.DataKeys[i].ToString() == value)

{

find = true;

break;

}

}

}

if (find == false)

{

MyDataGrid.CurrentPageIndex = pageIndex;

MyDataGrid.DataBind();

}

navigateRole.CurrentPage = MyDataGrid.CurrentPageIndex + 1;

navigateRole.TotalPages = MyDataGrid.PageCount;

}

}

#endregion

在PageLoad中取出前一页面的数据,进行处理,注意,从前一页面过来用的是Server.Transfer方法:

IPageInfo pageInfo = null;

//取出前一页面的信息并保存数据网格状态的信息

try

{

pageInfo = (IPageInfo)Context.Handler;

}

catch {}

if (pageInfo != null)

{

if (pageInfo.OtherParams != null)

OtherParams = pageInfo.OtherParams;

if (pageInfo.DataInfos != null)

{

//保存全部DataGrid信息

DataInfos = pageInfo.DataInfos;

//取出当前DataGrid的信息

if (pageInfo.DataInfos[dataInfo.DataName] != null)

{

dataInfo = pageInfo.DataInfos[dataInfo.DataName];

}

}

}

把数据取出来了然后自然就是处理了,我是设置前面那些属性的值的,实际上方法有很多种,这里就不详述了。

IPageInfo的实现,其中处理DataInfos属性时要更新页面上每一个DataGrid对应的DataInfo的信息,以反映最近的更改:

public string PageName

{

get

{

return "RoleList";

}

}

public Hashtable OtherParams

{

get

{

if (ViewState["OtherParams"] != null)

{

return (Hashtable)ViewState["OtherParams"];

}

else

return null;

}

set

{

ViewState["OtherParams"] = value;

}

}

public DataInfoList DataInfos

{

get

{

//更新数据网格状态信息

DataInfoList dataInfoList;

if (ViewState["DataInfos"] != null)

dataInfoList = (DataInfoList)ViewState["DataInfos"];

else

dataInfoList = new DataInfoList();

dataInfo.CurrentPage = CurrentPage;

dataInfo.ItemID = ItemID;

dataInfo.OtherParams = OtherDataParams;

dataInfo.SearchParams = SearchParams;

dataInfo.SortExpression = SortExpression;

dataInfoList[dataInfo.DataName] = dataInfo;

return dataInfoList;

}

set

{

ViewState["DataInfos"] = value;

}

}

跳转到其他页面(如详细页面):

Server.Transfer("RoleDetail.aspx");

对于Detail页面,会比较简单一些,因为基本上没有DatInfo更新的问题,旨在删除和新增时需要修改ItemID:

也是先实现接口:

public class RoleDetail : System.Web.UI.Page,IPageInfo

接着定义两个变量,一个保存前页来的数据,一个定义当前的数据类别,也就是要对哪一个DataInfo实例进行操作:

protected IPageInfo pageInfo;

protected string dataName = "Role";

PageLoad取出前页数据并处理当前项数据:

try

{

pageInfo = (IPageInfo)Context.Handler;

}

catch

{

}

//取出当前项的ID

if (pageInfo != null && pageInfo.DataInfos != null)

{

DataInfos = pageInfo.DataInfos;

}

//取详细数据

if (DataInfos != null && DataInfos[dataName].ItemID != null)

GetItemData();

else

GetNullData();

接口的实现:

public string PageName

{

get

{

return "RoleDetail";

}

}

public DataInfoList DataInfos

{

get

{

if (ViewState["DataInfos"] != null)

{

return (DataInfoList)ViewState["DataInfos"];

}

else

return null;

}

set

{

ViewState["DataInfos"] = value;

}

}

public Hashtable OtherParams

{

get

{

if (ViewState["OtherParams"] != null)

{

return (Hashtable)ViewState["OtherParams"];

}

else

return null;

}

set

{

ViewState["OtherParams"] = value;

}

}

#endregion

跳转到其他页面(如返回List):

Server.Transfer("RoleList.aspx");

这样我们需要的功能便实现了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有