This article introduces readers to the DropDownListField field binding control and EnumDescriptionAttribute properties implemented in NBear. Combining these two components can perfectly solve the problem of binding Enum to ASP.NET data binding controls in the simplest, easiest to use and scalable way. [05/26 Revision] - Added support for third-party enumeration descriptions and support for binary and over-the-counter enumeration values.
From the name of DropDownListField, you must have guessed that, yes, DropDownListField control is the same as ASP.NET's built-in BoundField, CheckBoxField, etc., and can be directly used in the declaration of GridView, DetailsView and other controls to describe the binding of an Enum type field. Certainly. Using DropDownListField, we no longer need to use a custom ItemTemplate and embed the DropDownList in order to bind an Enum type value, and write additional code to fill the DropDownList in the ItemTemplate.
First, let's see how to use the DropDownListField control in our page code:
1<%@ Page Language="C#" %>
2<%@ Register TagPrefix="nb" Namespace="NBear.Web.Data" Assembly="NBear.Web.Data" %>
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
4<html xmlns=" http://www.w3.org/1999/xhtml " >
5<head runat="server">
6 <title>NBear DataSource Test</title>
7</head>
8<body>
9<form id="aspnetForm" runat="server">
10 <nb:NBearDataSource ID="TestDS" runat="server" ConnectionStringName="test access db"
11 TypeName="Entities.SimpleData" FilterExpression="{SimpleID} > 0" DefaultOrderByExpression="{SimpleID} DESC, {SimpleName}" />
12 <asp:GridView ID="TestGrid" runat="server" DataSourceID="TestDS" AllowSorting="True" AllowPaging="True"
13 PageSize="3" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" DataKeyNames="SimpleId" AutoGenerateColumns="False" >
14 <Columns>
15 <asp:BoundField DataField="SimpleId" HeaderText="SimpleId" SortExpression="SimpleId" />
16 <asp:BoundField DataField="SimpleName" HeaderText="SimpleName" SortExpression="SimpleName" />
17 <asp:CheckBoxField DataField="BoolVal" HeaderText="BoolVal" SortExpression="BoolVal"
18 Text="BoolVal" />
19 <nb:DropDownListField DataField="Status" HeaderText="Status" SortExpression="Status" EnumType="EntityDesigns.SimpleStatus" />
20 </Columns>
21 </asp:GridView>
22 <br />
23 <asp:DetailsView ID="TestDetail" runat="server" DataSourceID="TestDS" DefaultMode="Insert"
24 AutoGenerateInsertButton="True" AutoGenerateRows="False">
25 <Fields><asp:BoundField DataField="SimpleName" HeaderText="Name" />
26 <asp:CheckBoxField DataField="BoolVal" HeaderText="BoolVal" />
27 <nb:DropDownListField DataField="Status" HeaderText="Status" EnumType="EntityDesigns.SimpleStatus" />
28 </Fields>
29 </asp:DetailsView>
30</form>
31</body>
32</html>
Please note lines 2 and 19,27. In the page, first register the NBear.Web.Data namespace. Next, just add DropDownListField to the Columns of the data control. The only difference from the use of ASP.NET's built-in CheckBoxField is that DropDownListField must set an additional EnumType property. As the name suggests, it is bound to the type of the Enum field of this control. FullName.
OK, it's that simple. The SimpleStatus used in this page is as follows:
1 public enum SimpleStatus
2 {
3 Value1 = 1,
4 Value2 = 2
5}
If you run this page, you will see that Value1 and Value2 are displayed in the DropDownList.
-Wait
for 1 second, you must say, this is not what I want yet, because the text description that I need to display the enumeration value in the DropDownList is the custom information I need.
OK, then it’s the turn of the second protagonist of this article-EnumDescriptionAttribute.
Just use EnumDescriptionAttribute to annotate Value1 and Value2 as follows:
1 public enum SimpleStatus
2 {
3 [NBear.Common.EnumDescription(DefaultDescription="Desc of Value1")]
4 Value1 = 1,
5 [NBear.Common.EnumDescription(DefaultDescription="Desc of Value2")]
6 Value2 = 2
7}
OK, no additional settings are required. Run the page above and you will see that the text displayed in the DropDownList is the custom information we specified. Cool isn't it?
-We
still won't be satisfied if we wait another second. Although we can specify custom description information, if it is a multi-language environment, we need to display different text information for the enumeration value at runtime. There are many situations. , we need to read the description information of the enumeration from the enumeration description table in the database.
We certainly have a solution for this.
We only need to inherit EnumDescriptionAttribute. The following MyEnumDescriptionAttribute demonstrates a custom enumeration description implementation:
1 public class MyEnumDescriptionAttribute : NBear.Common.EnumDescriptionAttribute
2 {
3 private static string[] customDescs = new string[] { "custom desc of Value1", null }; //the second value is null here to use the DefaultDescription set in enum definition
4
5 public override string GetDescription(object enumValue)
6 {
7 return customDescs[(int)enumValue] ?? base.GetDescription(enumValue);
8}
9}
This class overloads the GetDescription() method of EnumDescriptionAttribute to read description information from an array in memory. Similarly, we can also read description information from resource files or databases here. We only need to overload this method.
You must have noticed the comment code. If, for an enumeration value, our custom method cannot get the custom description information, then it will first check whether the MyEnumDescriptionAttribute annotated with this enumeration value is specified. DefaultDescription, if specified, returns this content, otherwise returns the ToString() content of the enumeration value.
Use MyEnumDescriptionAttribute to describe SimpleStatus as follows:
1 public enum SimpleStatus
2 {
3 [MyEnumDescription(DefaultDescription="Default Desc of Value1")]
4 Value1 = 1,
5 [MyEnumDescription(DefaultDescription="Default Desc of Value2")]
6 Value2 = 2
7}
Run the page again, you will see that the information displayed corresponding to Value1 in the DropDownList is custom desc of Value1, and the information displayed corresponding to Value2 is Default Desc of Value2. Why? Because for Value1 we can get the custom information returned by MyEnumDescriptionAttribute, and for Value2, MyEnumDescriptionAttribute returns null, then the default description information will be applied. Isn't it amazing?
Even, you can mix and use different EnumDescriptionAttribute or its inherited classes to specify description information for different member items of the same Enum type (is there such a need^-^). However, only the first EnumDescriptionAttribute or its inherited class annotation will take effect for each enumeration item, and the redundant annotations will be ignored.
Okay, the basic introduction is over. I dare to call this plan a perfect plan. Just don’t throw rotten eggs. :)
addition
to using EnumDescriptionAttribute in conjunction with data-bound controls, you can also use EnumDescriptionAttribute alone to transparently obtain enumeration value description information. Calling the static method EnumDescriptionAttribute.GetDescriptions(enumType) can get the description information of all enumeration values of the specified enumeration type marked by EnumDescriptionAttribute or its inherited class.
The DropDownListField class is written with reference to the built-in CheckBoxField class of ASP.NET.
Download
all source code and sample code of the components introduced in this article are included in the latest version of nbear. EnumDescriptionAttribute is defined in srcNBear.CommonEnumDescriptionAttribute.cs, and DropDownListField is defined in srcNBear.Web.DataDropDownListField. The sample program is located in tutorialsNBearDataSourceSample.
It can be downloaded from the official website of NBear: http://nbear.org
-
[05/26 revision] - Added support for third-party enumeration descriptions and support for binary and over-the-counter enumeration values. Included in NBearV3.7.1 build 7 or above.
1. For enumeration types in third-party compiled assemblies, that is to say, we do not have the opportunity to directly add EnumDescriptionAttribute to the enumeration definition, it is now supported. You only need to define an additional enumeration type corresponding to the external enumeration type to ensure that the int values of the enumeration items of the two enumeration types are equal, and mark the EnumDescriptionAttribute in this new enumeration type. Of course, at this time, the EnumType attribute needs to be filled with a new enumeration type name.
2. Enumeration values that support binary AND are now also supported without additional settings. However, if the insert and edit of this enumeration value need to be updated to a merged value, the user still needs to write code to complete it. This only supports displaying such values.
//End of this article