简体   繁体   中英

How do I setup a multi value dropdownlist from a list collection

How to setup a multi value dropdownlist from a list collection...

Datasource: Listcollection which contains ColorCode and Description...

how to I setup a dropdown with Colorcode-Description like ORG-orange...

then how to capture these selected value as colorcode only by removing description for update purpose...

Now I am doing like this...

ddl.datesource=list<datasetvalues> // ...contains (colorcode, description)

ddl.DataTextField = "ColorCode";
ddl.DataValueField = "ColorCode";
ddl.databind();

then selected value should be like this...

ddlcolor.Items.FindByValue((DataBinder.Eval(formView1.DataItem,
        "colorCode").ToString())).Selected = true;

for update:

ClassA.Color= ddl.selectedvalue();

Now what I need change to in the above code to get the combination of both..otherwise i need have textbox for description which syncs with ddl..which is bit complex for my level of programming...thanks..

There are a couple of solutions as per my knowlege.

1) You can concatenate the text like : Code + "-" + Value, while preparing the list using a For/Foreach... loop

2) If it is permitted as per your project, you may also consider overriding the string inside the entity but the selected value will be with a hyphenated(with a - inbetween code & value) string, which you need to string split in the code behind.

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                List<CodeValue> colors = new List<CodeValue> {new CodeValue{Code="",Value="Select"},new CodeValue{Code="RD",Value="Red"},
            new CodeValue{Code="BL",Value="Blue"}};
                ddlColors.DataSource = colors;
                ddlColors.DataBind();
            }
        }

        protected void btnClick_Click(object sender, EventArgs e)
        {
            var item = ddlColors.SelectedValue;
            var code = item.Split('-');
        }
    }

    class CodeValue
    {
        public string Code { get; set; }
        public string Value { get; set; }
        public override string ToString()
        {
            return this.Code + "-" + this.Value;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM