0

aspx

<select id="ddColumns" name="ddColumns" runat="server" multiple style="width: 400px;"
    data-placeholder="All" class="chosen-select">
</select>

DLL

ddColumns.DataSource = AvailableColumns
ddColumns.DataTextField = "ColumnNames"
ddColumns.DataValueField = "ColumnNames"
ddColumns.DataBind()

The code that loops through each item in dropdown

For Each selection As ListItem In ddColumns.Items
    If selection.Selected Then
        ddGroupBy.Items.Add(selection.Value)
    End If
Next

Instead of looping through each item I need to loop through only the selected ones in the Placeholder dropdownlist.

Please advise.

Linta Sheelkumar
  • 195
  • 1
  • 5
  • 21
  • The problem is that it loops through all the items in the order in which they appear in the dropdown.If I select a value further down in the dropdown and then the first value ,it doesnot adds the first value to ddGroupBY and then the value further down.But I want them in the order of selection.The values in ddGroupBy is passed to a sql statement to group by in the order of selection. – Linta Sheelkumar Jan 22 '16 at 12:15
  • Thats the problem.It is not doing that.Is there a way to only loop through the selected items like ddcolumns.selecteditem.value something(Just for clarifiaction) – Linta Sheelkumar Jan 22 '16 at 12:19
  • It's not clear. What means "order of selection"? If you have three items and the user selects first item 2 and then item 1 you want that the items are added to the other ddl in the same order, so first item 2 and then item 1? Is that correct? If so, use a real `DropDownList` , set `AutoPostBack` to `True` and handle it's `SelectedIndexChanged` event. Then you can add always a single `ListItem` to the second ddl, the one which was selected at that time. – Tim Schmelter Jan 22 '16 at 12:19
  • So are yioou saying that it is not possible to loop through selected items in the order of selection in a placeholder? – Linta Sheelkumar Jan 22 '16 at 12:34
  • @TimSchmelter Yes thats the idea..I cannot change it to a dropdownlist because I need many other functionalities.I really need a solution for this.If you get any information please let me know.Thank You. – Linta Sheelkumar Jan 22 '16 at 15:39

1 Answers1

0

It might be a little easier to do this with Javascript:

var a1 = selectElem.getElementsByTagName('ddColumns'); 
var a2 = [];
for(var i=0; i<a1.length; i++) {
    if(a1[i].selected)
        //do something;
}
WedgeBuster53
  • 88
  • 1
  • 9