1

UPDATE1

I tried with EnableViewState in Page directive: but no avail.

End

I am running as a usercontrol in sharepoint. (using SmartPart) Everything's fine. Just the Viewstate-Variables!

The usercontrol (also saving the variables in viewstate) works fine in a normal aspx-site. The issue is in a sharepoint environment it won't run. I checked the "enableViewState" is set to "true". So what can be wrong?

I am trying to extract the data(storing some text like Ids) from the ViewState but it does not seems to work and it just returns me a null value.

here is what i am doing:

UserControl Implementation (VisualWebPartControl1.cs):

WebPart Implementation (VisualWebPart1.cs)

I have created a visual web part (creates usercontrol.ascx and .cs file)

public partial class VisualWebPart1UserControl : UserControl
    {
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SetId = ddl.SelectedItem.Value;
            this.ViewState[TabStorageViewStateId] = SetId; 
        }

    }

WebPart Implementation (VisualWebPart1.cs)

  public class VisualWebPart1 : WebPart
    {
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/mypath/VisualWebPart1/VisualWebPart1UserControl.ascx";

        protected override void CreateChildControls()
        {
            //generated by visual studio
            //Control control = Page.LoadControl(_ascxPath);
            //Controls.Add(control);

            var control = (VisualWebPart1UserControl) Page.LoadControl(_ascxPath);

            string retValue = this.ViewState[TabStorageViewStateId] as string;

        }
    }

3 Answers 3

1

this refers to current instance of a class. So this.ViewState refers to viewstate of the control\webpart. So, logically you are wrong here as you are trying to retrieve from the place where you never stored!

Now speaking practically, Because the ViewState property is marked as protected, you can't get a control's ViewState in another.

If you want to share data between page controls per-request basis(similar to viewstate), use HttpContext.Items instead.

0

ViewState is not loaded when CreateChildControls is called so it will not be available. Please check this article for understanding of viewstate.

2
  • i am not sure if this is true but i have msdn example which works fine with the viewstate and it does uses createchildcontrols
    – Nick Kahn
    Commented Oct 13, 2011 at 17:15
  • 1
    Looking at your code, I think you should expose a public property in your user control to expose the SetID available. its a bad practice to set a view state in one control and read it in another control. And every control will have its own view state. in your code, the view state get and set are on different controls. Commented Oct 13, 2011 at 18:12
0

By just looking at your UPDATE1 maybe this is an answer: How to enable ViewState when developing asp.net form in web part?

By default ViewState is turned off.

To enable it you need to put EnableViewState="true" in your aspx page (Page directive) and also you need to put EnableViewState="true" in desired ContentPlaceHolder eg. PlaceHolderMain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.