ASP.NET提供一个类System.Web.HttpContext ,用来表示上下文,此对象有一个属性Items
暂存状态就是利用HttpContext.Items属性来存放数据
MSDN中HttpContext.Items属性的解释是这样的:获取可用于在 HTTP 请求过程中在 IHttpModule 和 IHttpHandler 之间组织和共享数据的键值
HttpContext.Items属性中可以存放任何类型数据,无论这个属性中存放什么数据,都会在请求处理结束后自动清除,这就是暂存状态,数据的存放时间非常短.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//例如:
//我们有一页面A.ASPX,有一按钮ID:Submit,点按钮把页面转向b.aspx页面
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void Submit_Click(Object sender, EventArgs E)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
SqlConnection myConnection = new SqlConnection("server=(local)NetSDK;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");
//把数据放入暂存中
Context.Items["MyData"] =ds;
Server.Transfer(b.aspx);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//b.aspx页面中
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void Page_Load(Object sender, EventArgs E)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//取得暂存数据
DataSet ds=(DataSet)Context.Items["MyData"];
//其它数据过程
}
}
在ibuyspyportal中我们也看到了此功能的使用:
查询字符串包含正被请求的选项卡的 TabIndedx 和 TabId 参数。在处理请求的整个过程中,一直使用此信息筛选要显示给用户的数据。
http://www.ibuyspyportal.com/DesktopDefault.aspx?tabindex=1&tabid=2
要使用查询字符串值,需要首先确保它是一个有效值,如果不是,则要进行一些错误处理。它并不是一大串代码,但是您真的要在每个使用该值的页和组件中复制它吗?当然不!在 Portal 示例中,甚至更多的地方都涉及到它,因为一旦我们知道了 TabId,就可以预先加载其他信息。
Portal 使用查询字符串值作为参数,以构造一个新的 PortalSettings 对象,并将它添加到 Global.asax 的 BeginRequest 事件的 Context.Items 中。由于在每个请求开始处都执行了开始请求,这使得与该选项卡有关的值在应用程序的所有页和组件中都可用。请求完成后,对象将被自动丢弃
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Application_BeginRequest(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int tabIndex = 0;
int tabId = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 从查询字符串获取 TabIndex
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Request.Params["tabindex"] != null)
{
tabIndex = Int32.Parse(Request.Params["tabindex"]);
}
// 从查询字符串获取 TabID
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Request.Params["tabid"] != null)
{
tabId = Int32.Parse(Request.Params["tabid"]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Context.Items.Add("PortalSettings", new PortalSettings(tabIndex, tabId));
}
DesktopPortalBanner.ascx 用户控件从 Context 请求 PortalSetting 的对象,以访问 Portal 的名称和安全设置。事实上,此模块是操作中的 Context 的一个典型综合示例。为阐明这一点,我已将代码进行了一些简化,并用粗体标记了 HTTP 或应用程序特定的 Context 被访问过的所有地方。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Import Namespace="ASPNetPortal" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Import Namespace="System.Data.SqlClient" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="C#" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int tabIndex;
public bool ShowTabs = true;
protected String LogoffLink = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void Page_Load(Object sender, EventArgs e)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 从当前上下文获取 PortalSettings
PortalSettings portalSettings =
(PortalSettings) Context.Items["PortalSettings"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 动态填充门户站点名称
siteName.Text = portalSettings.PortalName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 如果用户已登录,自定义欢迎信息
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Request.IsAuthenticated == true)
{
WelcomeMessage.Text = "欢迎" +
Context.User.Identity.Name + "!<" +
"span class=Accent" + ">|<" + "/span" + ">";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 如果身份验证模式为 Cookie,则提供一个注销链接
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Context.User.Identity.AuthenticationType == "Forms")
{
LogoffLink = "<" + "span class="Accent">|</span> " +
"<a href=" + Request.ApplicationPath +
"/Admin/Logoff.aspx class=SiteLink> 注销" +
"</a>";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 动态显示门户选项卡条
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (ShowTabs == true)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tabIndex = portalSettings.ActiveTab.TabIndex;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 生成要向用户显示的选项卡列表
ArrayList authorizedTabs = new ArrayList();
int addedTabs = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i=0; i < portalSettings.DesktopTabs.Count; i++)
{
TabStripDetails tab =
(TabStripDetails)portalSettings.DesktopTabs[i];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (PortalSecurity.IsInRoles(tab.AuthorizedRoles))
{
authorizedTabs.Add(tab);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (addedTabs == tabIndex)
{
tabs.SelectedIndex = addedTabs;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
addedTabs++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 用已授权的选项卡填充页顶部的选项卡
// 列表
tabs.DataSource = authorizedTabs;
tabs.DataBind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
<table width="100%" cellspacing="0" class="HeadBg" border="0">
<tr valign="top">
<td colspan="3" align="right">
<asp:label id="WelcomeMessage" runat="server" />
<a href="<%= Request.ApplicationPath %>">Portal 主页</a>
<span class="Accent"> |</span>
<a href="<%= Request.ApplicationPath %>/Docs/Docs.htm">
Portal 文档</a>
<%= LogoffLink %>
</td>
</tr>
<tr>
<td width="10" rowspan="2">
</td>
<td height="40">
<asp:label id="siteName" runat="server" />
</td>
<td align="center" rowspan="2">
</td>
</tr>
<tr>
<td>
<asp:datalist id="tabs" runat="server">
<ItemTemplate>
<a href='<%= Request.ApplicationPath %>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/DesktopDefault.aspx?tabindex=<%
# Container.ItemIndex %>&tabid=
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# ((TabStripDetails) Container.DataItem).TabId %>'>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# ((TabStripDetails) Container.DataItem).TabName %>
</a>
</ItemTemplate>
<SelectedItemTemplate>
<span class="SelectedTab">
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# ((TabStripDetails) Container.DataItem).TabName %>
</span>
</SelectedItemTemplate>
</asp:datalist>
</td>
</tr>
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)