locked
Get Selected Color in a dropdownlist, it is not a foreignk key RRS feed

  • Question

  • User-1759624489 posted

     Hello, I have a table in which I have a column of type char, and in that table I am saving color names

     

    I did all the user control and it works fine, except for the editing, I dont know how to set the selected value, I tried viewin the ForeignKey_edit control but it uses ForeignKey methods

     

    Take a look at this code.

     

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Collections.Specialized;

    /// <summary>
    /// This code is totally free and open. You may reuse it in any way you wish.
    /// </summary>

    public partial class ColorPicker : System.Web.DynamicData.FieldTemplateUserControl
    {
       

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] =DropDownList1.SelectedValue;

           
        }
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (Mode == DataBoundControlMode.Edit)
            {
               


            }
        }
        public System.Drawing.Color SelectedColor
        {
            get
            {
                if (DropDownList1.SelectedIndex > 0)
                {
                    //get the selected color
                    string SelectedColorStr = DropDownList1.SelectedValue;
                    //' conver the color string to System.drawing.color object
                    System.Drawing.ColorConverter converter = new System.Drawing.ColorConverter();
                    System.Drawing.Color objColor =
                        (System.Drawing.Color)converter.ConvertFromString(SelectedColorStr);
                    return objColor;
                }
                // if there is no color selected
                //  we return empty color object
                else
                {
                    return new System.Drawing.Color();
                }
            }
            set
            {
                string Color = value.Name;
                ListItem li = DropDownList1.Items.FindByValue(Color);
                // if found in the colors list , we select the matched color
                if (li != null)
                {
                    DropDownList1.ClearSelection();
                    li.Selected = true;
                }
            }

        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            //if (!IsPostBack)
                BindFillColorslist();
            //set the Background color of the listitem to the same color which the item represents
            SetCbackColorsForListItems();
        }
        private void SetCbackColorsForListItems()
        {
            foreach (ListItem li in DropDownList1.Items)
            {
                li.Attributes["style"] = " background-color: " + li.Value;
            }
        }
        // this function will fill the dropdownlist with the known colors
        private void BindFillColorslist()
        {
            // get the list of known colors
            System.Drawing.KnownColor[] colors =
                (System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor));
            //' add them to the drodownlist
            foreach (System.Drawing.KnownColor c in colors)
            {
                DropDownList1.Items.Add(c.ToString());
            }
        }

     
    }
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using Sodexo.Data;
    using System.Data.Linq;
    using System;

    namespace Sodexo.Entities
    {
        [MetadataType(typeof(KPIMetaData))]  
        public partial class KPI
        {

        }

        [DisplayName("Indicadores KPI")]
        public class KPIMetaData
        {
            public object Id { get; set; }

            [Required(ErrorMessage = "Este campo es Requerido")]
            [DisplayName("Nombre Kpi")]
            public object Name { get; set; }

            [DisplayName("Rango Inicial")]
            [DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]
            [Required(ErrorMessage = "Este campo es requerido.")]
            [Range(0, int.MaxValue, ErrorMessage = "El valor debe ser un entero positivo")]
            public object StartPercentage { get; set; }

            [DisplayName("Rango  Final")]
            [DataType("Int32", ErrorMessage = "El campo debe ser un entero positivo")]
            [Required(ErrorMessage = "Este campo es requerido.")]
            [Range(0, int.MaxValue, ErrorMessage = "El valor debe ser un entero positivo")]
            public object EndPercentage { get; set; }

            [UIHint("ColorPicker")]
            public object color { get; set; }



       
        }
    }
     

     

     

    Wednesday, September 17, 2008 12:26 PM

All replies

  • User-330204900 posted

    Hi Lavelencia, have a look a my article on FieldTemplate here: Your First FieldTemplate  look the DropDownList1_DataBound event hanler.

    The trick is to set it on or after OnDataBinding event of the FieldTemplate.

    hope this helps [:D]

    Wednesday, September 17, 2008 1:02 PM