Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a web part with a simple asp.net/c# form. The first thing I noticed is that the form controls are not keeping their values after a postback. I thought this seemed weird since my web.config has viewstate enabled. I then tried to manually create viewstate objects to hold certain pieces of information, but this is not working either. I then enabled trace on the page, and cannot see anything about viewstate anywhere. Can someone help me with this? Does sharepoint have this turned off somewhere? Thanks.

namespace PortalAdmin.AdminTool
{
    public partial class AdminToolUserControl : UserControl
    {
        SPWeb thisWeb = SPContext.Current.Web;

        protected void Page_Load(object sender, EventArgs e)
        {
            thisWeb.AllowUnsafeUpdates = true;

            if (!Page.IsPostBack)
            {
                lblMessage.Text = "";
            }
            if (Page.IsPostBack)
            {
                if (ViewState["reportPath"] != null)
                    txtReportName.Text = ViewState["reportPath"].ToString();

                if (ViewState["action"] != null)
                {
                    switch (ViewState["action"].ToString())
                    {
                        case "preview":
                            lblMessage.Text = "Previewing report...";
                            break;
                        case "clear":
                            lblMessage.Text = "Report cleared...";
                            break;
                        case "complete":
                            lblMessage.Text = "Report added...";
                            break;
                    }
                }
            }
        }

        protected void btnAddWebPart_Click(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Site.OpenWeb("/dev/RE/West/SA/3050");
            web.AllowUnsafeUpdates = true;

            SPFile f = web.GetFile("SitePages/Page.aspx");
            SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);

            OBIEEReport report = new OBIEEReport();
            report.ReportPath = Request.QueryString["path"].ToString();
            report.Title = "REGIS Report";
            report.ChromeType = PartChromeType.None;

            wpm.AddWebPart(report, "Zone 1", 1);
            wpm.Dispose();
            web.Update();
            ViewState["action"] = "complete";
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
        }

        protected void btnClearWebPart_Click(object sender, EventArgs e)
        {
            txtReportName.Text = "";

            SPFile f = thisWeb.GetFile(HttpContext.Current.Request.Url.ToString());
            SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);

            for (int i = 0; i < wpm.WebParts.Count; i++)
            {
                if (wpm.WebParts[i].Title != "Admin Tool")
                    wpm.DeleteWebPart(wpm.WebParts[i]);
            }

            wpm.Dispose();
            thisWeb.Update();
            ViewState["action"] = "clear";
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
        }

        protected void btnPreviewWebPart_Click(object sender, EventArgs e)
        {
            SPFile f = thisWeb.GetFile(HttpContext.Current.Request.Url.ToString());
            SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);

            OBIEEReport report = new OBIEEReport();
            report.ReportPath = txtReportName.Text;
            report.Title = "REGIS Report";
            report.ChromeType = PartChromeType.None;

            if (wpm.WebParts.Count == 2)
            {
                lblMessage.Text = "You are already previewing a report";
            }
            else
            {
                wpm.AddWebPart(report, "Zone 1", 1);
                wpm.Dispose();
                thisWeb.Update();
                ViewState["action"] = "preview";
                ViewState["reportPath"] = txtReportName.Text;
                Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path));
            }
        }
    }
}
share|improve this question
    
please specify if you are working with SharePoint 2007 or SharePoint 2010 –  Anders Rask Aug 4 '10 at 18:20
    
SharePoint 2010 is what I am using –  Anonymous Aug 4 '10 at 18:23
    
At what point in the page lifecycle are you checking the contents of viewstate? When are you instatiating your controls? Perhaps post your web part code. –  Charles Lee Aug 4 '10 at 21:02
    
I just added my code above... One thing to note is that I am now doing ajax calls... So at the end of each event click, I am redirecting... I would like to keep this method though because before I was doing the ajax calls, I was having to refresh the page a second time in order to get the changes to take place from the different actions. –  Anonymous Aug 4 '10 at 21:49
    
It makes sense that the default viewstates on the controls would be wiped since I am doing a second redirect after each event click, but I figured the ViewState objects could take care of that issue. –  Anonymous Aug 4 '10 at 21:52
show 2 more comments

3 Answers 3

up vote 3 down vote accepted

EDIT: i just re-read the question and realized it was about View State, not session state! DUH! :-)

Enabling ASP.NET session state in SharePoint 2010 is done using the following PowerShell cmdlet:

Enable-SPSessionStateService -DatabaseName "ASPNet_Session_State" -databaseServer "sp2010dev.contoso.com" -SessionTimeout 30

This will add the following setting to all root web.config files (also for sites added later)

<sessionState mode="SQLServer" timeout="30" allowCustomSqlDatabase="true" sqlConnectionString="Data Source=sp2010dev.contoso.com;Initial Catalog=ASPNet_Session_State;Integrated Security=True;Enlist=False;Connect Timeout=15" />

Read more here:

http://todd-carter.com/post/2010/04/30/A-Session-State-By-Any-Other-Name.aspx

share|improve this answer
    
I am talking about viewstate, not session. –  Anonymous Aug 4 '10 at 18:34
    
Have you considered using SessionState as opposed to ViewState for your purpose? –  James Love Aug 4 '10 at 21:54
    
I actually did... And then I tried to use a session and it of course didn't work because SP2010 doesn't have it all configured yet. I could result to that if necessary, but I would like to understand what I am doing wrong with the ViewStates above. –  Anonymous Aug 4 '10 at 22:14
add comment

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.

share|improve this answer
add comment

Tried EnableViewState = true?

share|improve this answer
    
Do I place that in my masterpage? I do not want to set each controls property to EnableViewState = true. Is this what you are talking about? –  Anonymous Aug 4 '10 at 18:32
    
I tried doing Page.EnableViewState = true on page load and that doesn't work either. What am I missing? –  Anonymous Aug 4 '10 at 18:38
    
Another things is if I EnableViewState on a control (just as a test), and it also gets wiped. –  Anonymous Aug 4 '10 at 19:02
    
i am on a same situation i did tried with enableviewstate=true but does not seems hold values in the viewstate –  Abu Hamzah Oct 13 '11 at 2:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.