3

我有一个 asp.net 页面,其中我使用枚举(属性在 app_code 的类文件中定义)

现在我的问题是每当页面得到回发时,属性中的枚举值就会重置为第一个

我什至尝试将属性设置为静态,但仍然没有帮助。

下边是

我的枚举和财产声明:

private static UrlType _type;
public static UrlType UrlPattern
{
    get
    {
        HttpContext.Current.Response.Write("GET: " +_type + "<br>");
        return _type;
    }
    set
    {
        _type = value;
        HttpContext.Current.Response.Write("SET : " +_type + "<br>");
    }
}
public int VanityId { get; set; }
public enum UrlType
{
    ArticleOnly,
    ArticleCategoryCombination,
    Normal,
    TechForum
}

这就是我所说的:

public void BindRewrite()
{
    GrdRewrite.DataSource = objVanity.GetAllRewriteVanities(Vanity.UrlPattern);
    GrdRewrite.DataBind();
    if (Vanity.UrlPattern == Vanity.UrlType.ArticleCategoryCombination)
    {
        GrdRewrite.Columns[2].Visible = false;
        GrdRewrite.Columns[3].Visible = GrdRewrite.Columns[5].Visible = GrdRewrite.Columns[6].Visible = true;
    }
    else if (Vanity.UrlPattern == Vanity.UrlType.ArticleOnly)
    {
        GrdRewrite.Columns[5].Visible = true;
        GrdRewrite.Columns[2].Visible = GrdRewrite.Columns[3].Visible = GrdRewrite.Columns[6].Visible = false;
    }
    else if (Vanity.UrlPattern == Vanity.UrlType.Normal)
    {
        GrdRewrite.Columns[2].Visible = true;
        GrdRewrite.Columns[3].Visible = GrdRewrite.Columns[5].Visible = GrdRewrite.Columns[6].Visible = false;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    pnlAdmin.Visible = (objVanity.UserName == "host");

    if (objVanity.UserName == "host")
        Enable();
    else
        FieldsOpenForEditors(objVanity.SiteSupportUrlFormat);

    if (!IsPostBack)
    {
        Vanity.GenerateListFromEnums(drpAdminUrlType);
        if (objVanity.UserName == "host")
            Vanity.UrlPattern = Vanity.UrlType.ArticleOnly;
        else
            Vanity.UrlPattern = objVanity.SiteSupportUrlFormat;

        BindRewrite();
    }
}

谁能告诉我如何在回发中保留枚举的价值

我认为 viewstate 可能是选项,但不知道如何存储枚举值和恢复枚举中的字符串值。

4

1 回答 1

8

如果你想在回发之间持久化一个值,你需要将它存储在 Session、Cache 或 ViewState 中。

在您的情况下, ViewState 可能是首选。

public UrlType UrlPattern
{
    get
    {
        if (ViewState["UrlPattern"] != null)
            return (UrlType)Enum.Parse(typeof(UrlType), ViewState["UrlPattern"].ToString());
        return UrlType.Normal; // Default value
    }
    set
    {
        ViewState["UrlPattern"] = value;
    }
}
于 2013-09-25T17:53:44.337 回答