Is it possible to pass an enum to a Web User Control?

M

M

I can pass in a string to a Web User Control, but I haven't been successful
passing in an enum. I declared my enum as a public variable in my Web User
Control itself.

Thanks in advance.
 
K

Karl Seguin

It'd be easier if you showed us what you have so far...

public class x1 : System.Web.UI.UserControl
{

private DisplayType type;
public DisplayType Type
{
get { return type; }
set { type = value; }
}
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write(type.ToString());
}
}
public enum DisplayType
{
Simple = 10,
Complex = 15
}


<uc1:x1 Type="Simple" id="X11" runat="server"></uc1:x1>

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
M

M

Hi Karl,

I thought I was not clear, but your explanation is right on; you read my
mind. And it solved my problem too.

I was trying something like:
<uc1:x1 Type="DisplayType.Simple" id="X11" runat="server"></uc1:x1>

then even more complicated:
<uc1:x1 Type="MyNamespace.MyClass.DisplayType.Simple" id="X11"
runat="server"></uc1:x1>

I got a couple of questions though because I'm not sure I understand why it
works.
1. How can you access Simple directly? That is without DisplayType.Simple.
2. Is an enum actually a class by itself (or subclass in this case)? Seems I
read that somewhere, or maybe I'm just getting completely confused.

Thanks a lot.
 
K

Karl Seguin

M,
ASP.Net uses something known as TypeConverters to turn seemingly "string"
values into real values...For example, when you type ForeColor="Red" since
ForeColor is of type Color it uses the ColorConverter to turn "Red" into an
actual instance of the class Color which represents red.

http://msdn.microsoft.com/library/d...rfsystemdrawingcolorconvertermemberstopic.asp

Not suprisingly, for enumerations, it uses a class called EnumConverter:
http://msdn.microsoft.com/library/d...emcomponentmodelenumconvertermemberstopic.asp


You can check out the code of EnumConverter using reflector...but, to answer
your question, that's just how EnumConverter works....it knows that Simple
is of type DisplayType because the Type property is of that type...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top