zoukankan      html  css  js  c++  java
  • Asp.net 基础6(自定义控件之ViewState)

    构建自定义控件时,需要考虑周全状态管理。
    是用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);
            }
  • 相关阅读:
    Windows下升级MySQL5.0到5.5
    聊聊MVC和模块化以及MVVM和组件化
    还有很多行业,并没有和互联网相加
    用React实现一个自动生成文章目录的组件
    一个Js开发者学习Python的第一天
    React弹窗组件
    React项目开发经验汇总
    Audio 标签的使用和自己封装一个强大的React音乐播放器
    你知道的和不知道的sass
    我眼中javascript的这些年
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1734178.html
Copyright © 2011-2022 走看看