作为的ViewState属性 [英] ViewState as Attribute
本文介绍了作为的ViewState属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
而不是这个..
public string Text
{
get { return ViewState["Text"] as string; }
set { ViewState["Text"] = value; }
}
我想这个..
[ViewState]
public String Text { get; set; }
能不能做到?
推荐答案
这样的:
public class BasePage: Page {
protected override Object SaveViewState() {
object baseState = base.SaveViewState();
IDictionary<string, object> pageState = new Dictionary<string, object>();
pageState.Add("base", baseState);
// Use reflection to iterate attributed properties, add
// each to pageState with the property name as the key
return pageState;
}
protected override void LoadViewState(Object savedState) {
if (savedState != null) {
var pageState = (IDictionary<string, object>)savedState;
if (pageState.Contains("base")) {
base.LoadViewState(pageState["base"]);
}
// Iterate attributed properties. If pageState contains an
// item with the appropriate key, set the property value.
}
}
}
这是从该类继承可以使用你已经提出了属性驱动的语法页面。
Pages that inherit from this class could use the attribute-driven syntax you've proposed.
这篇关于作为的ViewState属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文