用户控件在回发中丢失视图状态 [英] Usercontrol losing Viewstate across Postback

查看:29
本文介绍了用户控件在回发中丢失视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在以编程方式设置 Step 类的属性时遇到问题,当以编程方式设置时,它在回发中丢失,这表明与 Viewstate (?) 有关.

当以声明方式设置 Step 类的属性时,它工作正常.

有没有人知道这段代码是什么/是什么导致它在回发时丢失状态?

ASPX 页面

 <Step1 Title="1. 选择您的产品" Enabled="true"><内容><div class="clearfix"><div class="floatRight"><asp:Button ID="btnGoToStep2"runat="服务器"文字=下一个"CausesValidation="false"OnClick="btnGoToStep2_OnClick"/>

</内容></步骤1><Step2 Title="2. 选择您的功能"><内容><div class="clearfix"><div class="floatLeft"><asp:Button ID="btnBackToStep1"runat="服务器"文字=返回"CausesValidation="false"OnClick="btnBackToStep1_OnClick"/>

<div class="floatRight"><asp:Button ID="btnGoToStep3"runat="服务器"文字=下一个"CausesValidation="false"OnClick="btnGoToStep3_OnClick"/>

</内容></Step2></uc1:StepControl>

背后的ASPX代码

 protected void btnGoToStep2_OnClick(object sender, EventArgs e){StepControl1.Step1.StatusText = "选择了 4 个产品";}protected void btnBackToStep1_OnClick(object sender, EventArgs e){//StatusText (of Step1) 此时丢失.}

背后的用户控制代码

公共部分类 StepControl : System.Web.UI.UserControl{[PersistenceMode(PersistenceMode.InnerProperty)][DesignerSerializationVisibility(DesignerSerializationVisibility.Content)][NotifyParentProperty(true)]public Step1 { get;放;}[PersistenceMode(PersistenceMode.InnerProperty)][DesignerSerializationVisibility(DesignerSerializationVisibility.Content)][NotifyParentProperty(true)]公开第2步{得到;放;}protected void Page_Init(对象发送者,EventArgs e){添加步骤();}私有无效 AddSteps() { }}[可序列化()][ParseChildren(true)][PersistChildren(false)]公开课步骤{[PersistenceMode(PersistenceMode.Attribute)]公共字符串标题{获取;放;}[PersistenceMode(PersistenceMode.Attribute)]公共字符串状态 { 获取;放;}[PersistenceMode(PersistenceMode.InnerProperty)][TemplateInstance(TemplateInstance.Single)][TemplateContainer(typeof(StepContentContainer))]公共 ITemplate 内容 { 获取;放;}公共类 StepContentContainer : Control, INamingContainer { }}

解决方案

我认为您设置的字符串永远不会进入 ViewState.我在这里有点术语(阅读:我不知道术语)但我认为你的属性 [PersistenceMode(PersistenceMode.Attribute)] 只告诉 ASP.NET 它应该寻找一个名为标记(ASPX 文件)中的状态",如果找到,则将属性 Status 设置为其值(实际上我想知道它在您的示例中的确切位置?).不过,它并没有告诉任何人将某些内容放入 ViewState 中.

如果您将按照这些行定义您的属性 Status

[PersistenceMode(PersistenceMode.Attribute)]公共字符串状态{得到{对象 o = ViewState["状态"];if(o != null) {返回(字符串)o;}返回字符串.空;}放{视图状态[状态"] = 值;}}

你应该过得更好.

对于其余部分,我不确定您是否必须在 UserControls 中调用 TrackViewState() 或什至覆盖 SaveViewStateLoadViewState 但是我不这么认为.如果是这种情况,以下链接可能会有所帮助:

I have a user control which uses objects as inner properties (some code is below).

I am having trouble with setting the attribute of the Step class programmatically, when set programmatically it is being lost across postback which would indicate something to do with Viewstate (?).

When setting the property of the Step class declaratively it's working fine.

Does anybody have any ideas of what this code be/what's causing it to lose the state across postback?

ASPX Page

    <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 code behind

    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. 
    }

User control code behind

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 { }
}

解决方案

I think the string you set never makes it to the ViewState. I am a bit short of terminology here (read: I do not know the terminology) but I think your attribute [PersistenceMode(PersistenceMode.Attribute)] only tells ASP.NET it should look for an attribute called "Status" in the markup (ASPX-file) and if it finds one, set the property Status to its value (actually I am wondering where exactly it is put in your example?). It does not tell anybody to put something into ViewState though.

If you would define your property Status along these lines

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

you should be better off.

For the rest of it I am not sure if you have to call TrackViewState() in UserControls or even override SaveViewState and LoadViewState but I do not think so. If this would be the case the following links might help:

这篇关于用户控件在回发中丢失视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆