I have developed an ASP.NET control for which one o开发者_Go百科f the properties is a [Flags] enum. However, I don't seem to be able to specify multiple flags for this property in the ASP.NET control markup. Is there a special syntax to do this or is it just not possible?
Maybe I'm understanding the question wrong, but can't you set the enum value with a comma-separated string.
E.g. if I have this property in my control:
public System.IO.FileOptions Options { get; set; }
The I can set it in the markup like this:
<uc1:MyControl ID="control1" runat="server"
Options="DeleteOnClose,Asynchronous" />
Just seperate the flags by commas.
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
}
Test.aspx
<%@ 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>
精彩评论