Asp.net basics 6 (ViewState of custom controls)

Source: Internet
Author: User
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 );
}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.