关于asp.net2.0中缓存页面的部分数据(Request.UserHostAddress.ToString(),就是客户端的IP不同反映的欢迎信息不同)需要实时变化的问题
想在一个页面中,只把Request.UserHostAddress.ToString()实时显示,并且输出到页面上,其他的内容都缓存起来,任何浏览用户看的都是一样。
想在一个页面中,只把Request.UserHostAddress.ToString()实时显示,并且输出到页面上,其他的内容都缓存起来,任何浏览用户看的都是一样。
具体设置方法:
<%@ outputcache duration="60" varybyparam="none" %>
<script runat="server" language="C#">
void Page_Load(object sender, System.EventArgs e)
{
// Programmatically create a Substitution control.
Substitution Substitution1 = new Substitution();
// Specify the callback method.
Substitution1.MethodName = "GetCurrentDateTime";
// Add the Substitution control to the controls
// collection of PlaceHolder1.
PlaceHolder1.Controls.Add (Substitution1);
// Display the current date and time in the label.
// Output caching applies to this section of the page.
CachedDateLabel.Text = DateTime.Now.ToString();
}
// The Substitution control calls this method to retrieve
// the current date and time. This section of the page
// is exempt from output caching.
public static string GetCurrentDateTime (HttpContext context)
{
return DateTime.Now.ToString ();
}
</script>
<html>
<head id="Head1" runat="server">
<title>Substitution Class Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Substitution Constructor Example</h3>
<p>This section of the page is not cached:</p>
<asp:placeholder id="PlaceHolder1"
runat="Server">
</asp:placeholder>
<br />
<p>This section of the page is cached:</p>
<asp:label id="CachedDateLabel"
runat="Server">
</asp:label>
<br /><br />
<asp:button id="RefreshButton"
text="Refresh Page"
runat="Server">
</asp:button>
</form>
</body>
</html>
<script runat="server" language="C#">
void Page_Load(object sender, System.EventArgs e)
{
// Programmatically create a Substitution control.
Substitution Substitution1 = new Substitution();
// Specify the callback method.
Substitution1.MethodName = "GetCurrentDateTime";
// Add the Substitution control to the controls
// collection of PlaceHolder1.
PlaceHolder1.Controls.Add (Substitution1);
// Display the current date and time in the label.
// Output caching applies to this section of the page.
CachedDateLabel.Text = DateTime.Now.ToString();
}
// The Substitution control calls this method to retrieve
// the current date and time. This section of the page
// is exempt from output caching.
public static string GetCurrentDateTime (HttpContext context)
{
return DateTime.Now.ToString ();
}
</script>
<html>
<head id="Head1" runat="server">
<title>Substitution Class Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>Substitution Constructor Example</h3>
<p>This section of the page is not cached:</p>
<asp:placeholder id="PlaceHolder1"
runat="Server">
</asp:placeholder>
<br />
<p>This section of the page is cached:</p>
<asp:label id="CachedDateLabel"
runat="Server">
</asp:label>
<br /><br />
<asp:button id="RefreshButton"
text="Refresh Page"
runat="Server">
</asp:button>
</form>
</body>
</html>
转载于:https://blog.51cto.com/yaozefeng/288835