Link to home
Start Free TrialLog in
Avatar of CMS-Team
CMS-Team

asked on

Get data from control state on the INIT event

What should we do to get the data in a control state of a page on the Init event after a postback is done?

You must keep in mind that the method(LoadControlState) that gets the data is automatically called when that method finishes and before the Load event starts similar to what happens with the viewstate and attributes.

Any ideas?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
    string uniqueControlId = null;
 
    protected override void LoadControlState(object savedState)
    {
        if (savedState != null)
            uniqueControlId = (string)savedState;
    }
 
    protected override object SaveControlState()
    {
        return (object)uniqueControlId;
    }
 
    protected override void OnInit(EventArgs e)
    {
        try
        {
            Page.RegisterRequiresControlState(this);
 
            if (!IsPostBack)
            {
                uniqueControlId = Guid.NewGuid().ToString();
                Test test = new Test();
                test.value1 = "testData1";
                test.value2 = "testData2";
                Session["Data_" + uniqueControlId] = test;
            }
            else
            {
                //uniqueControlId is null, control state not loaded
                if (Session["Data_" + uniqueControlId] != null)
                    ((Test)Session["Data_" + uniqueControlId]).value1 += "Modified_INIT";
            }
 
            base.OnInit(e);
        }
        catch
        {
        }
    }
 
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Session["Data_" + uniqueControlId] != null)
                ((Test)Session["Data_" + uniqueControlId]).value2 += "Modified_LOAD";
        }
 
 
        //uniqueControlId success loaded
        if (Session["Data_" + uniqueControlId] != null)
        {
            Response.Write("<br /> test.value1=" + ((Test)Session["Data_" + uniqueControlId]).value1);
            Response.Write("<br /> test.value2=" + ((Test)Session["Data_" + uniqueControlId]).value2);
        }
        else
        {
            Response.Write("<br />ERROR");
        }
 
 
    }
 
    protected override void OnUnload(EventArgs e)
    {
        SaveControlState();
        base.OnUnload(e);
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
 
    }
}
 
public class Test
{
    public string value1;
    public string value2;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JunkMan
JunkMan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial