// IndexButton.cs

using System;

using System.ComponentModel;

using System.Security.Permissions;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace Samples.AspNet.CS.Controls

{

    [

    AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal),

    ToolboxData("<{0}:IndexButton runat=\"server\"> </{0}:IndexButton>")

    ]

    public class IndexButton : Button

    {

        private int _index;

 

        [

        Bindable(true),

        Category("Behavior"),

        DefaultValue(0),

        Description("The index stored in control state.")

        ]

        public int Index

        {

            get

            {

                return _index;

            }

            set

            {

                _index = value;

            }

        }

 

        [

        Bindable(true),

        Category("Behavior"),

        DefaultValue(0),

        Description("The index stored in view state.")

        ]

        public int IndexInViewState

        {

            get

            {

                object obj = ViewState["IndexInViewState"];

                return (obj == null) ? 0 : (int)obj;

            }

            set

            {

                ViewState["IndexInViewState"] = value;

            }

        }

//The LoadControlState method receives an object identical to the one created in SaveControlState.  An array of objects can also be used so that every slot corresponds to a particular property.

        protected override void LoadControlState(object state)

        {

            if (state != null)

            {

                Pair p = state as Pair;

                if (p != null)

                {

                    base.LoadControlState(p.First);

                    _index = (int)p.Second;

                }

                else

                {

                    if (state is int)

                    {

                        _index = (int)state;

                    }

                    else

                    {

                        base.LoadControlState(state);

                    }

                }

            }

        }

 

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            Page.RegisterRequiresControlState(this);

        }

 

        protected override object SaveControlState()

        {

            // Invoke the base class's method and

            // get the contribution to control state

            // from the base class.

            // If the _index field in not zero

            // and the base class's control state is not null

            // use Pair as a convenient data structure

            // to efficiently save

            // (and restore in LoadControlState)

            // the two part control state.

 

            object obj = base.SaveControlState();

 

            if (_index != 0)

            {

                if (obj != null)

                {

                    return new Pair(obj, _index);

                }

                else

                {

                    return (_index);

                }

            }

            else

            {

                return obj;

            }

        }

    }

}