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?
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