代码之家  ›  专栏  ›  技术社区  ›  Robert W

用户控件在回发过程中丢失视图状态

  •  0
  • Robert W  · 技术社区  · 16 年前

    我有一个用户控件,它使用对象作为内部属性(下面是一些代码)。

    我在以编程方式设置步骤类的属性时遇到问题,当以编程方式设置时,它会在回发过程中丢失,这表明与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 { }
    }
    
    2 回复  |  直到 16 年前
        1
  •  4
  •   scherand    16 年前

    我认为你设置的字符串永远不会到达视图状态。我这里的术语有点短(读:我不知道术语),但我认为你的属性 [PersistenceMode(PersistenceMode.Attribute)] 只告诉ASP.NET它应该在标记(aspx文件)中查找一个名为“status”的属性,如果找到了该属性,请设置该属性 Status 它的价值(实际上我想知道它到底放在您的示例中的什么地方?)。但它不会告诉任何人将某些内容放入视图状态。

    如果你要定义你的财产 状态 沿着这些线

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

    你应该过得更好。

    剩下的我不确定你是否需要打电话 TrackViewState() 在用户控件中,甚至重写 SaveViewState LoadViewState 但我不这么认为。如果是这种情况,以下链接可能会有所帮助:

        2
  •  0
  •   Arthis    16 年前

    这可能与在页面中创建控件的顺序有关。如果在回发后,控件的创建顺序与页面的第一次加载顺序不同,则检索视图状态对这些控件无效。

    如何以编程方式设置step的属性?