代码改变世界

Asp.net 基础6(自定义控件之ViewState)

2010-05-13 08:41  c#在路上  阅读(1164)  评论(0编辑  收藏  举报
构建自定义控件时,需要考虑周全状态管理。
是用ViewState来管理状态

能够保存在ViewState里边的包括一些标准结合类和带有Serializable 标记的类。

下面自定义控件,实现了用viewstate来保存变量。从而显示到客户端
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace OwnControl
{
    
public class BarGrphaControl:Control
    {
        
private ArrayList dataDescription;
        
private ArrayList dataValues;
        
private double max;

        
public BarGrphaControl()
        {
            dataDescription 
= new ArrayList();
            dataValues 
= new ArrayList();
        }

        
public void AddValue(string name, double value)
        {
            dataDescription.Add(name);
            dataValues.Add(value);
            
if (value > max)
                max 
= value;
        }

        
protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"5px");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            
foreach (object objStr in dataDescription)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"5px");
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                
foreach (object objValue in dataValues)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"5px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(objStr);
                    writer.RenderEndTag();
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"5px");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, 
"background-color:Red");
                    
string widthValue=string.Format("{0}px",objValue.ToString());
                    writer.AddAttribute(HtmlTextWriterAttribute.Width, widthValue);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();
                }
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }

        
protected override void LoadViewState(object savedState)
        {
            
if (savedState != null)
            {
                
object[] objStae = (object[])savedState;
                
if (objStae[0!= null)
                    
base.LoadViewState(objStae[0]);
                
if (objStae[1!= null)
                    dataDescription 
= (ArrayList)objStae[1];
                
if (objStae[2!= null)
                    dataValues 
= (ArrayList)objStae[2];
                
if (objStae[3!= null)
                    max 
= (double)objStae[3];
            }
        }

        
protected override object SaveViewState()
        {
            
object[] objRet = new object[4];
            objRet[
0= base.SaveViewState();
            objRet[
1= dataDescription;
            objRet[
2= dataValues;
            objRet[
3= max;
            
return objRet;
        }
    }
}

 

 


界面程序:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestStateViewForm.aspx.cs" Inherits="TestCodeBehind.TestStateViewForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Namespace="OwnControl" TagPrefix="useown" Assembly="OwnControl" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<useown:BarGrphaControl ID="user1" runat="server" EnableViewState="true"></useown:BarGrphaControl>
    
<asp:Button ID="btnOK" runat="server" Text="增加" onclick="btnOK_Click" />
    
</div>
    
</form>
</body>
</html>

button事件:

        
protected void btnOK_Click(object sender, EventArgs e)
        {
            
this.user1.AddValue("123",10);
            
this.user1.AddValue("456",20);
        }