I've got a table where one of the columns is an integer. In my Linq To Sql mapping I map this column to a field whose type is an Enum. When I pull up this table in Dynamic Data the column in question is not displayed at all. What's up with this? Is there
any way to use an Enum-bound field with Dynamic Data?
Well, I couldn't wait so I came up with a solution myself. Now what I'd like to know is if there is a better way to accomplish this. To do it my way I did the following:
1. Open your .dbml file and click on the integer field that should be represented in .NET by an enum type. (If this enum isn't yet defined define it now.)
2. With the field in question selected, in the property pane change the Type property to the name of your enum (just type it in, it won't appear in the dropdown list.) That's all you need to do to get LINQ to map an integer in the db to a .NET enum. BUT,
that's not enough for the Dynamic Data stuff to do its magic. It seems (as I noted in my previous post) that Dynamic Data out of the box will just not render fields that are of type Enum. They just don't show up on the generated page. In order to get them
working you need to:
3. In your Dynamic Data web project add two new user controls to the App_Shared/DynamicDataFields folder, Enumeration.ascx and Enumeration_Edit.ascx.
4. Now that you've got Enumeration handlers the final step is to add a RenderHint to let the Dynamic Data framework know to use the Enumeration user controls to render that field. You can either do this by adding an attribute to your LINQ class like so:
[RenderHint("MyLocalFieldName", "Enumeration")] public partial class MyDBClass { }
Or if as in my case you have made a separate ListDetails.aspx page for editing this table then you can simply add RenderHint="Enumeration" to your asp:DynamicField declarations on the aspx page.
So is this the 'right' way to use enumerated fields in Dynamic Data?
Very nice! Yes, this is absolutely the right way of doing it. In fact, it's a great demonstration of the beneifits of the field template architecture: you can easily add support for a data type that we had not anticipated.
We'll look into whether we want to support this by default.
thanks,
David
Marked as answer by ricka6 on Oct 15, 2008 05:39 PM
Excellent. At first this seemed like a very difficult task but once I managed to wrap my head around how the Dynamic Data stuff is actually working the solution became pretty obvious. The ability to add custom types to the DynamicDataFields templates
is really pretty cool.
I do think that you should really consider native enum support similar to what I've done here. It would be great if this was built in and didn't require a render hint.
FieldTemplateUserControlBase no longer exists. Change any instances of the Inherits attribute that reference the old class to reference the FieldTemplateUserControl class
instead.
stevekain
Member
20 Points
16 Posts
Dynamic Data and Enum-bound fields
Jan 28, 2008 04:48 PM|LINK
I've got a table where one of the columns is an integer. In my Linq To Sql mapping I map this column to a field whose type is an Enum. When I pull up this table in Dynamic Data the column in question is not displayed at all. What's up with this? Is there any way to use an Enum-bound field with Dynamic Data?
stevekain
Member
20 Points
16 Posts
Re: Dynamic Data and Enum-bound fields
Jan 28, 2008 07:15 PM|LINK
Well, I couldn't wait so I came up with a solution myself. Now what I'd like to know is if there is a better way to accomplish this. To do it my way I did the following:
1. Open your .dbml file and click on the integer field that should be represented in .NET by an enum type. (If this enum isn't yet defined define it now.)
2. With the field in question selected, in the property pane change the Type property to the name of your enum (just type it in, it won't appear in the dropdown list.) That's all you need to do to get LINQ to map an integer in the db to a .NET enum. BUT, that's not enough for the Dynamic Data stuff to do its magic. It seems (as I noted in my previous post) that Dynamic Data out of the box will just not render fields that are of type Enum. They just don't show up on the generated page. In order to get them working you need to:
3. In your Dynamic Data web project add two new user controls to the App_Shared/DynamicDataFields folder, Enumeration.ascx and Enumeration_Edit.ascx.
The code for Enumeration.ascx is:
The code for Enumeration_Edit.ascx is:
4. Now that you've got Enumeration handlers the final step is to add a RenderHint to let the Dynamic Data framework know to use the Enumeration user controls to render that field. You can either do this by adding an attribute to your LINQ class like so:
Or if as in my case you have made a separate ListDetails.aspx page for editing this table then you can simply add RenderHint="Enumeration" to your asp:DynamicField declarations on the aspx page.
So is this the 'right' way to use enumerated fields in Dynamic Data?
Enum Enumeration LINQ Dynamic Data
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Dynamic Data and Enum-bound fields
Jan 28, 2008 08:35 PM|LINK
Very nice! Yes, this is absolutely the right way of doing it. In fact, it's a great demonstration of the beneifits of the field template architecture: you can easily add support for a data type that we had not anticipated.
We'll look into whether we want to support this by default.
thanks,
David
stevekain
Member
20 Points
16 Posts
Re: Dynamic Data and Enum-bound fields
Jan 28, 2008 08:52 PM|LINK
Excellent. At first this seemed like a very difficult task but once I managed to wrap my head around how the Dynamic Data stuff is actually working the solution became pretty obvious. The ability to add custom types to the DynamicDataFields templates is really pretty cool.
I do think that you should really consider native enum support similar to what I've done here. It would be great if this was built in and didn't require a render hint.
Thanks. Still really liking Dynamic Data!
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Dynamic Data and Enum-bound fields
Jan 28, 2008 08:55 PM|LINK
Thanks Steve, this is really good feedback!
vishnu108mis...
Member
20 Points
65 Posts
Re: Dynamic Data and Enum-bound fields
Oct 15, 2008 03:39 PM|LINK
Hi Davidebb
I read your article about the enum data types.
I am stuck on a point in enum datatypes, actully I need to assign enum values from the database field. I have no idea how can I do this?
There is a field CareerTypes in Career Tables. I have to declare enum with the values of CareerTypes from the Career Tables.
Can you guide me/ wriite a method if possible?
I will very thankful for your help.
Thanks
Vishnu Narayan MIshra
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Dynamic Data and Enum-bound fields
Oct 15, 2008 08:22 PM|LINK
Hi Vishnu,
Which article about enum types are you referring to?
Note that this is a very old thread from way before we released, so it would be best to start a new thread to discuss your issue to avoid confusion.
thanks!
David
artcolman
Member
6 Points
8 Posts
Re: Dynamic Data and Enum-bound fields
Aug 18, 2011 05:36 PM|LINK
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.enumdatatypeattribute.aspx
For the Enumeration Data Type Attribute
http://www.drybridge.com Art Colman
Drybridge Technologies
http://www.drybridge.com
noman.aftab
Member
2 Points
1 Post
Re: Dynamic Data and Enum-bound fields
Sep 20, 2011 06:39 AM|LINK
I used your code and it didn't compiled and found that there are some changes made by Microsoft which are mentioned here:
http://www.asp.net/downloads/35-sp1/readme
FieldTemplateUserControlBase no longer exists. Change any instances of the Inherits attribute that reference the old class to reference the FieldTemplateUserControl class instead.
MetaMember changed to Column
DataItem has been changed to Row.
and so on.
Here is what it appears after the changes:
private string selectedValue = null; protected void Page_Load(object sender, EventArgs e) { selectedValue = EnumDDL.SelectedValue; string[] enumNames = Enum.GetNames(Column.ColumnType); Array.Sort(enumNames); EnumDDL.DataSource = enumNames; EnumDDL.DataBind(); } private string GetCurrentValue() { //DynamicMetaForeignKeyMember column = (DynamicMetaForeignKeyMember)MetaMember; object value = DataBinder.GetPropertyValue(Row, "Status"); if (value == null) return String.Empty; return value.ToString(); } protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); if (Mode == DataBoundControlMode.Edit) { EnumDDL.SelectedValue = GetCurrentValue(); } } protected override void ExtractValues(IOrderedDictionary dictionary) { object val = null; if (string.IsNullOrEmpty(selectedValue)) { try { val = Enum.Parse(Column.ColumnType, EnumDDL.SelectedValue); } catch { } } else { val = selectedValue; } dictionary[Column.Name] = val; }