?
Solved

Get data from control state on the INIT event

Posted on 2008-10-17
1
Medium Priority
?
890 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:CMS-Team
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
JunkMan earned 1500 total points
ID: 22746205
It will not be possible to do without hacking around the framework's page cycle, and there might be implications with that.

For whatever reason you need to load your controls state on Init i would suggest you to change your logic.

If you explain me exactly why you need to do so, i'll be happy to guide you to another solution.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question