2010-06-18 94 views
5

OK剛剛開始出現此錯誤,我不知道爲什麼。我有一個託管頁面,其中有一個用戶控件的列表視圖和麪板。列表視圖用linkbutton加載記錄。你點擊鏈接按鈕來編輯那個特定的記錄 - 在formview中(在用戶控件中)加載到編輯模式。在formview中發生更新後,我觸發了託管頁面正在偵聽的事件。託管頁面然後重新綁定列表視圖以顯示更新的數據。這全部在一個updatepanel內。無法加載視圖狀態

所以這所有的工作 - 但是當我然後去點擊一個不同的LinkBut​​ton我得到下面的錯誤:

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. 

,就如何解決有什麼建議?

我的列表視圖似乎被成功反彈 - 我可以看到我改變的數據..我只是不知道爲什麼它點擊linkbutton時抱怨viewstate。還是有更好的方法來更新我的列表視圖中的數據?我的列表視圖和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> 
+0

我能想到的唯一動態控件是因爲listview itemtemplate包含一個用戶控件。當我在列表視圖中調用重新綁定時,它將與首次加載頁面時不同。但我必須以某種方式刷新列表視圖以反映新數據。 或者更多的更新面板相關的問題? – Jen 2010-06-18 04:40:49

+0

刪除我的更新面板,我仍然得到相同的錯誤。還嘗試註釋掉我的列表視圖的數據綁定,並仍然得到相同的錯誤..所以顯然是別的東西。 – Jen 2010-06-18 04:44:02

+0

試圖設置viewstate在我的列表視圖和/或我的formview中被禁用 - 並且這樣做導致頁面無法正常工作。當我點擊我的linkbutton時,網格會丟失它的項目,或者控件不會在編輯模式下顯示。 – Jen 2010-06-18 05:35:07

回答

1

那麼看起來我的問題是與formview和更改模式有關。

我最終創建了第二個帶有formview的用戶控件,它只有編輯項目模板,並且我設置了默認模式進行編輯。使用這一個進行編輯,而用於查看費率的原始視圖已經擺脫了視圖狀態錯誤。

這顯然是不理想的,因爲我不應該這樣做 - 但儘管我試圖強制它在保存更改後不能切換到只讀模式,但我嘗試過的任何操作都不會阻止視圖狀態錯誤。在這個問題上已經花了足夠長的時間 - 繼續前進!感謝您的建議,雖然人:)

4

很難給你一個確切的答案,而無需通過你的整個aspx頁面尋找,但一般當您修改使用javascript啓用Viewstate的元素時,會發生這樣的問題。當您回發到您的服務器(例如,單擊鏈接按鈕)時,頁面上的視圖狀態與服務器正在查找的視圖狀態不同,因此它崩潰。這是一項安全措施。

通過更新面板使用AJAX嗎?在更新面板中是否有頁面的某些部分,有些則不是?也許你的活動正在更新一些控件而不是其他的。

您能否提供我們可以查看的ASPX頁面的註釋版本?

2

這個問題也是我最近的噩夢。有很多可能導致最終導致「無法加載視圖狀態」錯誤。大多數情況下,它與動態創建的控件和控件的可見屬性有關,可以改變添加到頁面的控件的層次結構。

據我所知,該問題可能圍繞控件的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 %>' /> 
+0

感謝您的建議 - 但沒有喜悅。刪除文字上的所有可見屬性後仍然出現錯誤。 – Jen 2010-06-21 00:55:47

5

我已經與這個問題打了幾個小時和幾天。 我找到的答案都沒有完全解決我的問題,但我終於找到了解決方案。

我的場景如下。 我有一個GridView和一個FormView。這些是在MultiView控件的兩個視圖上。 我在GridView中顯示數據列表。我的編輯按鈕切換到FormView,因爲我想編輯的字段比gridview上的水平線更多(沒有水平滾動)。

如果我從FormView的編輯模式取消並返回到GridView(我使用OnClick事件來做到這一點),下次發生回發時,我會收到一個ViewState錯誤。

如果我從FormView的編輯模式取消,只需切換到ReadOnly模式,然後取消返回到GridView,一切工作正常。

SOLUTION: 我的解決方案是將FormView的默認模式設置爲「編輯」。 我的理論是,如果FormView默認模式是「ReadOnly」,當我從FormView的Edit模式中取消時,下次頁面加載時,它會嘗試將Edit模式的ViewState應用於ReadOnly模式(默認當頁面加載時)。通過將默認視圖設置爲「編輯」,隨着編輯模式保存的ViewState在頁面加載後重新加載到現在的新編輯模式(默認)。

即使MultiView的當前ActiveView顯示網格而不是表單,也會發生這種情況。

我還沒有因爲切換FormView控件「編輯」

希望這有助於默認模式的任何ViewState的問題,我歡迎任何評論。

謝謝。

HARV Sather

0

無法加載視圖狀態這一點的同時使用幾個內置在其中改變與背面後instace或異步回發事件

實施例視圖的ASP控制命令主要發生:控制等GridviewMultiviewFormviewListview

對於所有上述控制events命令等'edit'(其convers到編輯模式)在這一點上不存在2事件存在像1)'onrowediting' 2)'onrowedited' 注第二事件簡化版,存在。如果您試圖在此時動態添加控件'無法加載視圖狀態'發生。

解決方案:我們需要使用命令,如'select'它有兩個獨立的事件,如1)'onselectedindexchanging'和2)'onselectedindexchanged'

使用2次事件,我們可以添加控件和組動態的CSS,屬性..這將不會無法加載視圖狀態錯誤。但如果我們在第一個事件中使用它,它可能會拋出錯誤。

相關問題