无法加载viewstate

时间:2010-06-18 03:24:54

标签: c# listview events 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<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();
    }
}

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

<%@ 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>

我的用户控件:

<%@ 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>

6 个答案:

答案 0 :(得分:5)

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

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

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

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

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

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

自从将FormView的默认模式切换为“编辑”

后,我没有遇到任何ViewState问题

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

感谢。

Harv Sather

答案 1 :(得分:4)

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

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

您能否提供我们可以查看的ASPX页面的注释版本?

答案 2 :(得分:2)

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

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

<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 %>' />

答案 3 :(得分:1)

好吧,看来我的问题与表单视图和更改模式有关。

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

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

答案 4 :(得分:0)

加载viewstate失败主要是因为ASP控件的内置命令很少使用post-back instace或Async Postback事件更改视图

示例:GridviewMultiviewFormviewListview等控件。

对于上述所有控件,事件命令如 'edit' (转换为编辑模式)此时不存在2个事件,例如1) {{1} } 2) 'onrowediting' 注意第二个事件是否存在。如果您此时想要动态添加控件 &#39;无法加载视图状态&#39;

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

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

答案 5 :(得分:0)

不推荐,但您可以选择通过ViewStateMode变量在.aspx文件中禁用视图状态。

<%@ Control Language="C#" AutoEventWireup="true" ViewStateMode="Disabled" CodeFile="IN-Content-Filter.ascx.cs" Inherits="CMSWebParts_CustomWebParts_Intranet_IN_Content_Filter" %>
相关问题