Comprehensive State management is required when building custom controls. ViewState is used to manage the classes whose States can be saved in ViewState, including some standard combination classes and classes with Serializable labels. The following custom control implements the use of viewstate to save variables. To display the client code 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)
{
Datadeworkflow. Add (name );
DataValues. Add (value );
If (value> max)
Max = value;
}
Protected override void Render (HtmlTextWriter writer)
{
Writer. addattriter( HtmlTextWriterAttribute. Border, "5px ");
Writer. RenderBeginTag (HtmlTextWriterTag. Table );
Foreach (object objStr in dataDescription)
{
Writer. addattriter( HtmlTextWriterAttribute. Border, "5px ");
Writer. RenderBeginTag (HtmlTextWriterTag. Tr );
Foreach (object objValue in dataValues)
{
Writer. addattriter( HtmlTextWriterAttribute. Border, "5px ");
Writer. RenderBeginTag (HtmlTextWriterTag. Td );
Writer. Write (objStr );
Writer. RenderEndTag ();
Writer. addattriter( 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)
Datadegion = (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] = dataderet;
ObjRet [2] = dataValues;
ObjRet [3] = max;
Return objRet;
}
}
}
Interface Program:
<% @ 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> No title page </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 = "added" onclick = "btnOK_Click"/>
</Div>
</Form>
</Body>
</Html>
Button event:
Protected void btnOK_Click (object sender, EventArgs e)
{
This. user1.AddValue ("123", 10 );
This. user1.AddValue ("456", 20 );
}