1
votes

I have the list box control (ASP.NET Control On aspx page, language C# ). It has collection of integers as Items:

100, 200, 300, 400, 500, 600, 700. ok ?

I randomly select the list items at run time. as :

200, 500, 100, 300. Ok?

I want this sequence of selected list in List collection. How can i do this ? Please guide me.

2

2 Answers

1
votes

A little modification to anishmarokey's answer. If you want to actually store the ListItem in a List collection instead of just the string:

 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       List<ListItem> list = new List<ListItem>();
       list.Add(ListBox1.SelectedItem);
       string testValue = list[0].Value; // this is how you access a listitem in the List
    }

If you do this you can work with the actual listbox item rather than just the number selected. And if you want to keep the value in "list" after each postback you can put "list" in a session object or cache.

1
votes

in selected index change . you can add .

 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
 List<ListItem> list = new List<ListItem>();
                list.Add(ListBox1.SelectedValue.ToString());
        }