views:

50

answers:

2

Hi,

I have created a composite control with sample details as follows. Basically, the first time on page load the control sets a view state variable and the problem is that on post back (when the button is clicked), the ViewState variable is null. I have researched extensively and I am not able to find a solution. I checked all the Microsoft recommended articles and also from other developers. This approach seem to work for everyone and I can't figure out what I'm doing wrong. If anyone can help, I would really appreciate it.

PS: This code may not work as it is only for illustrative purposes. but this is exactly what i'm doing in my code.

Public class Test : CompositeControl
{
    private Button btnTest = new Button();
    public string TestViewState
    {
        get
        {
            string s = (string)ViewState["test"];
            return (s == null) ? String.Empty : s;
        }
        set
        {
            ViewState["test"] = value;
        }
    }

    private void set()
    {
        TestViewState = "test";
    }

    protected void Page_Load(object sender, EventArgs e)
    {  
        if(!Page.IsPostBack)
            set();  
    }

    protected override void RecreateChildControls()
    {
        EnsureChildControls();
    }

    protected override void CreateChildControls()
    {
         base.Controls.Clear();  
         btnTest.ID = "btnTest";
         btnTest.Click += new EventHandler(btnSubmitTest_Click);

         if (!ChildControlsCreated)
         Controls.Add(btnTest);      

        base.CreateChildControls();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        btnSumbit.Render(writer);
    }

    protected void btnSubmitTest_Click(object sender, EventArgs e)
    {
        string test = TestViewState; // Viewstate value is null here!!!!!!
    }
}
A: 

Are you sure that Page_Load is getting called? As far as I can remember that "notation" works only on pages and User Controls (didn't check that). Try overriding:

protected override void OnLoad(EventArgs e)
{
    ...
}

Test it with a Debugger.

Arthur
Yes, it is getting called. Actually the page_load is being called from the page where this control is. I put it in here for display purposes. I have also verified that the page_load works from within the control as well.. I made a mistake in saying that this custom control is a child of another one. Actually, this is being used straight from an aspx page.
I've Tested your code now with an ASP.NET WebSite. Page_Load has **NOT** been called, OnLoad(...) was called. When moving set() to OnLoad(...) everything works fine
Arthur
ok, it seems to work when I tried with this code, even with page load (in the actual page)..so, it must be something on my actual control... i'll look into it further. Thanks for checking that.
A: 

Ok, the enableviewstate was disabled at the web.config level by another team member. Glad I found it. Thanks Arthur for confirming it worked for you.

Oh - one of those "nice" bugs ;-)
Arthur