asp.net控件状态使用

每次提交CustomerID都能得到保存

首先先创建一个UserControl

CutomerControl.ascx:

 

代码
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CutomerControl.ascx.cs" Inherits="WebApplication1.CutomerControl" %>
<asp:Label ID="lblCutomerName" runat="server" Text="客户ID:"></asp:Label>
<asp:TextBox ID="txtCustomerName" runat="server"></asp:TextBox>

 

 

代码
   public partial class CutomerControl : System.Web.UI.UserControl
    {
        
private int m_CustomerID;

        
public int CustomerID
        {
            
get
            {
                
return m_CustomerID;
            }

            
set
            {
                m_CustomerID 
= value;
            }
        }

        
protected void Page_Load(object sender, EventArgs e)
        {

        }

       
/// <summary>
       
/// 保存控件状态
       
/// </summary>
       
/// <returns>返回自定义控件状态和继承自UserControl基类的控件状态的数组</returns>
        protected override object SaveControlState()
        {
           
/*该页面从UserControl控件继承下来的,
            * UserControl本身会存放一些控件状态值得
            *所以我们需要保存基类里控件状态值,和我们
            *自己需要保存的控件状态值
*/

            
/*把当前基类的和自己需要保存
             * 的控件状态,保存在一个数组里
*/
            
object [] states=new object[2];

            
//基类的控件状态
            states[0= base.SaveControlState();

            
//自己的控件状态
            states[1= CustomerID;

            
return states;
        }

        
/// <summary>
        
/// 回发后加载控件状态
        
/// </summary>
        
/// <param name="savedState">保存下来的控件状态</param>
        protected override void LoadControlState(object savedState)
        {
            
//将保存下来的控件状态转换为对象数组
            object[] states = savedState as object[];

            
//加载基类的控件状态
            base.LoadControlState(states[0]);

            
//从控件状态中得到值给CustomerID赋值
            CustomerID = (int)states[1];
        }


        
/// <summary>
        
/// 告诉页面当前用户控件是需要控件状态使用的
        
/// </summary>
        
/// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            
base.OnInit(e);

            
/* 将该页面注册控件状态使用的控件,
             * 这样当页面执行的时候,就知道当前页面
               是需要使用控件状态的加载和保存的
*/
            Page.RegisterRequiresControlState(
this);
        }

 

 

测试用户控件状态页面:

WebForm1.aspx:

 

代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<%@ Register src="CutomerControl.ascx" tagname="CutomerControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
        
<uc1:CutomerControl ID="cc" runat="server" />
    
        
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="保存" />
        
<asp:Label ID="lblCurrtentCustomer" runat="server"></asp:Label>
    
    
</div>
    
</form>
</body>
</html>

 

 

代码
public partial class WebForm1 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        
}

        
protected void btnAdd_Click(object sender, EventArgs e)
        {
            lblCurrtentCustomer.Text 
= cc.CustomerID.ToString();
            cc.CustomerID
++;
        }

        
    }

 

 

  总结: 控件状态中应该保存最核心和最重要的数据,因为和视图状态一样,使用控件状态会增加网络传输的流量,如果过度的使用空间状态,而用户无法去关闭,这可能导致极度增加你网络的流量然后导致性能非常低下.
           

posted @ 2010-08-22 11:59  唔愛吃蘋果  阅读(291)  评论(0编辑  收藏  举报