2010-03-29 67 views
0

我有一個用戶控件,它使用對象作爲內部屬性(某些代碼如下)。Usercontrol在Postback中丟失Viewstate

我遇到了以編程方式設置Step類的屬性的問題,當它以編程方式設置時,它會在回傳中丟失,這將指示ViewState(?)做些什麼。

以聲明方式設置Step類的屬性時,它工作正常。

有沒有人有任何想法這個代碼是什麼/是什麼原因導致它跨越回髮狀態?

ASPX頁

<uc1:StepControl ID="StepControl1" runat="server"> 
     <Step1 Title="1. Select your Products" Enabled="true"> 
      <Content> 

       <div class="clearfix"> 
        <div class="floatRight"> 
         <asp:Button ID="btnGoToStep2" 
         runat="server" 
         Text="Next" 
         CausesValidation="false" 
         OnClick="btnGoToStep2_OnClick" /> 
        </div> 
       </div> 

      </Content> 
     </Step1> 
     <Step2 Title="2. Select your Features">  
      <Content> 

       <div class="clearfix"> 
        <div class="floatLeft"> 
         <asp:Button ID="btnBackToStep1" 
         runat="server" 
         Text="Back" 
         CausesValidation="false" 
         OnClick="btnBackToStep1_OnClick" /> 
        </div>      
        <div class="floatRight"> 
         <asp:Button ID="btnGoToStep3" 
         runat="server" 
         Text="Next" 
         CausesValidation="false" 
         OnClick="btnGoToStep3_OnClick" /> 
        </div> 
       </div>      

      </Content> 
     </Step2>      
    </uc1:StepControl> 

後面ASPX代碼

protected void btnGoToStep2_OnClick(object sender, EventArgs e) 
    { 
     StepControl1.Step1.StatusText = "4 Products Selected"; 
    } 

    protected void btnBackToStep1_OnClick(object sender, EventArgs e) 
    { 
     // StatusText (of Step1) gets lost at this point. 
    } 

用戶控制碼後面

public partial class StepControl : System.Web.UI.UserControl 
{ 
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    [NotifyParentProperty(true)] 
    public Step Step1 { get; set; } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    [NotifyParentProperty(true)] 
    public Step Step2 { get; set; } 

    protected void Page_Init(object sender, EventArgs e) 
    { 
     AddSteps(); 
    } 

    private void AddSteps() { } 
} 

[Serializable()] 
[ParseChildren(true)] 
[PersistChildren(false)] 
public class Step 
{ 
    [PersistenceMode(PersistenceMode.Attribute)] 
    public string Title { get; set; } 

    [PersistenceMode(PersistenceMode.Attribute)] 
    public string Status { get; set; } 

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [TemplateInstance(TemplateInstance.Single)] 
    [TemplateContainer(typeof(StepContentContainer))] 
    public ITemplate Content { get; set; } 

    public class StepContentContainer : Control, INamingContainer { } 
} 
+0

您使用在你的頁面的任何AJAX或更新面板所具有視圖狀態問題? – 2010-03-29 14:31:04

+0

@Tj凱莉:不行 - 我已經評論過所有這些讓我不知所措。 – 2010-03-29 14:33:44

回答

4

我想你設置的字符串永遠不會讓它到ViewState。我在這裏有點缺乏術語(閱讀:我不知道術語),但我認爲你的屬性[PersistenceMode(PersistenceMode.Attribute)]只告訴ASP.NET它應該在標記(ASPX文件)中尋找名爲「狀態」的屬性,如果它找到一個,將屬性Status設置爲它的值(實際上我想知道它在你的例子中的確切位置?)。它並沒有告訴任何人把東西放入ViewState中。

如果你想沿着這些線路

[PersistenceMode(PersistenceMode.Attribute)] 
public string Status 
    { 
     get 
     { 
      object o = ViewState["Status"]; 
      if(o != null) { 
       return (string)o; 
      } 
      return string.Empty; 
     } 
     set 
     { 
      ViewState["Status"] = value; 
     } 
    } 

定義屬性Status你應該會更好。

對於其餘部分,我不確定是否必須在UserControls中調用TrackViewState()甚至覆蓋SaveViewStateLoadViewState,但我不這麼認爲。如果這會出現這種情況以下鏈接可以幫助:

0

它可能與您在頁面中創建控件的順序有關。如果在回發之後,控件的創建順序與第一次加載頁面的順序不同,那麼撤回視圖狀態將不適用於這些控件。

如何以編程方式設置步驟的屬性?

+0

@yoannr:我用一些額外的代碼更新了這個問題。 – 2010-03-29 15:05:17

+0

沒有找到解決方案,對不起!如果你發現任何東西,請告訴我們..;) – Arthis 2010-03-30 07:50:02

相關問題