无法加载viewstate

好的,刚刚开始收到此错误,我不知道为什么。 我有一个托管页面,其中包含listview和一个带有usercontrol的面板。 列表视图使用链接按钮加载记录。 单击链接按钮以编辑该特定记录 – 它将在窗体视图(在用户控件内)中加载,并进入编辑模式。 在formview中发生更新后,我正在触发我的主机页面正在侦听的事件。 托管页面然后重新绑定列表视图以显示更新的数据。 这都在更新面板内。

所以这一切都有效 – 但是当我点击另一个链接按钮时,我得到以下错误:

Message: Sys.WebForms.PageRequestManagerServerErrorException: 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. 

有关如何修复的任何建议?

我的listview似乎正在成功反弹 – 我可以看到我更改的数据..当我点击链接按钮时,我只是不知道它为什么抱怨viewstate。 或者是否有更好的方法来更新列表视图中的数据? 我的listview和formview绑定到objectdata源(如果重要的话)

谢谢您的帮助!

这是我控制的代码隐藏。 其他人已经评论过这个错误与更改formview模式有关但是尝试了一些没有成功的更改:

 public partial class UserControls_RatesEditDate : System.Web.UI.UserControl { public delegate void EditDateRateEventHandler(DateTime theDateTime); public event EditDateRateEventHandler EditDateRateSelected; public delegate void UpdateDateRateEventHandler(); public event UpdateDateRateEventHandler EditDateRateUpdated; public int PropertyID { get; set; } public int AccommodationTypeID { get; set; } public DateTime TheDateTime { get; set; } public PropertyCMSRate Rate { get; set; } // display filters public bool DisplayMTABuy { get; set; } public bool DisplayMTASell { get; set; } public bool DisplayAffiliate { get; set; } public bool DisplayWeekly { get; set; } public bool DisplayThirtyDay { get; set; } public bool DisplayFlexi { get; set; } protected void Page_PreRender(object sender, EventArgs e) { if (Rate != null || TheDateTime != DateTime.MinValue) { if (TheDateTime == DateTime.MinValue) { frmViewRatesDate.DataSource = new List { Rate }; } else { PropertyCMSRateItemDs.SelectParameters["propertyID"].DefaultValue = PropertyID.ToString(); PropertyCMSRateItemDs.SelectParameters["accommodationTypeId"].DefaultValue = AccommodationTypeID.ToString(); PropertyCMSRateItemDs.SelectParameters["theDateTime"].DefaultValue = TheDateTime.ToString(); frmViewRatesDate.DataSourceID = "PropertyCMSRateItemDs"; frmViewRatesDate.ChangeMode(FormViewMode.Edit); } frmViewRatesDate.DataBind(); if (frmViewRatesDate.CurrentMode == FormViewMode.Edit) { ((HiddenField)frmViewRatesDate.FindControl("hdnPropertyID")).Value = PropertyID.ToString(); ((HiddenField)frmViewRatesDate.FindControl("hdnAccommTypeID")).Value = AccommodationTypeID.ToString(); } } } protected void lnkEditDate_Click(object sender, EventArgs e) { if (EditDateRateSelected != null) EditDateRateSelected(Convert.ToDateTime(((LinkButton)frmViewRatesDate.Row.FindControl("lnkEditDate")).Text)); } protected void btnUpdate_Click(object sender, EventArgs e) { if (Page.IsValid) { if (EditDateRateUpdated != null) EditDateRateUpdated(); } } protected void frmViewRatesDate_ItemCommand(object sender, FormViewCommandEventArgs e) { if (e.CommandName == "Update") { PropertyCMSRateItemDs.UpdateParameters["propertyId"].DefaultValue = ((HiddenField)frmViewRatesDate.FindControl("hdnPropertyID")).Value; PropertyCMSRateItemDs.UpdateParameters["accommodationTypeId"].DefaultValue = ((HiddenField)frmViewRatesDate.FindControl("hdnAccommTypeID")).Value; PropertyCMSRateItemDs.UpdateParameters["minStay"].DefaultValue = ((DropDownList)frmViewRatesDate.FindControl("EditPriceMinStayList")).SelectedValue; } } protected void PropertyCMSRateItemDs_Updated(object sender, ObjectDataSourceStatusEventArgs e) { if (EditDateRateUpdated != null) EditDateRateUpdated(); } } 

好的,这是我的aspx页面 – 抱歉意识到在我离开办公室抢食物后可能会有所帮助。 :)在包含该单词的listview之前发表评论..

         
  •  
  •  



Display rates Range



Show










Click on individual dates to edit
Monday Tuesday Wednesday Thursday Friday Saturday Sunday

我的用户控制:

    <asp:LinkButton ID="lnkEditDate" runat="server" Text='' OnClick="lnkEditDate_Click" /> <asp:Literal ID="Literal1" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal2" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal3" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal4" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal5" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal6" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal7" runat="server" Text='' Visible='' /> <asp:Literal ID="Literal8" runat="server" Text='' Visible='' />   
<asp:Literal ID="TheDate" runat="server" Text='' />
<asp:TextBox ID="MTABuyRate1" runat="server" Text='' />
<%----%>

我已经和这个问题争了好几个小时和几天了。 我发现的答案都没有完全解决我的问题,但我终于找到了解决方案。

我的方案如下。 我有一个GridView和一个FormView。 它们位于MultiView控件的两个视图中。 我在GridView中显示数据列表。 我的编辑按钮切换到FormView,因为我想要编辑的字段多于网格视图中的水平线(没有水平滚动)。

如果我从FormView的编辑模式取消并返回到GridView(我使用OnClick事件来执行此操作),下次发生回发时,我会收到ViewState错误。

如果我从FormView的编辑模式取消并只是切换到ReadOnly模式,然后取消返回到GridView,一切正常。

解决方案:我的解决方案是将FormView的默认模式设置为“编辑”。 我的理论是,如果FormView默认模式是“ReadOnly”,当我从FormView的编辑模式取消时,下次页面加载时,它会尝试将编辑模式的ViewState应用于ReadOnly模式(默认情况下为页面加载)。 通过将默认视图设置为“编辑”,使用编辑模式保存的ViewState将在页面加载后重新加载到新的编辑模式(默认)。

即使MultiView的当前ActiveView正在显示网格而不是表单,这一切都会发生。

我没有遇到任何ViewState问题,因为将FormView的默认模式切换为“编辑”

希望这会有所帮助,我欢迎任何评论。

谢谢。

哈夫萨瑟

在没有查看整个aspx页面的情况下很难给出确切的答案,但是当你修改使用javascript启用了viewstate的元素时,通常会发生这样的问题。 当您回发到服务器(例如,单击链接按钮)时,页面上的视图状态与服务器正在查找的视图状态不同,因此它会崩溃。 这是一项安全措施。

您是否通过更新面板使用AJAX? 您是否在更新面板中有部分页面而有些部分没有? 也许您的活动正在更新某些控件而不是其他控件。

你能提供我们可以看到的ASPX页面的注释版本吗?

这个问题也是我最近的噩梦。 有许多可能导致最终导致“无法加载视图状态”错误。 大多数情况下,它与动态创建的控件和控件的.​​Visible属性有关,这些属性可以更改添加到页面的控件的层次结构。

据我所知,问题可能围绕控件的Visible属性。 请首先尝试删除数据绑定到Visible属性,看看它是如何进行的。

         

好吧,看来我的问题与formview和更改模式有关。

我最终创建了第二个usercontrol,其中formview只有编辑项模板,我设置了默认模式进行编辑。 使用这个进行编辑,以及用于查看费率的原始文件已经摆脱了viewstate错误。

它显然不理想,因为我不应该这样做 – 但是当我试图强制它在保存更改后不切换到只读模式 – 我试过的任何东西都会阻止该视图状态错误。 已经在这个问题上花了足够长的时间 – 继续前进!! 感谢人们的建议:)

无法加载视图状态,这主要发生在使用少量ASP控件的内置命令时,这些命令通过post-back instace或Async Postback事件更改视图

示例: GridviewMultiviewFormviewListview等控件。

对于所有上述控件,事件命令如'edit' (转换为编辑模式)此时没有2事件存在,如1) 'onrowediting''onrowedited'注意第二事件不存在。 如果您在此时尝试动态添加控件,则“无法加载视图状态”发生。

解决方案 :我们需要使用像'select'这样的命令,它们有两个单独的事件,例如1) 'onselectedindexchanging'和2) 'onselectedindexchanged'

使用第二个事件我们可以添加控件并动态设置css,properties .. 这将无法加载viewstate错误。 但如果我们在第一个事件中使用它,它可能会抛出错误。