1、通过页面隐藏域实现:view_state与隐藏控件 。
TextBox与DropDownList不一样?
2、通过内存实现:Catch、Session、Application ,Catch是个好咚咚(据说ASP.NET提供三种主要形式的缓存:页面级输出缓存、用户控件级输出缓存(或称为片段缓存)和缓存API,前两种没有用过)!
这三个是通过Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler
3、硬盘文件数据库等
2、通过内存实现:Catch、Session、Application ,Catch是个好咚咚(据说ASP.NET提供三种主要形式的缓存:页面级输出缓存、用户控件级输出缓存(或称为片段缓存)和缓存API,前两种没有用过)!
这三个是通过Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler
3、硬盘文件数据库等
示例:
页面级缓存与用户控件缓存
<%@ OutputCache Duration="60" VaryByParam="*" %>
<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser"%>
Cache的应用(摘自MSDN)
System.Web.Caching.Cache
<%@ Import Namespace="System.IO"%>
<html>
<Script runat=server language="C#">
static bool itemRemoved = false;
static CacheItemRemovedReason reason;
CacheItemRemovedCallback onRemove = null;
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
itemRemoved = true;
reason = r;
}
public void AddItemToCache(Object sender, EventArgs e) {
itemRemoved = false;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove);
}
public void RemoveItemFromCache(Object sender, EventArgs e) {
if(Cache["Key1"] != null)
Cache.Remove("Key1");
}
</Script>
<body>
<Form runat="server">
<input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
<input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
</Form>
<% if (itemRemoved) {
Response.Write("RemovedCallback event raised.");
Response.Write("<BR>");
Response.Write("Reason: <B>" + reason.ToString() + "</B>");
}
else {
Response.Write("Value of cache key: <B>" + Cache["Key1"] + "</B>");
}
%>
</body>
</html>
ASPNET1.X动态加载
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 168px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
//private const string BTNAME = "NEWBUTTON";
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
{
//将视图状态取出,动态加载Button,放入数值
ArrayList myBTState = (ArrayList)myState[1];
for(int i=0;i< myBTState.Count ;i++)
{
TextBox tbTextBox=new TextBox();
tbTextBox.Text = myBTState[i].ToString();
Page.Controls[1].Controls.Add(tbTextBox);
}
}
}
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] allStates = new object[2];
allStates[0] = baseState;
ArrayList ar = new ArrayList();
foreach(Control myControl in Page.Controls[1].Controls)
{
if(myControl is TextBox)
{
TextBox myTextBox = (TextBox)myControl;
ar.Add(myTextBox.Text);
}
}
allStates[1] = ar;
return allStates;
}
private void Page_Load(object sender, System.EventArgs e)
{
}
Web 窗体设计器生成的代码
private void Button_Click(object sender, System.EventArgs e)
{
adduc();
}
public void adduc()
{
TextBox t=new TextBox();
//t.ID=BTNAME;
Page.Controls[1].Controls.Add(t);
}
}
}
<%@ OutputCache Duration="60" VaryByParam="*" %>
<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser"%>
Cache的应用(摘自MSDN)
System.Web.Caching.Cache
<%@ Import Namespace="System.IO"%>
<html>
<Script runat=server language="C#">
static bool itemRemoved = false;
static CacheItemRemovedReason reason;
CacheItemRemovedCallback onRemove = null;
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
itemRemoved = true;
reason = r;
}
public void AddItemToCache(Object sender, EventArgs e) {
itemRemoved = false;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove);
}
public void RemoveItemFromCache(Object sender, EventArgs e) {
if(Cache["Key1"] != null)
Cache.Remove("Key1");
}
</Script>
<body>
<Form runat="server">
<input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
<input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
</Form>
<% if (itemRemoved) {
Response.Write("RemovedCallback event raised.");
Response.Write("<BR>");
Response.Write("Reason: <B>" + reason.ToString() + "</B>");
}
else {
Response.Write("Value of cache key: <B>" + Cache["Key1"] + "</B>");
}
%>
</body>
</html>
ASPNET1.X动态加载
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 168px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
//private const string BTNAME = "NEWBUTTON";
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
{
//将视图状态取出,动态加载Button,放入数值
ArrayList myBTState = (ArrayList)myState[1];
for(int i=0;i< myBTState.Count ;i++)
{
TextBox tbTextBox=new TextBox();
tbTextBox.Text = myBTState[i].ToString();
Page.Controls[1].Controls.Add(tbTextBox);
}
}
}
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] allStates = new object[2];
allStates[0] = baseState;
ArrayList ar = new ArrayList();
foreach(Control myControl in Page.Controls[1].Controls)
{
if(myControl is TextBox)
{
TextBox myTextBox = (TextBox)myControl;
ar.Add(myTextBox.Text);
}
}
allStates[1] = ar;
return allStates;
}
private void Page_Load(object sender, System.EventArgs e)
{
}
Web 窗体设计器生成的代码
private void Button_Click(object sender, System.EventArgs e)
{
adduc();
}
public void adduc()
{
TextBox t=new TextBox();
//t.ID=BTNAME;
Page.Controls[1].Controls.Add(t);
}
}
}