如何提交 Sharepoint 页面并保留其状态(防止它出现 re-initializing)?

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

我有一个 Sharepoint 网页,开始时只是向用户提问;一旦他们回答了,页面的其余部分就创建好了。当用户通过 "Save" 按钮提交页面时,页面恢复到其初始(几乎空白)状态。

我正在努力解决这个问题 - 我希望页面 return 恢复到原来的 pre-submit,荣耀。

我 运行 遇到 previously-created 元素的问题,不想成为 re-created 由于新实例具有与先前实例相同的 ID("primary key violation"我估计)。

因此,在发布有关情况 之后,我认为我已经找到了解决方案,因为我添加了代码以使每个 ID 都独一无二,基于实例化发生的时间:

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);
}

这样,我就得到了元素的 ID,如下所示:

因此,在提交页面后随后创建的元素应该生成一个新的不同 ID,例如 "ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_2015072210842379boxemailsection1" 或其他任何内容而不是 "ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_2015072210754251boxemailsection1"

不过还是不行;我得到了可怕的 "Error An unexpected error has occurred." 和相当神秘的关联 ID。在日志文件中追踪它,我看到了这个特别感兴趣的条目:

07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 运行时 tkau 意外 System.Web.HttpException:加载视图状态失败。加载视图状态的控件树必须与上次请求期间用于保存视图状态的控件树相匹配。例如,动态添加控件时,在 post-back 期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。 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 ) 在 DirectPaymentSectionsWebPart.DPSVisualWe... 2150ed41-3b86-42ac-93bf-72da4e68cec3

所以它告诉我,“正在加载视图状态的控件树必须与上次请求期间用于保存视图状态的控件树相匹配...”为什么但是,它不匹配,这是我不理解的。

对于完整上下文,从关联 ID 的第一个出现运行ce(日志文件中的第 1240 行)到相同的最后一个出现运行ce(第 1254 行):

07/22/2015 09:51:37.50 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 记录相关数据 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 监控 b4ly 高离开监控范围 (GetFileAndMetaInfo)。执行时间=14.7337161566624 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 监控 b4ly 高离开监控范围 (GetWebPartPageContent)。执行时间=14.945265389875 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 记录相关数据 xmnv Medium Site=/ 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.52 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 监控 b4ly 高离开监控范围 (PostResolveRequestCacheHandler)。执行时间=19.2141040271878 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:37.65 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 监控 b4ly 高离开监控范围 (EnsureListItemsData)。执行时间=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 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 文档管理服务器文档管理 cm6w 中型模板 Discovery/InitScriptData:无法获取用户配置文件,当前上下文中不存在 UserProfileApplicationProxy 2150ed41-3b86 -42ac-93f-72da4e68cec3 07/22/2015 09:51:37.76 w3wp.exe (0x1E08) 0x1FBC 文档管理服务器文档管理 cm6w 中型模板 Discovery/InitScriptData:无法获取用户配置文件,当前上下文中不存在 UserProfileApplicationProxy 2150ed41-3b86 -42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.08 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 监控 b4ly 高离开监控范围 (EnsureListItemsData#1)。执行时间=277.14073995438 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 运行时 tkau 意外 System.Web.HttpException:无法加载视图状态。加载视图状态的控件树必须与上次请求期间用于保存视图状态的控件树相匹配。例如,动态添加控件时,在 post-back 期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配。 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 ) 在 DirectPaymentSectionsWebPart.DPSVisualWe... 2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.33 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 运行时 tkau 意外 ...bPart.DPSVisualWebPartUserControl.Page_Load(Object 发件人,EventArgs e)在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) 在 System.Web.UI.Control.OnLoad (EventArgs e) 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Control.LoadRecursive () 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)2150ed41-3b86-42ac-93bf-72da4e68cec3 07/22/2015 09:51:38.47 w3wp.exe (0x1E08) 0x1FBC SharePoint Foundation 监控 b4ly 中离开监控范围(请求(POST:https://financial.ucsc.edu:443/Pages/FinAff_Demo_Page_Clay.aspx))。执行时间=968.813145246114 2150ed41-3b86-42ac-93bf-72da4e68cec3*

当然,在提交之前保留页面的最后状态是一个常见的要求,不可能这么难。你会认为这样做的能力会通过布尔值公开,例如 "retainPriorState" 或类似的东西。或者,因为这可能不是字面上可能的,"restorePriorState"

本应简单的事情往往变得困难,hoop-jumping 编程练习让我思考网络是否真的是由国会议员和其他类型的 public 罪犯发明和设计的.

更新

我正在尝试 re-create Page_Load() 事件中的元素:

if (Page.IsPostBack) 
{

...在尝试将它们设置为 Visible 后没有起作用(因为它们被视为 null);所以它是 "Catch-22": 我无法将元素 (panels/divs) 设置为可见,因为它们在回发时为空,但我无法重新创建它们,因为...为什么?我不知道....

更新 2

可见性显然不是一个状态。当我提交 form/page 时,所有 dynamically-created 元素都可见。但是,在回传中,dynamically-created 元素中的 none 是可见的。

我想我会通过在 Load_Page() 事件中强制切换导致元素可见的单选按钮来规避这种情况,如下所示:

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

我的方法的疯狂之处在于单选按钮 (rbPaymentForSelf) 已经在回发时保留了它的选中状态,所以要触发事件 (jQuery*),我需要先将其关闭,然后重新开始。

我的想法是,如果上面的代码有效,我将根据 Session 变量的内容确定切换哪个单选按钮。但是,POC 甚至没有工作,所以它回到了编码板...

我现在会尝试禁用 ViewState...

更新 3

this.EnableViewState = false;

...没有任何改变(st运行ge(对我来说,无论如何)但是真实的)。

我在 MSDN 的博客上发现了这个问题:Why we get the exception “Failed to load viewstate”

这是摘要,以防 link 出现故障:

原因

[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.

分辨率

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.

祝你好运。