Monday 21 January 2013

How to use special characters in enums in asp.net

aspx
<telerik:radgrid autogeneratecolumns="False" id="RadGrid1" onitemdatabound="RadGrid1_ItemDataBound" onneeddatasource="RadGrid1_NeedDataSource" runat="server">
            <mastertableview datakeynames="Rating" editmode="InPlace">
                <columns>
                    <telerik:gridboundcolumn datafield="Shipper" headertext="Shipper" uniquename="Shipper">
                    </telerik:gridboundcolumn>
                    <telerik:gridboundcolumn datafield="ShipperName" headertext="ShipperName" uniquename="ShipperName">
                    </telerik:gridboundcolumn>
                    <telerik:gridtemplatecolumn uniquename="Rating">
                        <itemtemplate>
                            <asp:label id="Label1" runat="server" text="<%# Eval(&quot;Rating&quot;) %>"></asp:label>
                        </itemtemplate>
                        <edititemtemplate>
                            <telerik:radcombobox id="RadComboBox1" runat="server">
                            </telerik:radcombobox>
                        </edititemtemplate>
                    </telerik:gridtemplatecolumn>
                    <telerik:grideditcommandcolumn>
                    </telerik:grideditcommandcolumn>
                </columns>
            </mastertableview>
</telerik:radgrid>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
      Class1.BindDropDownListWithEnum(ref DropDownList1, typeof(Enums.ShipperRating));
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Shipper", typeof(int));
        dt.Columns.Add("ShipperName", typeof(string));
        dt.Columns.Add("Rating", typeof(int));
        dt.Rows.Add("1", "ShipperName1", "1");
        dt.Rows.Add("2", "ShipperName2", "2");
        dt.Rows.Add("3", "ShipperName3", "2");

        RadGrid1.DataSource = dt;
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode && e.Item is GridEditableItem)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            RadComboBox RadComboBox1 = item.FindControl("RadComboBox1") as RadComboBox;
            int Rating = Convert.ToInt32(item.GetDataKeyValue("Rating"));
            Class1.BindRadComboBoxWithEnum(ref RadComboBox1, typeof(Enums.ShipperRating));

            if (RadComboBox1.Items.FindItemByValue(Convert.ToString(Rating)) != null)
            {
                RadComboBox1.Items.FindItemByValue(Convert.ToString(Rating)).Selected = true;
            }
        }
    }
Class1.cs
public class DisplayValue : Attribute
{
    private string _value;

   
    public DisplayValue(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}

public class EnumListItem
{
    private int _value;
    public int Value
    {
        get { return this._value; }
        set { this._value = value; }
    }

    private string _displayName;
    public string DisplayName
    {
        get { return _displayName; }
        set { _displayName = value; }
    }

    private string _name;
    public string Name
    {
        get { return _name.Replace("_", " "); }
        set { _name = value.Replace("_", " "); }
    }

    public EnumListItem(int Value, string Name, string DisplayName)
    {
        _value = Value;
        _displayName = string.IsNullOrEmpty(DisplayName) ? Name : DisplayName;
        _name = Name;
    }
}


public class Enums
{

    public enum ShipperRating
    {
        [DisplayValue("1-3 (Poor)")]
        Poor = 1,
        [DisplayValue("4-5 (Fair/Average)")]
        Average = 2,
        [DisplayValue("6-7 (Good)")]
        Good = 3

    }
}

public static class Class1
{
    public static void BindDropDownListWithEnum(ref DropDownList ddl, Type enumType)
    {
        List<enumlistitem> el = GetEnumValues(enumType, false);
        ddl.DataSource = el;
        ddl.DataTextField = "DisplayName";
        ddl.DataValueField = "Value";
        ddl.DataBind();
    }

    public static void BindRadComboBoxWithEnum(ref RadComboBox radcmb, Type enumType)
    {
        List<enumlistitem> el = GetEnumValues(enumType, true);
        radcmb.DataSource = el;
        radcmb.DataTextField = "DisplayName";
        radcmb.DataValueField = "Value";
        radcmb.DataBind();
    }
    public static List<enumlistitem> GetEnumValues(Type type, bool isSelectRequired)
    {
        List<enumlistitem> el = new List<enumlistitem>();
        EnumListItem ei;
        foreach (int item in Enum.GetValues(type))
        {
            ei = GetEnumItem(type, item);
            el.Add(ei);
        }
        if (isSelectRequired)
        {
            el.Insert(0, new EnumListItem(0, "--Select--", "--Select--"));
        }
        return el;
    }
    public static EnumListItem GetEnumItem(Type type, int item)
    {
        string name = Enum.GetName(type, item);
        string displayName = string.Empty;
        object[] displayAttributes = type.GetField(Enum.GetName(type, item)).GetCustomAttributes(typeof(DisplayValue), false);
        if (displayAttributes.Length > 0)
            displayName = ((DisplayValue)displayAttributes[0]).Value;
        else
            displayName = name.Replace("_", " ");

        
        return new EnumListItem(item, name, displayName);
    }
}

4 comments:

  1. Hi,
    I have a problem to implement a tooltip with combobox.
    the code that i have write is correct because it works for other application .but when i tried to my one application it will not work .
    I have add reference in config file,add script of Kendo and also add reference of kendo.mvc assembly what should be the problem ?


    Please reply its urgent.
    Thanks in advance
    !!
    My code for tool tip is:

    @(Html.Kendo().Tooltip()
    .For("#SelectedCountry")

    .Content("Select Country")
    )

    ReplyDelete
  2. Are you able to use other kendo controls in your page/project?
    Means you have the problem with the all kendo-ui control or only for this kendo tooltip ?

    ReplyDelete
  3. In addition also provide version detail of Kedoui.Dll and Kendo.Js.

    ReplyDelete
  4. Good article. Thanks for posting

    ReplyDelete