views:

121

answers:

2

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.

+1  A: 

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());
        }
anishmarokey
+1  A: 

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.

WVDominick
Thanks . u r RT
anishmarokey
You're solution will work if they just want to collect the string values, but it was a little unclear what they needed so mine just covers whatever they need to do.
WVDominick