asp.net – 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.

        }
    }
}

继承此类的页面可以使用您提出的属性驱动语法。

相关文章

判断URL文件是不是在于在。private static bool UrlIsExist(...
由于在.net中,Request时出现有HTML或Javascript等字符串时,...
public static bool ProcessIdCard(this string idCard, out...
protected void Page_Load(object sender, EventArgs e){ Sc...