无法加载视图状态 [英] Failed to load viewstate

查看:407
本文介绍了无法加载视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK刚开始得到这个错误,我不知道为什么。我有ListView和一个用户控件面板托管页面。 ListView控件加载了记录有一个LinkBut​​ton。你点击链接按钮,编辑特定记录 - 转到编辑模式,获取FormView控件(UserControl中)装起来。经过FormView控件发生的更新我这触发我的托管页面被监听的事件。托管页面然后重新绑定列表视图来显示更新的数据。这是所有人的UpdatePanel内。

因此​​,这所有的工作 - 但是当我然后去点击一个不同的LinkBut​​ton我得到下面的错误:

 消息:Sys.WebForms.PageRequestManagerServerErrorException:无法加载视图状态。到被加载视图状态的控件树必须在使用了previous请求期间保存视图状态控件树相匹配。例如,动态添加控制时,一个后回过程中添加的控制必须初始请求过程中添加的控制的类型和位置相匹配。

如何解决有什么建议?

我的列表视图似乎是成功反弹 - 我可以看到我的改变的数据。我只是不知道为什么它抱怨ViewState的当我点击LinkBut​​ton的。还是有更好的方式来更新我的列表视图中的数据?我的ListView和FormView绑定到目标数据源(在这种情况下,重要的)

感谢您的帮助!

下面是我控制的codebehind。其他人都谈到这个错误的关系是不断变化的FormView的模式,但已经尝试没有成功的几个变化:

 公共部分类UserControls_RatesEditDate:System.Web.UI.UserControl
{    公共委托无效EditDateRateEventHandler(DateTime的theDateTime);
    公共事件EditDateRateEventHandler EditDateRateSelected;    公共委托无效UpdateDateRateEventHandler();
    公共事件UpdateDateRateEventHandler EditDateRateUpdated;    公众诠释物业ID {搞定;组; }
    公众诠释AccommodationTypeID {搞定;组; }
    公众的DateTime TheDateTime {搞定;组; }
    公共PropertyCMSRate率{搞定;组; }    //显示过滤器
    公共BOOL DisplayMTABuy {搞定;组; }
    公共BOOL DisplayMTASell {搞定;组; }
    公共BOOL DisplayAffiliate {搞定;组; }
    公共BOOL DisplayWeekly {搞定;组; }
    公共BOOL DisplayThirtyDay {搞定;组; }
    公共BOOL DisplayFlexi {搞定;组; }    保护无效Page_ preRender(对象发件人,EventArgs的发送)
    {
        如果(速率!= NULL || TheDateTime!= DateTime.MinValue)
        {
            如果(TheDateTime == DateTime.MinValue)
            {
                frmViewRatesDate.DataSource =新的List< PropertyCMSRate> {}率;
            }
            其他
            {
                PropertyCMSRateItemDs.SelectParameters [属性ID] =默认值PropertyID.ToString()。
                PropertyCMSRateItemDs.SelectParameters [accommodationTypeId] =默认值AccommodationTypeID.ToString()。
                PropertyCMSRateItemDs.SelectParameters [theDateTime] =默认值TheDateTime.ToString()。
                frmViewRatesDate.DataSourceID =PropertyCMSRateItemDs;                frmViewRatesDate.ChangeMode(FormViewMode.Edit);
            }            frmViewRatesDate.DataBind();            如果(frmViewRatesDate.CurrentMode == FormViewMode.Edit)
            {
                ((HiddenField)frmViewRatesDate.FindControl(hdnPropertyID))值= PropertyID.ToString();
                ((HiddenField)frmViewRatesDate.FindControl(hdnAccommTypeID))值= AccommodationTypeID.ToString();            }        }
    }    保护无效lnkEditDate_Click(对象发件人,EventArgs的发送)
    {
        如果(EditDateRateSelected!= NULL)
            EditDateRateSelected(Convert.ToDateTime(((LinkButton)frmViewRatesDate.Row.FindControl(\"lnkEditDate\")).Text));    }    保护无效btnUpdate_Click(对象发件人,EventArgs的发送)
    {        如果(Page.IsValid)
        {
            如果(EditDateRateUpdated!= NULL)
                EditDateRateUpdated();
        }    }    保护无效frmViewRatesDate_ItemCommand(对象发件人,FormViewCommandEventArgs E)
    {
        如果(e.CommandName ==更新)
        {
            PropertyCMSRateItemDs.UpdateParameters [属性ID]默认值=((HiddenField)frmViewRatesDate.FindControl(hdnPropertyID))值。;
            PropertyCMSRateItemDs.UpdateParameters [accommodationTypeId]默认值=((HiddenField)frmViewRatesDate.FindControl(hdnAccommTypeID))值。;
            PropertyCMSRateItemDs.UpdateParameters [minStay]默认值=((DropDownList的)frmViewRatesDate.FindControl(EditPriceMinStayList))的SelectedValue。;
        }
    }    保护无效PropertyCMSRateItemDs_Updated(对象发件人,ObjectDataSourceStatusEventArgs E)
    {        如果(EditDateRateUpdated!= NULL)
            EditDateRateUpdated();
    }
}

确定这里是我的aspx页面 - 对不起实现后,我离开办公室抢食物,可能会有帮助。 :)已经把注释包含这个词的列表视图我以前..

 <%@ VirtualPath的MasterType =〜/属性-CMS / AddEdit.master%GT;
<%@注册SRC =../用户控件/ RatesEditDate.ascx变量名=RatesEditDate标签preFIX =UC1%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =ContentPlaceHolder1=服务器>
    < ASP:的UpdatePanel ID =UpdatePanel1=服务器>
        <&触发器GT;
        < /触发器>
        <&的ContentTemplate GT;
                < ASP:的ValidationSummary ID =ValidationSummary1=服务器的ValidationGroup =主/>
            < D​​IV CLASS =BLUEBOX>
                < ASP:的ValidationSummary ID =ValidationSummary2=服务器的ValidationGroup =AddAccommodationType/>
                < ASP:的CustomValidator ID =DisplayPriceValidation显示=动态=服务器OnServerValidate =DisplayPrice_Validated
                                的ValidationGroup =主的ErrorMessage =请确定您已经选择了住宿,并选择有效的显示率选项>
                            < / ASP:的CustomValidator>
                < D​​IV CLASS =col33>
                    <! - 住宿 - >
                    < D​​IV CLASS =字段>
                        < ASP:标签ID =Label1的=服务器文本=选择住宿AssociatedControlID =Accommodations1/>
                        < ASP:DropDownList的ID =Accommodations1=服务器的AutoPostBack =真DataTextField =AccommodationTypeDescription
                            DataValueField =IDOnDataBound =Accommodations1_DataBoundOnSelectedIndexChanged =Accommodations1_SelectedIndexChanged
                            WIDTH =200像素的DataSourceID =AccommodationDs>
                        < / ASP:DropDownList的>
                        < ASP:ObjectDataSource控件ID =AccommodationDs=服务器OldValues​​ParameterFormatString =original_ {0}
                            SelectMethod =ListByPropertyId的TypeName =Shared.MtaLib.Bll.PropertyAccommodationController>
                            < SelectParameters>
                                < ASP:参数名称=属性ID类型=的Int32/>
                            < / SelectParameters>
                        < / ASP:ObjectDataSource控件>
                    < / DIV>
                < / DIV>
                <! - 中间一列 - >
                < D​​IV CLASS =col33>
                    < UL风格=名单样式类型:无;>
                        <立GT;
                            < ASP:标签ID =Label100=服务器文本=住宿AssociatedControlID =lblAccom/>&安培; NBSP;< ASP:标签
                                ID =lblAccom=服务器文本=/>< /李>
                        <立GT;
                            < ASP:标签ID =Label101=服务器文本=本地标签AssociatedControlID =lblLocalLabel/>&安培; NBSP;< ASP:标签
                                ID =lblLocalLabel=服务器文本=/>< /李>
                    < / UL>
                < / DIV>
                <! - 右边等级1 - >
                < D​​IV CLASS =col33>
                    < ASP:按钮=服务器ID =btnAddAccommodation文本=添加住宿的OnClick =btnAddAccommodation_Click/>< BR />
                    < BR />
                    < ASP:LinkBut​​ton的=服务器ID =lnkEditAccommodation文本=查看/编辑提供住宿
                        的OnClick =lnkEditAccommodation_Click/>
                    < BR />
                < / DIV>
                < D​​IV的风格=明确:既;>
                < / DIV>
            < / DIV>
            <! - 左手侧等级2 - >
            < ASP:面板=服务器ID =pnlViewAccommodationRatesDates>
                < D​​IV的风格=明确:既; />
                < D​​IV CLASS =盒子>
                    < D​​IV CLASS =col40>
                        显示速率范围和LT; BR />
                        < ASP:单选按钮ID =ViewRange=服务器组名=DisplayDate选中=真/>
                        < ASP:标签=服务器ID =Label5AssociatedControlID =txtDisplayFrom文本=从/>
                        < ASP:文本框ID =txtDisplayFrom=服务器>< / ASP:文本框>
                        < ASP:标签=服务器ID =Label6AssociatedControlID =txtDisplayTo文本=要/>
                        < ASP:文本框ID =txtDisplayTo=服务器>< / ASP:文本框>
                    < / DIV>
                    < D​​IV CLASS =col20>
                        < BR />
                        < ASP:单选按钮ID =ViewRange1=服务器组名=DisplayDate文本=下一个1月/>< BR />
                        < ASP:单选按钮ID =ViewRange3=服务器组名=DisplayDate文本=下3个月/>< BR />
                        < ASP:单选按钮ID =ViewRange6=服务器组名=DisplayDate文本=后6月/>
                    < / DIV>
                    < D​​IV CLASS =col20>
                        展会< BR />
                        < ASP:复选框ID =chkDisplayMTABuy=服务器文本=MTA买入选中=真/>< BR />
                        < ASP:复选框ID =chkDisplayMTASell=服务器文本=MTA卖出选中=真/>< BR />
                        < ASP:复选框ID =chkDisplayAffiliateNett=服务器文本=加盟实价选中=真/>< BR />
                        < ASP:复选框ID =chkDisplayWeekly=服务器文本=周末画报选中=真/>< BR />
                        < ASP:复选框ID =chkDisplay30Day=服务器文本=30日选中=真/>< BR />
                        < ASP:复选框ID =chkDisplayFlexi=服务器文本=灵活选中=真/>
                    < / DIV>
                    < D​​IV CLASS =col20>
                        < BR />
                        < BR />
                        < BR />
                        < ASP:按钮的ID =btnDisplayRates=服务器文本=显示价格的OnClick =btnDisplayRates_Click/>
                        < BR />
                        < BR />
                        < ASP:LinkBut​​ton的ID =lnkAddEditRate=服务器文本=添加/编辑汇率的OnClick =lnkAddEditRate_Click/>
                    < / DIV>
                    < D​​IV的风格=明确:既; />
                < / DIV>
            < / ASP:面板>
            <! - 隐藏面板除去我出的字符 - >
            <! - 包含用户控制列表视图..用户控件已经与一个LinkBut​​ton FormView控件 - >
            < ASP:面板=服务器ID =pnlViewAccommodationRates可见=假>
                < D​​IV CLASS =propertyaddedit_ratesbox>
                    <&字段集GT;
                        <传奇>点击个别日期编辑< /传说>
                        < ASP:面板ID =Panel5=服务器滚动条=垂直HEIGHT =400>
                            < ASP:ListView控件ID =PropertyAccommodationRates1=服务器的DataSourceID =PropertyCMSRateListDs
                                ItemPlaceholderID =itemsGoHereOnItemDataBound =PropertyAccommodationRates1_ItemDataBound
                                GroupItemCount =7GroupPlaceholderID =groupsGoHere>
                                < GroupTemplate>
                                    &所述; TR>
                                        < ASP:占位符=服务器ID =itemsGoHere/>
                                    < / TR>
                                < / GroupTemplate>
                                <&ItemTemplate中GT;
                                    &所述; TD>
                                        < UC1:RatesEditDate ID =RatesViewDate1=服务器OnEditDateRateSelected =RatesEditDate1_EditDateRateSelected
                                            OnEditDateRateUpdated =RatesEditDate1_EditDateRateUpdated/>
                                    < / TD>
                                < / ItemTemplate中>
                                <&LayoutTemplate模板GT;
                                    <表格的宽度=525pxCELLSPACING =2>
                                        &所述; TR类=propertyaddedit_gridViewRates>
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期一
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期二
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期三
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期四
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期五
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期六
                                            < /第i
                                            百分位类=propertyaddedit_ratesGrid>
                                                星期日
                                            < /第i
                                        < / TR>
                                        &所述; TR>
                                            < ASP:占位符=服务器ID =groupsGoHere/>
                                        < / TR>
                                    < /表>
                                < / LayoutTemplate模板>
                            < / ASP:的ListView>
                            < ASP:ObjectDataSource控件ID =PropertyCMSRateListDs=服务器SelectMethod =ListPropertyCMSRates
                                类型名=Shared.MtaLib.Bll.PropertyRateControllerOnSelecting =PropertyCMSRateListDs_Selecting>
                                < SelectParameters>
                                    < ASP:参数名称=属性ID类型=的Int32/>
                                    < ASP:参数名称=accommodationTypeIdTYPE =的Int32/>
                                    < ASP:参数名称=minDateTime类型=日期时间/>
                                    < ASP:参数名称=maxDateTime类型=日期时间/>
                                    < ASP:参数名称=fillMondays类型=布尔默认值=真/>
                                < / SelectParameters>
                            < / ASP:ObjectDataSource控件>
                        < / ASP:面板>
                    < /字段集>
                < / DIV>
            < / ASP:面板>            < D​​IV的风格=明确:既;>
            < / DIV>
            <! - 这就是编辑发生用户控件 - >
            < D​​IV =服务器ID =divEditRate可见=真>
                < UC1:RatesEditDate ID =RatesEditDate1=服务器/>
            < / DIV>
        < /&的ContentTemplate GT;
    < / ASP:的UpdatePanel>
< / ASP:内容>

我的用户控件:

 <%@控制语言=C#AutoEventWireup =真codeFILE =RatesEditDate.ascx.cs
    继承=UserControls_RatesEditDate%GT;
< ASP:FormView控件ID =frmViewRatesDate=服务器HEIGHT =50像素WIDTH =125pxOnItemCommand =frmViewRatesDate_ItemCommand
    的DataKeyNames =TheDateTime>
    <&ItemTemplate中GT;
        < ASP:LinkBut​​ton的ID =lnkEditDate=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,TheDateTime,{0:DD / MM / YYYY})%GT;'
            的OnClick =lnkEditDate_Click/>
        < ASP:文字ID =Literal1=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,MTABuyRateDaily,{0:N})%GT;'
            可见='<%#DisplayMTABuy%GT;' />
        < ASP:文字ID =Literal2=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,MTASellRateDaily,{0:N})%GT;'
            可见='<%#DisplayMTASell%GT;' />
        < ASP:文字ID =Literal3=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,AffiliateNettDaily,{0:N})%GT;'
            可见='<%#DisplayAffiliate%GT;' />
        < ASP:文字ID =Literal4=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,MTABuyRateWeekly,{0:N})%GT;'
            可见='<%#DisplayWeekly%GT;' />
        < ASP:文字ID =Literal5=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,MTASellRateWeekly,{0:N})%GT;'
            可见='<%#DisplayWeekly%GT;' />
        < ASP:文字ID =Literal6=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,AffiliateNettWeekly,{0:N})%GT;'
            可见='<%#DisplayWeekly%GT;' />
        < ASP:文字ID =Literal7=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,ThirtyDay,{0:N})%GT;'
            可见='<%#DisplayThirtyDay%GT;' />
        < ASP:文字ID =Literal8=服务器文本='<%#DataBinder.Eval的(的Container.DataItem,灵活,{0:N})%GT;'
            可见='<%#DisplayFlexi%GT;' />
    < / ItemTemplate中>
    <&EditItemTemplate的GT;
        < D​​IV CLASS =字段>
            < ASP:文字ID =TheDate=服务器文本='<%#绑定(TheDateTime,{0:DD / MM / YYYY})%GT;' />
        < / DIV>
        < D​​IV CLASS =字段>
            < ASP:标签ID =Label1的文本=MTA购买率AssociatedControlID =MTABuyRate1=服务器/>< ASP:文本框
                ID =MTABuyRate1=服务器文本='<%#绑定(MTABuyRateDaily,{0:N})%GT;' />
            < ASP:CompareValidator ID =MTABuyRateValidator=服务器TYPE =货币的ControlToValidate =MTABuyRate1
                操作=大于ValueToCompare =0的ErrorMessage =显示=动态的ValidationGroup =EditPrice>
                < ASP:图片ID =图片8SkinID =ValidationErrorImage=服务器/>
            < / ASP:CompareValidator>
            < ASP:的RequiredFieldValidator ID =MTABuyRateValidator2=服务器的ControlToValidate =MTABuyRate1
                的ErrorMessage =显示=动态的ValidationGroup =EditPrice>
                < ASP:图片ID =Image33SkinID =ValidationErrorImage=服务器/>
            < / ASP:&的RequiredFieldValidator GT;
        < / DIV>
        <! - 类似的项目删除,由于字符限制 - >
        < D​​IV CLASS =字段>
            < ASP:标签ID =Label6文本=总PAX=服务器AssociatedControlID =TotalPax1/>
            < ASP:CompareValidator ID =TotalPaxValidator1=服务器的ControlToValidate =TotalPax1
                操作=NotEqualValueToCompare =0的ErrorMessage =请选择一个非零总大同
                显示=动态的ValidationGroup =EditPrice>
                < ASP:图片ID =Image15SkinID =ValidationErrorImage=服务器/>
            < / ASP:CompareValidator>
            < ASP:DropDownList的ID =TotalPax1=服务器>
                < ASP:ListItem的值=0/>
                < ASP:ListItem的值=1/>
                < ASP:ListItem的值=2/>
                < ASP:ListItem的值=3/>
                < ASP:ListItem的值=4/>
                < ASP:ListItem的值=5/>
                < ASP:ListItem的值=6/>
                < ASP:ListItem的值=7/>
                < ASP:ListItem的值=8/>
                < ASP:ListItem的值=9/>
                < ASP:ListItem的值=10/>
            < / ASP:DropDownList的>
        < / DIV>
        < D​​IV CLASS =字段>
            < ASP:标签=服务器ID =Label15AssociatedControlID =EditPriceMinStayList
                文本=闽逗留(晚)/>
            < ASP:CompareValidator ID =EditPriceMinStayListValidator1=服务器的ControlToValidate =EditPriceMinStayList
                操作=NotEqualValueToCompare =0的ErrorMessage =请选择一个非零闽停留
                显示=动态的ValidationGroup =EditPrice>
                < ASP:图片ID =图片6SkinID =ValidationErrorImage=服务器/>
            < / ASP:CompareValidator>
            <! - TODO绑定 - >
            < ASP:DropDownList的ID =EditPriceMinStayList=服务器>
                < ASP:ListItem的值=0/>
                < ASP:ListItem的值=1/>
                < ASP:ListItem的值=2/>
                < ASP:ListItem的值=3/>
                < ASP:ListItem的值=4/>
                < ASP:ListItem的值=5/>
                < ASP:ListItem的值=6/>
                < ASP:ListItem的值=7/>
                < ASP:ListItem的值=8/>
                < ASP:ListItem的值=9/>
                < ASP:ListItem的值=10/>
                < ASP:ListItem的值=11/>
                < ASP:ListItem的值=12/>
                < ASP:ListItem的值=13/>
                < ASP:ListItem的值=14/>
                < ASP:ListItem的值=15/>
                < ASP:ListItem的值=16/>
                < ASP:ListItem的值=17/>
                < ASP:ListItem的值=18/>
                < ASP:ListItem的值=19/>
                < ASP:ListItem的值=20/>
                < ASP:ListItem的值=21/>
                < ASP:ListItem的值=22/>
                < ASP:ListItem的值=23/>
                < ASP:ListItem的值=24/>
                < ASP:ListItem的值=25/>
                < ASP:ListItem的值=26/>
                < ASP:ListItem的值=27/>
                < ASP:ListItem的值=28/>
                < ASP:ListItem的值=29/>
                < ASP:ListItem的值=30/>
            < / ASP:DropDownList的>
        < / DIV>
        < D​​IV CLASS =字段>
            <% - < ASP:按钮的ID =btnUpdate=服务器文本=更新的OnClick =btnUpdate_Click的CommandName =更新/> - %GT;
            < ASP:按钮的ID =btnUpdate=服务器文本=更新的CommandName =更新的ValidationGroup =EditPrice/>
        < / DIV>
        < ASP:HiddenField =服务器ID =hdnPropertyID/>
        < ASP:HiddenField =服务器ID =hdnAccommTypeID/>
    < / EditItemTemplate中>
< / ASP:FormView控件>
< ASP:ObjectDataSource控件ID =PropertyCMSRateItemDs=服务器SelectMethod =ListPropertyCMSRate
    类型名=Shared.MtaLib.Bll.PropertyRateControllerUpdateMethod =UpdatePropertyCMSRate
    OldValues​​ParameterFormatString =original_ {0}OnUpdated =PropertyCMSRateItemDs_Updated>
    < UpdateParameters>
        < ASP:参数名称=属性ID类型=的Int32/>
        < ASP:参数名称=accommodationTypeIdTYPE =的Int32/>
        < ASP:参数名称=minStayTYPE =的Int32/>
        < ASP:参数名称=TheDateTime类型=日期时间/>
        < ASP:参数名称=MTABuyRateDailyTYPE =十进制/>
        < ASP:参数名称=MTASellRateDailyTYPE =十进制/>
        < ASP:参数名称=AffiliateNettDailyTYPE =十进制/>
        < ASP:参数名称=MTABuyRateWeeklyTYPE =十进制/>
        < ASP:参数名称=MTASellRateWeeklyTYPE =十进制/>
        < ASP:参数名称=AffiliateNettWeeklyTYPE =十进制/>
        < ASP:参数名称=ThirtyDayTYPE =十进制/>
        < ASP:参数名称=灵活类型=十进制/>
        < ASP:参数名称=IsBreakfastIncluded类型=布尔/>
        < ASP:参数名称=IsFairWarning类型=布尔/>
        < ASP:参数名称=TotalPaxTYPE =的Int32/>
        < ASP:参数名称=original_TheDateTime类型=日期时间/>
    < / UpdateParameters>
    < SelectParameters>
        < ASP:参数名称=属性IDTYPE =的Int32默认值=0/>
        < ASP:参数名称=accommodationTypeIdTYPE =的Int32默认值=0/>
        < ASP:参数名称=theDateTime类型=日期时间/>
    < / SelectParameters>
< / ASP:ObjectDataSource控件>


解决方案

那么它出现在我的问题是有关FormView控件和不断变化的模式。

我结束了创建一个FormView控件,只有具有编辑项目模板的第二个用户控件,我设置的默认模式进行编辑。使用这一个编辑,和原来的观看率已经摆脱了视图状态的错误。

这显然不是理想的,因为我不应该这样做 - 但同时我试图迫使它保存更改后不切换到只读模式 - 没有什么我试图将prevent的视图状态错误。花了足够长的时间在这个问题上已经 - 离开的时候了!感谢您的建议,虽然人:)

OK have just started getting this error and I'm not sure why. I have a hosting page which has listview and a panel with a usercontrol. The listview loads up records with a linkbutton. You click the link button to edit that particular record - which gets loaded up in the formview (within the usercontrol) which goes to edit mode. After an update occurs in the formview I'm triggering an event which my hosting page is listening for. The hosting page then rebinds the listview to show the updated data. This is all inside an updatepanel.

So this all works - but when I then go to click on a different linkbutton I get the below error:

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.

Any suggestions on how to fix?

My listview appears to be being rebound successfully - I can see my changed data.. I just don't know why its complaining about viewstate when I click on the linkbutton. Or is there a better way to update the data in my listview? My listview and formview are bound to objectdata sources (in case that matters)

Thanks for the help!

Here is the codebehind of my control. Other people have commented on this error being in relation to changing the mode of formview but have tried a few changes without success:

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<PropertyCMSRate> { 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();
    }
}

OK here is my aspx page - sorry realised that might help after I left the office to grab food. :) Have put a comment before my listview containing that word..

<%@ MasterType VirtualPath="~/Properties-Cms/AddEdit.master" %>
<%@ Register Src="../UserControls/RatesEditDate.ascx" TagName="RatesEditDate" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
        </Triggers>
        <ContentTemplate>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Main" />
            <div class="bluebox">
                <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="AddAccommodationType" />
                <asp:CustomValidator ID="DisplayPriceValidation" Display="Dynamic" runat="server" OnServerValidate="DisplayPrice_Validated"
                                ValidationGroup="Main" ErrorMessage="Please ensure you have selected Accommodation and selected a valid display rates option">
                            </asp:CustomValidator>
                <div class="col33">
                    <!-- accommodation -->
                    <div class="field">
                        <asp:Label ID="Label1" runat="server" Text="Select Accommodation" AssociatedControlID="Accommodations1" />
                        <asp:DropDownList ID="Accommodations1" runat="server" AutoPostBack="true" DataTextField="AccommodationTypeDescription"
                            DataValueField="Id" OnDataBound="Accommodations1_DataBound" OnSelectedIndexChanged="Accommodations1_SelectedIndexChanged"
                            Width="200px" DataSourceID="AccommodationDs">
                        </asp:DropDownList>
                        <asp:ObjectDataSource ID="AccommodationDs" runat="server" OldValuesParameterFormatString="original_{0}"
                            SelectMethod="ListByPropertyId" TypeName="Shared.MtaLib.Bll.PropertyAccommodationController">
                            <SelectParameters>
                                <asp:Parameter Name="PropertyId" Type="Int32" />
                            </SelectParameters>
                        </asp:ObjectDataSource>
                    </div>
                </div>
                <!-- middle column -->
                <div class="col33">
                    <ul style="list-style-type: none;">
                        <li>
                            <asp:Label ID="Label100" runat="server" Text="Accommodation" AssociatedControlID="lblAccom" />&nbsp;<asp:Label
                                ID="lblAccom" runat="server" Text="" /></li>
                        <li>
                            <asp:Label ID="Label101" runat="server" Text="Local Label" AssociatedControlID="lblLocalLabel" />&nbsp;<asp:Label
                                ID="lblLocalLabel" runat="server" Text="" /></li>
                    </ul>
                </div>
                <!--Right hand side Level 1 -->
                <div class="col33">
                    <asp:Button runat="server" ID="btnAddAccommodation" Text="Add Accommodation" OnClick="btnAddAccommodation_Click" /><br />
                    <br />
                    <asp:LinkButton runat="server" ID="lnkEditAccommodation" Text="View/Edit available Accommodations"
                        OnClick="lnkEditAccommodation_Click" />
                    <br />
                </div>
                <div style="clear: both;">
                </div>
            </div>
            <!--Left hand side Level 2 -->
            <asp:Panel runat="server" ID="pnlViewAccommodationRatesDates">
                <div style="clear: both;" />
                <div class="box">
                    <div class="col40">
                        Display rates Range<br />
                        <asp:RadioButton ID="ViewRange" runat="server" GroupName="DisplayDate" Checked="true" />
                        <asp:Label runat="server" ID="Label5" AssociatedControlID="txtDisplayFrom" Text="From" />
                        <asp:TextBox ID="txtDisplayFrom" runat="server"></asp:TextBox>
                        <asp:Label runat="server" ID="Label6" AssociatedControlID="txtDisplayTo" Text="To" />
                        <asp:TextBox ID="txtDisplayTo" runat="server"></asp:TextBox>
                    </div>
                    <div class="col20">
                        <br />
                        <asp:RadioButton ID="ViewRange1" runat="server" GroupName="DisplayDate" Text="Next 1 month" /><br />
                        <asp:RadioButton ID="ViewRange3" runat="server" GroupName="DisplayDate" Text="Next 3 month" /><br />
                        <asp:RadioButton ID="ViewRange6" runat="server" GroupName="DisplayDate" Text="Next 6 month" />
                    </div>
                    <div class="col20">
                        Show<br />
                        <asp:CheckBox ID="chkDisplayMTABuy" runat="server" Text="MTA Buy" Checked="true" /><br />
                        <asp:CheckBox ID="chkDisplayMTASell" runat="server" Text="MTA Sell" Checked="true" /><br />
                        <asp:CheckBox ID="chkDisplayAffiliateNett" runat="server" Text="Affiliate Nett" Checked="true" /><br />
                        <asp:CheckBox ID="chkDisplayWeekly" runat="server" Text="Weekly" Checked="true" /><br />
                        <asp:CheckBox ID="chkDisplay30Day" runat="server" Text="30 Day" Checked="true" /><br />
                        <asp:CheckBox ID="chkDisplayFlexi" runat="server" Text="Flexi" Checked="true" />
                    </div>
                    <div class="col20">
                        <br />
                        <br />
                        <br />
                        <asp:Button ID="btnDisplayRates" runat="server" Text="Display Rates" OnClick="btnDisplayRates_Click" />
                        <br />
                        <br />
                        <asp:LinkButton ID="lnkAddEditRate" runat="server" Text="Add / Edit Rates" OnClick="lnkAddEditRate_Click" />
                    </div>
                    <div style="clear: both;" />
                </div>
            </asp:Panel>
            <!-- hidden panel removed as I'm out of characters -->
            <!-- Listview containing user control.. usercontrol has formview with a linkbutton -->
            <asp:Panel runat="server" ID="pnlViewAccommodationRates" Visible="false">
                <div class="propertyaddedit_ratesbox">
                    <fieldset>
                        <legend>Click on individual dates to edit</legend>
                        <asp:Panel ID="Panel5" runat="server" ScrollBars="Vertical" Height="400">
                            <asp:ListView ID="PropertyAccommodationRates1" runat="server" DataSourceID="PropertyCMSRateListDs"
                                ItemPlaceholderID="itemsGoHere" OnItemDataBound="PropertyAccommodationRates1_ItemDataBound"
                                GroupItemCount="7" GroupPlaceholderID="groupsGoHere">
                                <GroupTemplate>
                                    <tr>
                                        <asp:PlaceHolder runat="server" ID="itemsGoHere" />
                                    </tr>
                                </GroupTemplate>
                                <ItemTemplate>
                                    <td>
                                        <uc1:RatesEditDate ID="RatesViewDate1" runat="server" OnEditDateRateSelected="RatesEditDate1_EditDateRateSelected"
                                            OnEditDateRateUpdated="RatesEditDate1_EditDateRateUpdated" />
                                    </td>
                                </ItemTemplate>
                                <LayoutTemplate>
                                    <table width="525px" cellspacing="2">
                                        <tr class="propertyaddedit_gridViewRates">
                                            <th class="propertyaddedit_ratesGrid">
                                                Monday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Tuesday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Wednesday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Thursday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Friday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Saturday
                                            </th>
                                            <th class="propertyaddedit_ratesGrid">
                                                Sunday
                                            </th>
                                        </tr>
                                        <tr>
                                            <asp:PlaceHolder runat="server" ID="groupsGoHere" />
                                        </tr>
                                    </table>
                                </LayoutTemplate>
                            </asp:ListView>
                            <asp:ObjectDataSource ID="PropertyCMSRateListDs" runat="server" SelectMethod="ListPropertyCMSRates"
                                TypeName="Shared.MtaLib.Bll.PropertyRateController" OnSelecting="PropertyCMSRateListDs_Selecting">
                                <SelectParameters>
                                    <asp:Parameter Name="propertyId" Type="Int32" />
                                    <asp:Parameter Name="accommodationTypeId" Type="Int32" />
                                    <asp:Parameter Name="minDateTime" Type="DateTime" />
                                    <asp:Parameter Name="maxDateTime" Type="DateTime" />
                                    <asp:Parameter Name="fillMondays" Type="Boolean" DefaultValue="true" />
                                </SelectParameters>
                            </asp:ObjectDataSource>
                        </asp:Panel>
                    </fieldset>
                </div>
            </asp:Panel>



            <div style="clear: both;">
            </div>
            <!-- this is the usercontrol where the edit occurs -->
            <div runat="server" id="divEditRate" visible="true">
                <uc1:RatesEditDate ID="RatesEditDate1" runat="server" />
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

My user control:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RatesEditDate.ascx.cs"
    Inherits="UserControls_RatesEditDate" %>
<asp:FormView ID="frmViewRatesDate" runat="server" Height="50px" Width="125px" OnItemCommand="frmViewRatesDate_ItemCommand"
    DataKeyNames="TheDateTime">
    <ItemTemplate>
        <asp:LinkButton ID="lnkEditDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TheDateTime", "{0:dd/MM/yyyy}") %>'
            OnClick="lnkEditDate_Click" />
        <asp:Literal ID="Literal1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MTABuyRateDaily", "{0:n}") %>'
            Visible='<%# DisplayMTABuy %>' />
        <asp:Literal ID="Literal2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MTASellRateDaily", "{0:n}") %>'
            Visible='<%# DisplayMTASell %>' />
        <asp:Literal ID="Literal3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "AffiliateNettDaily", "{0:n}") %>'
            Visible='<%# DisplayAffiliate %>' />
        <asp:Literal ID="Literal4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MTABuyRateWeekly", "{0:n}") %>'
            Visible='<%# DisplayWeekly %>' />
        <asp:Literal ID="Literal5" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MTASellRateWeekly", "{0:n}") %>'
            Visible='<%# DisplayWeekly %>' />
        <asp:Literal ID="Literal6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "AffiliateNettWeekly", "{0:n}") %>'
            Visible='<%# DisplayWeekly %>' />
        <asp:Literal ID="Literal7" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ThirtyDay", "{0:n}") %>'
            Visible='<%# DisplayThirtyDay %>' />
        <asp:Literal ID="Literal8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Flexi", "{0:n}") %>'
            Visible='<%# DisplayFlexi %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <div class="field">
            <asp:Literal ID="TheDate" runat="server" Text='<%# Bind("TheDateTime", "{0:dd/MM/yyyy}") %>' />
        </div>
        <div class="field">
            <asp:Label ID="Label1" Text="MTA buy rate" AssociatedControlID="MTABuyRate1" runat="server" /><asp:TextBox
                ID="MTABuyRate1" runat="server" Text='<%# Bind("MTABuyRateDaily", "{0:n}")%>' />
            <asp:CompareValidator ID="MTABuyRateValidator" runat="server" Type="Currency" ControlToValidate="MTABuyRate1"
                Operator="GreaterThan" ValueToCompare="0" ErrorMessage="" Display="Dynamic" ValidationGroup="EditPrice">
                <asp:Image ID="Image8" SkinID="ValidationErrorImage" runat="server" />
            </asp:CompareValidator>
            <asp:RequiredFieldValidator ID="MTABuyRateValidator2" runat="server" ControlToValidate="MTABuyRate1"
                ErrorMessage="" Display="Dynamic" ValidationGroup="EditPrice">
                <asp:Image ID="Image33" SkinID="ValidationErrorImage" runat="server" />
            </asp:RequiredFieldValidator>
        </div>
        <!-- similar items removed due to character restriction -->
        <div class="field">
            <asp:Label ID="Label6" Text="Total PAX" runat="server" AssociatedControlID="TotalPax1" />
            <asp:CompareValidator ID="TotalPaxValidator1" runat="server" ControlToValidate="TotalPax1"
                Operator="NotEqual" ValueToCompare="0" ErrorMessage="Please choose a non zero Total Pax "
                Display="Dynamic" ValidationGroup="EditPrice">
                <asp:Image ID="Image15" SkinID="ValidationErrorImage" runat="server" />
            </asp:CompareValidator>
            <asp:DropDownList ID="TotalPax1" runat="server">
                <asp:ListItem Value="0" />
                <asp:ListItem Value="1" />
                <asp:ListItem Value="2" />
                <asp:ListItem Value="3" />
                <asp:ListItem Value="4" />
                <asp:ListItem Value="5" />
                <asp:ListItem Value="6" />
                <asp:ListItem Value="7" />
                <asp:ListItem Value="8" />
                <asp:ListItem Value="9" />
                <asp:ListItem Value="10" />
            </asp:DropDownList>
        </div>
        <div class="field">
            <asp:Label runat="server" ID="Label15" AssociatedControlID="EditPriceMinStayList"
                Text="Min stay (nights)" />
            <asp:CompareValidator ID="EditPriceMinStayListValidator1" runat="server" ControlToValidate="EditPriceMinStayList"
                Operator="NotEqual" ValueToCompare="0" ErrorMessage="Please choose a non zero Min Stay"
                Display="Dynamic" ValidationGroup="EditPrice">
                <asp:Image ID="Image6" SkinID="ValidationErrorImage" runat="server" />
            </asp:CompareValidator>
            <!-- todo bind-->
            <asp:DropDownList ID="EditPriceMinStayList" runat="server">
                <asp:ListItem Value="0" />
                <asp:ListItem Value="1" />
                <asp:ListItem Value="2" />
                <asp:ListItem Value="3" />
                <asp:ListItem Value="4" />
                <asp:ListItem Value="5" />
                <asp:ListItem Value="6" />
                <asp:ListItem Value="7" />
                <asp:ListItem Value="8" />
                <asp:ListItem Value="9" />
                <asp:ListItem Value="10" />
                <asp:ListItem Value="11" />
                <asp:ListItem Value="12" />
                <asp:ListItem Value="13" />
                <asp:ListItem Value="14" />
                <asp:ListItem Value="15" />
                <asp:ListItem Value="16" />
                <asp:ListItem Value="17" />
                <asp:ListItem Value="18" />
                <asp:ListItem Value="19" />
                <asp:ListItem Value="20" />
                <asp:ListItem Value="21" />
                <asp:ListItem Value="22" />
                <asp:ListItem Value="23" />
                <asp:ListItem Value="24" />
                <asp:ListItem Value="25" />
                <asp:ListItem Value="26" />
                <asp:ListItem Value="27" />
                <asp:ListItem Value="28" />
                <asp:ListItem Value="29" />
                <asp:ListItem Value="30" />
            </asp:DropDownList>
        </div>
        <div class="field">
            <%--<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" CommandName="Update" />--%>
            <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" ValidationGroup="EditPrice" />
        </div>
        <asp:HiddenField runat="server" ID="hdnPropertyID" />
        <asp:HiddenField runat="server" ID="hdnAccommTypeID" />
    </EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="PropertyCMSRateItemDs" runat="server" SelectMethod="ListPropertyCMSRate"
    TypeName="Shared.MtaLib.Bll.PropertyRateController" UpdateMethod="UpdatePropertyCMSRate"
    OldValuesParameterFormatString="original_{0}" OnUpdated="PropertyCMSRateItemDs_Updated">
    <UpdateParameters>
        <asp:Parameter Name="propertyId" Type="Int32" />
        <asp:Parameter Name="accommodationTypeId" Type="Int32" />
        <asp:Parameter Name="minStay" Type="Int32" />
        <asp:Parameter Name="TheDateTime" Type="DateTime" />
        <asp:Parameter Name="MTABuyRateDaily" Type="Decimal" />
        <asp:Parameter Name="MTASellRateDaily" Type="Decimal" />
        <asp:Parameter Name="AffiliateNettDaily" Type="Decimal" />
        <asp:Parameter Name="MTABuyRateWeekly" Type="Decimal" />
        <asp:Parameter Name="MTASellRateWeekly" Type="Decimal" />
        <asp:Parameter Name="AffiliateNettWeekly" Type="Decimal" />
        <asp:Parameter Name="ThirtyDay" Type="Decimal" />
        <asp:Parameter Name="Flexi" Type="Decimal" />
        <asp:Parameter Name="IsBreakfastIncluded" Type="Boolean" />
        <asp:Parameter Name="IsFairWarning" Type="Boolean" />
        <asp:Parameter Name="TotalPax" Type="Int32" />
        <asp:Parameter Name="original_TheDateTime" Type="DateTime" />
    </UpdateParameters>
    <SelectParameters>
        <asp:Parameter Name="propertyId" Type="Int32" DefaultValue="0" />
        <asp:Parameter Name="accommodationTypeId" Type="Int32" DefaultValue="0" />
        <asp:Parameter Name="theDateTime" Type="DateTime" />
    </SelectParameters>
</asp:ObjectDataSource>

解决方案

Well it appears my issue is related to the formview and changing modes.

I ended up creating a second usercontrol with a formview that only has the edit item template and I set the default mode to edit. Using this one for editing, and the original one for viewing the rates has gotten rid of the viewstate error.

It's obviously not ideal as I shouldn't have to do this - but whilst I tried to force it to not switch to readonly mode after saving the changes - nothing I tried would prevent that viewstate error. Spent long enough on this issue already - time to move on!! Thanks for the suggestions though people :)

这篇关于无法加载视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆