Using ASP.NET Control State

In this example we will create a Server Control and use the Control State to store and retrieve values. It is better practice to use Control State when creating custom control rather than using Page ViewState, because if Page ViewState is disabled it would break your control where as Control State cannot be disabled. This custom control is used to display an address.

Lets start by creating the Structure that we will use to store our data in


using System;

namespace ControlLibrary
{
    [Serializable]
    internal struct AddressInfo
    {
        public string Address { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string Postcode { get; set; }
    }
}

Now lets create our Server Control, you need to call the RegisterRequiresControlState method in the OnInit method to allow the control to use its Control State


using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlLibrary.Controls
{
    [DefaultProperty("Address1")]
    [ToolboxData("<{0}:AddressControl runat=server>< / {0}:AddressControl>")]
    public class AddressControl : WebControl
    {
        private AddressInfo _addressInfo;

        public AddressControl()
        {
            _addressInfo = new AddressInfo();
            _addressInfo.Address = "32 Windsor Gardens";
            _addressInfo.City = "Bromsgrove";
            _addressInfo.County = "Worcestershire";
            _addressInfo.Postcode = "B60 2";
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("32 Windsor Gardens")]
        [Localizable(true)]
        [Description("The Address property.")]
        public string Address
        {
            get { return _addressInfo.Address; }

            set { _addressInfo.Address = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Bromsgrove")]
        [Localizable(true)]
        [Description("The City property.")]
        public string City
        {
            get { return _addressInfo.City; }

            set { _addressInfo.City = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Worcestershire")]
        [Localizable(true)]
        [Description("The County property.")]
        public string County
        {
            get { return _addressInfo.County; }

            set { _addressInfo.County = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("B60 2")]
        [Localizable(true)]
        [Description("The Postcode property.")]
        public string Postcode
        {
            get { return _addressInfo.Postcode; }

            set { _addressInfo.Postcode = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }

        protected override object SaveControlState()
        {
            object state = base.SaveControlState();
            return new Pair(state, _addressInfo);
        }

        protected override void LoadControlState(object savedState)
        {
            _addressInfo = new AddressInfo();
            Pair pair = (Pair) savedState;
            _addressInfo = (AddressInfo) pair.Second;
            base.LoadControlState(pair.First);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("{0},", Address);
            builder.AppendFormat("{0},", City);
            builder.AppendFormat("{0},", County);
            builder.AppendFormat("{0}", Postcode);

            output.Write(builder.ToString());
        }
    }
}

Notice the SaveControlState and LoadControlState methods, these are used to store and retrieve the Control State.

Then we place the control on a web page, first you have to Register the control


<%@ Register TagPrefix="Ctrl" Assembly="ControlLibrary" Namespace="ControlLibrary.Controls" %>

Then you place the Control Declaration on the page




And it will Render this

32 Windsor Gardens,
Bromsgrove,
Worcestershire,
B60 2

You can download the ASP.NET Control State project here



Comments

  1. andy March 4th

    Comment Arrow

    Nice explanation.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.