代替这个..

    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.

        }
    }
}


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

07-28 09:29