views:

39

answers:

1

I have a list box which is populated using a dictioanry. When I iterate throught the selected items using the following code, it always show only the first items as selected - even if the first item is not selected.

Have you ever encountered this scenario?

Could you please help on this?

This problem occurs when I use dictionary for binding. On the other hand a generic list works fine.

 private void PopulateListBox2()
    {
        List<string> subjectList = new List<string>();
        subjectList.Add("Maths");
        subjectList.Add("Science");

        ListBox1.DataSource = subjectList;
        ListBox1.DataBind();
    }

Even it will work fine, if the values are unique. But in my scenario, the values are same; only key varies. The following works

 private void PopulateListBox5()
    {
        Dictionary<string, string> resultDictionary = new Dictionary<string, string>();

        resultDictionary.Add("Maths", "Lijo1");
        resultDictionary.Add("Science", "Lijo2");

        ListBox1.DataValueField = "Value";
        ListBox1.DataTextField = "Key";

        ListBox1.DataSource = resultDictionary;
        ListBox1.DataBind();
    }

^^^^^^^^^^^^^^^^^^^ The following code has the problem.

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulateListBox1();
            ListBox1.SelectionMode = ListSelectionMode.Multiple;
        }
    }

    private void PopulateListBox1()
    {
        Dictionary<string, string> resultDictionary = new Dictionary<string, string>();

        resultDictionary.Add("Maths", "Lijo");
        resultDictionary.Add("Science", "Lijo");

        ListBox1.DataValueField = "Value";
        ListBox1.DataTextField = "Key";

        ListBox1.DataSource = resultDictionary;
        ListBox1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        MakeList1Items();
    }

    private void MakeList1Items()
    {
        string test = null;

        foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected == true)
            {
                if(string.IsNullOrEmpty(test))
                {
                    test=item.Text;
                }
                else
                {
                    test = test +", " + item.Text;
                }

            }
        } 
        Response.Write(test);
    }

}

Thanks

Lijo

A: 

You have the DataValueField and DataTextField of the ListBox the wrong way around. I'm not sure why, but they must be like this:

ListBox1.DataValueField = "Key";
ListBox1.DataTextField = "Value";

So, in reality, you are best not using a Dictionary for for this kind of binding. Maybe try a List<KeyValuePair<string, string>> instead.

Dan Diplo
No. It did not work. I have updated the original question with more details.
Lijo
It **does** work. I copied your code, switched the "Key" and "Value" around, and both were output. They need to be that way around.
Dan Diplo