我们如何设置Viewstate的属性?

时间:2010-05-28 13:15:27

标签: asp.net

我正在使用枚举

 public enum WatchUsageMode
    {
        Watch = 1,
        EmailPreferences = 2
    }

我想在我的视图状态中设置该枚举的属性,以便每当视图状态为null时返回Watch else EmailPreference.how我可以获取并设置属性吗?

2 个答案:

答案 0 :(得分:2)

创建一个属性来封装此

public WatchUsageMode WatchUsageModeValue
{
    get
    {
        if(ViewState["WatchUsageModeValue"] != null &&
            ViewState["WatchUsageModeValue"] is WatchUsageMode) 
            return (WatchUsageMode)ViewState["WatchUsageModeValue"];
        else
            return null;
    }
    set
    {
        ViewState["WatchUsageModeValue"] = value;
    }
}

答案 1 :(得分:0)

环境:

ViewState["KeyString"] = WatchUsageMode.EmailPreferences

从视图状态获取

WatchUsageMode get()
{ 
if(ViewState["KeyString"]!=null) 
    return (WatchUsageMode)ViewState["KeyString"]; 
return WatchUsageMode.Watch; 
}