Posted by Manoj Garg on August 7, 2008
Suppose we have a situation where we want to bind an DropDown List with the values from an Enum. But we want to show different text for each enum member. for example we have following enum containing various actions a user can perform in a source control tool like check in, check out etc.
1: public enum Actions
3: [Description("Check in")]
5: [Description("Check out")]
7: [Description("Undo Check out")]
9: [Description("View File")]
Now on our page we want to show the text specified in Description attribute and the actual enum member name as its value. Following is an elegant way to do it in DotNet 2.0 using reflection.
1: class ActionItems
3: public string Text;
4: public string Value;
6: public ActionItems(string _text, string _value)
8: Text = _text;
9: Value = _value;
13: public List<ActionItems> GetActionListItems(Type EnumType)
15: List<ActionItems> items = new List<ActionItems>();
16: foreach (string value in Enum.GetNames(EnumType))
18: /// Get field info
19: FieldInfo fi = EnumType.GetField(value);
21: /// Get description attribute
22: object descriptionAttrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
23: DescriptionAttribute description = (DescriptionAttribute)descriptionAttrs;
25: ActionItems item = new ActionItems(description.Description, value);
28: return items;
The method above GetActionListItems returns a list of type ActionItems which contains two fields Text and Value. This method uses Enum.GetNames method to get all the members in a given enum type. GetField method of type is used to get the information about that member. This method return an object of type FieldInfo. Using this FieldInfo object we can get the values of various custom attributes defined for that enum using GetCustomAttributes method.
1: List<ActionItems> comboItems = GetActionListItems(typeof(Actions));
The above code can be used to call GetActionListItems method.
Hope it helps.
Posted in C# | Tagged: Description Custom Attribute, Enum, Reflection | 5 Comments »
Posted by Manoj Garg on August 6, 2008
Sometimes we need to display images along with the error message in the validators in ASP.net. There is an easy way to do it. Validators in ASP.NET have two properties “ErrorMessage” and “Text”. ErrorMessage is the property that is displayed when validation specified by the validator doesn’t pass i.e it is the error message related to an invalid condition. “Text” property is the Text to display in this control if invalid.
So following is the code that can be used to display an Image next to the text box that fails validation and an image with text in the validation summary control.
1: <asp:TextBox ID="txtUID" runat="server" />
2: <asp:RequiredFieldValidator id="txtUIDValidatorReq" runat="server" ControlToValidate="txtUID"
Display="Dynamic" Text="<img src='../images/icons/error_message.gif' />"
ErrorMessage="<img src='../images/icons/error_message.gif' /> Please enter a User ID">
In the above code, There is a textbox for entering the UserID. A RequireFieldValidator txtUIDValidatorReq is associated with this textbox to check that userid is not empty. Following image shows the outcome of the above code if the user submits the page without entering the UserID.
For a detailed discussion of ASP.NET Validation Controls, you can refer links below..
ASP.NET Validators Unclouded By Paul Riley
Input Validation With ASP.NET by Harish Kamath Melonfire
Posted in ASP.Net | Tagged: Validation in ASP.NET | 1 Comment »