简体   繁体   中英

How can I submit a Sharepoint page and retain its state (prevent it from re-initializing)?

I have a Sharepoint web page that starts off with just a question to the user; once they answer it, the rest of the page is created. When the user submits the page via a "Save" button, the page reverts to its initial (almost blank) state.

I'm trying to work around that - I want the page to return to its former, pre-submit, glory.

I ran into problems with the previously-created elements not wanting to be re-created due to the new instances having the same IDs as the previous instances (a "primary key violation" I reckon).

So after posting about the situation here , I thought I had come up with the solution when I added code to make each ID unique, based on when the instantiation takes place:

ckbxIMInsider.ID = GetYYYYMMDDHourMinSecMs() + "ckbxUCSCFacultyStaffOrStudent";

// For making IDs unique, when the same elements are created multiple times (such as before and after form submission)
private string GetYYYYMMDDHourMinSecMs()
{
    DateTime dt = DateTime.Now;
    String dateYYYYMMDD = dt.ToString("yyyyMMdd");
    int Hour = dt.Hour;
    int Min = dt.Minute;
    int Sec = dt.Second;
    int Millisec = dt.Millisecond;
    String HourMinSecMilliConcat = String.Format("{0}{1}{2}{3}", Hour, Min, Sec, Millisec);
    return String.Format("{0}{1}", dateYYYYMMDD, HourMinSecMilliConcat);
}

With that, I get IDs for elements like so:

So a subsequent creation of the elements following the submission of the page should generate a new and different ID, such as "ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_2015072210842379boxemailsection1" or whatever instead of "ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_2015072210754251boxemailsection1"

It still doesn't work, though; I get the dreaded "Error An unexpected error has occurred." with the rather cryptic Correlation ID. Tracking that down in the log file, I see this entry of particular interest:

07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Runtime tkau Unexpected System.Web.HttpException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request. at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GetSection1Table(Boolean cre8HiddenField) at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GenerateSection1(Boolean cre8HiddenField) at DirectPaymentSectionsWebPart.DPSVisualWe... 2150ed41-3b86-42ac-93bf-72da4e68cec3

So it tells me, " The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request... " Why it doesn't match, though, is what I don't grok.

For full context, from first appearance of the correlation ID (line 1240 in the log file) to the last appearance of same (line 1254):

07/22/2015 09:51:37.50 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Logging Correlation Data xmnv Medium Name=Request (POST: https://financial.ucsc.edu:443/Pages/FinAff_Demo_Page_Clay.aspx ) 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (GetFileAndMetaInfo). Execution Time=14.7337161566624 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (GetWebPartPageContent). Execution Time=14.945265389875 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Logging Correlation Data xmnv Medium Site=/ 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (PostResolveRequestCacheHandler). Execution Time=19.2141040271878 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.65 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (EnsureListItemsData). Execution Time=127.102117727253 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.66 w3wp.exe (0x1E08) 0x1FBC Web Content Management Content Deployment 78fl Unexpected SiteCacheSettings.IsImportInProgress flag is set to true, but import status has not been updated for a long time. 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.73 w3wp.exe (0x1E08) 0x1FBC Web Content Management Publishing 7fz3 Medium Setting [Display] as the FormContext.FormMode for the current page 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.73 w3wp.exe (0x1E08) 0x1FBC Web Content Management Publishing 7fz3 Medium Setting [Display] as the FormContext.FormMode for the current page 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.76 w3wp.exe (0x1E08) 0x1FBC Document Management Server Document Management cm6w Medium Template Discovery/InitScriptData: cannot get user profile, no UserProfileApplicationProxy exists in the current context 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.76 w3wp.exe (0x1E08) 0x1FBC Document Management Server Document Management cm6w Medium Template Discovery/InitScriptData: cannot get user profile, no UserProfileApplicationProxy exists in the current context 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.08 w3wp.exe (0x1E 08) 0x1FBC SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (EnsureListItemsData#1). Execution Time=277.14073995438 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Runtime tkau Unexpected System.Web.HttpException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request. at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GetSection1Table(Boolean cre8HiddenField) at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GenerateSection1(Boolean cre8HiddenField) at DirectPaymentSectionsWebPart.DPSVisualWe... 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Runtime tkau Unexpected ...bPart.DPSVisualWebPartUserControl.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Cont rol.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.47 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation Monitoring b4ly Medium Leaving Monitored Scope (Request (POST: https://financial.ucsc.edu:443/Pages/FinAff_Demo_Page_Clay.aspx )). Execution Time=968.813145246114 2150ed41-3b86-42ac-93bf-72da4e68cec3*

Surely retaining the last state of the page prior to submission is a common requirement and can't be this tough. You would think the ability to do that would be exposed through a boolean, such as "retainPriorState" or some such. Or, since that is probably not literally possible, "restorePriorState"

The way that things that should be easy are often made difficult, hoop-jumping exercises in programming cause me to ponder whether the web really was invented and designed by Congressmen and other flavors of public criminals.

UPDATE

I'm trying to re-create the elements in the Page_Load() event:

if (Page.IsPostBack) 
{

...after trying to set them to Visible there didn't work (because they are seen as null); so it is a "Catch-22": I can't set the elements (panels/divs) Visible because they are null on the postback, but I can't create them anew because...why? I don't know....

UPDATE 2

Visibleness is apparently not a state noted. When I submit my form/page, all the dynamically-created elements are visible. However, on the postback, none of the dynamically-created elements are visible.

I thought I would circumvent that by forcing the toggling of the radiobutton that causes the elements to be visiblized, in the Load_Page() event, like so:

if (IsPostBack)
{
    rbPaymentForSelf.Checked = false;
    rbPaymentForSelf.Checked = true;
}

The madness to my method is that the radio button (rbPaymentForSelf) already retains its checkedness on postback, so to get the event (jQuery*) to fire, I need to first toggle it off and then back on again.

  • Here's the jQuery I want to fire via the checking/unchecking of the radio button:

    $(document).on("click", '[id$=rbPaymentForSelf]', function () { if (this.checked) { $('[id$=panelSection2]').slideUp(); $('[id$=panelSection3]').slideUp();

      show_sections(); $('[id$=rbPaymentToIndividual]').attr('checked', true).click(); $('[id$=boxemailsection1]').val(function () { return $('[id$=hfuseremail]').val(); }) } 

    });

    function show_sections() { $('[id$=panelSection1]').slideDown(); $('[id$=panelSection4]').slideDown(); $('[id$=panelSection5]').slideDown(); $('[id$=panelSection5B]').slideDown(); $('[id$=btnSave]').slideDown(); }

My idea was that if the code above worked, I would determine which radiobutton to toggle based on the contents of a Session variable. But, the POC did not even work, so it's back to the coding board...

I will try disabling ViewState now...

UPDATE 3

this.EnableViewState = false;

...changes nothing (strange (to me, anyway) but true).

I found this issue on MSDN's Blogs: Why we get the exception “Failed to load viewstate”

Here's the summary, in case the link ever comes down:

Cause

[F]or the current POST-BACK request, when you try to add controls dynamically, the newly added control will be validated and see if it matches the control stored in the previous VIEWSTAT. If not, you will get above exception.

Resolution

Option 1:

You can disable the VIEWSTATE of this ASPX page :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" #####.aspx.cs" Inherits="#####" EnableViewState="false"%>

Option 2:

You can also change your source code to make sure the controls added during a post-back must match the type and position of the controls added during the initial request.

Fingers cross that it helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM