ViewState 不会在回发过程中保留自定义添加内容

发布于 2024-12-08 11:59:45 字数 953 浏览 0 评论 0原文

我有一个问题,可能是因为我不理解视图状态的工作方式。我有一些代码在我制作的自定义控件中的属性的设置器中设置视图状态变量。

 public bool EditMode
    {
        get { return (bool)(ViewState["editMode" + this.ID] ?? false); }
        set {ViewState["editMode" + this.ID] = value;} 
    }

编辑模式是在按钮单击事件期间设置的。

 public void shippingButton_Click(object sender, EventArgs e) 
    {
        if (((Button)sender).CommandName== "Edit")
        {
            ctrlShippingAddress.EditMode = true;
        }
        else
        {
            Page.Validate();
            if (Page.IsValid)
            {
                ctrlShippingAddress.SaveAddress();
                ctrlShippingAddress.EditMode = false;
            }
        }
    }

我尝试在页面加载时手动设置它,以防我没有在页面周期的正确位置将其添加到视图状态中,但据我了解,事件发生在渲染之前。我还尝试将 ViewStateMode="Enabled" 添加到控件,然后添加到使用它的页面,然后添加到母版页,但没有成功。

如果我在 get/set 点进行调试,我会看到 viewstate 是一个空集合(这没有意义,因为它还保存了应有的持久保存的表单数据)。

我很感激任何帮助。

I have a question, and it may be because I'm not understanding the way viewstate works. I have some code that sets a viewstate variable in the setter for a property in a custom control I have made.

 public bool EditMode
    {
        get { return (bool)(ViewState["editMode" + this.ID] ?? false); }
        set {ViewState["editMode" + this.ID] = value;} 
    }

The editmode is being set during a button click event.

 public void shippingButton_Click(object sender, EventArgs e) 
    {
        if (((Button)sender).CommandName== "Edit")
        {
            ctrlShippingAddress.EditMode = true;
        }
        else
        {
            Page.Validate();
            if (Page.IsValid)
            {
                ctrlShippingAddress.SaveAddress();
                ctrlShippingAddress.EditMode = false;
            }
        }
    }

I've tried manually setting it on page load in case I wasn't adding this to the viewstate at the correct point in the page cycle, but as I understand it events occur before render. I have also tried adding ViewStateMode="Enabled" to the control, then to the page using it, then to the master page with no luck.

If I debug at the point of the get/set I see that viewstate is an empty collection (which doesn't make sense because it's also saving form data that is persisting as it should).

I appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯淡〆 2024-12-15 11:59:45

您不需要将控件 ID 附加到 ViewState 键。我认为您这样做是为了独特性,但这不是必要的。

上面的方法可能会解决您的问题,但如果不能,请尝试以下方法:

public bool EditMode 
{ 
    get 
    { 
        bool editMode = false;
        if (ViewState["editMode"] != null)
            editMode = (bool)ViewState["editMode"];
        return editMode;
    } 
    set 
    {
        ViewState["editMode"] = value;
    }  
} 

You don't need to append the control ID to the ViewState key. I assume you're doing that for uniqueness, but it's not necessary.

The above may fix your problem, but if not try something like this instead:

public bool EditMode 
{ 
    get 
    { 
        bool editMode = false;
        if (ViewState["editMode"] != null)
            editMode = (bool)ViewState["editMode"];
        return editMode;
    } 
    set 
    {
        ViewState["editMode"] = value;
    }  
} 
马蹄踏│碎落叶 2024-12-15 11:59:45

与同事审查后,我发现问题出在 web.config 的页面节点中,

<pages enableViewState="false"> 

需要设置为 true

After reviewing with a co-worker I discovered that the issue was in the pages node of the web.config

<pages enableViewState="false"> 

needed to be set to true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文