ViewState 不会在回发过程中保留自定义添加内容
我有一个问题,可能是因为我不理解视图状态的工作方式。我有一些代码在我制作的自定义控件中的属性的设置器中设置视图状态变量。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要将控件 ID 附加到 ViewState 键。我认为您这样做是为了独特性,但这不是必要的。
上面的方法可能会解决您的问题,但如果不能,请尝试以下方法:
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:
与同事审查后,我发现问题出在 web.config 的页面节点中,
需要设置为 true
After reviewing with a co-worker I discovered that the issue was in the pages node of the web.config
needed to be set to true