代码之家  ›  专栏  ›  技术社区  ›  Chris

具有[Flags]枚举的ASP.NET控件属性

  •  12
  • Chris  · 技术社区  · 15 年前

    2 回复  |  直到 15 年前
        1
  •  13
  •   M4N    15 年前

    例如,如果我控制着这个财产:

    public System.IO.FileOptions Options { get; set; }
    

    我可以在标记中设置它,如下所示:

    <uc1:MyControl ID="control1" runat="server"
        Options="DeleteOnClose,Asynchronous" />
    
        2
  •  7
  •   Ben Lesh    15 年前

    Test.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Test.ascx.cs" Inherits="Test" %>
    <asp:Label ID="lblTest" runat="server"></asp:Label>
    

    Test.ascx.cs

    public partial class Test : System.Web.UI.UserControl
    {
        public TestEnum MyProperty
        {
            //coalesce was done to be lazy. sorry. haha.
            get { return (TestEnum)(ViewState["te"] ?? TestEnum.One); }
            set { ViewState["te"] = value; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            lblTest.Text = MyProperty.ToString();
        }
    }
    
    [Flags]
    public enum TestEnum : int
    {
        One = 1,
        Two = 2,
        Four = 4,
        Eight = 8
    }
    

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
    
    <%@ Register Src="~/Test.ascx" TagPrefix="test" TagName="Test" %>
    <form id="form1" runat="server">
        <test:Test ID="test" runat="server" MyProperty="Four,Eight" />
    </form>