Ianna Ett Ianna Ett - 11 days ago 5
Javascript Question

How to access multiselect dropdownlist using asp.net c#

I have a dropdown list with multiselect like this:

<select id="ddMonths" multiple="multiple">
<option value="oneM" selected="selected"> OneMonth</option>
<option value="twoM" selected="selected">TwoMonths</option>
<option value="threM" selected="selected">ThreMonths</option>
<option value="fourM" selected="selected">FourMonths</option>
<option value="fiveM" selected="selected">FiveMonths</option>
<option value="SixM" selected="selected">SixMonths</option>

</select>


and the javascript :

$(document).ready(function () {
$('#ddMonths').multiselect();
});


and i have a html table like this:

<table>
<thead>
<tr>
<th >Something</th>
<th>Something1</th>
<th>Something2 </th>
<th >Something3 </th>
<th>Something4 </th>
</tr>

</thead>
<tbody id="trBoth">
<asp:Literal ID="allSomething" runat="server"></asp:Literal>
</tbody>
</table>


I populate the table using the literal in code behind.
I want to control the value of the table using the selected items from multiselect dropdownlist is it possible??? how can I access the selected items from code behind? I'm using asp.net C#. Please I need your help.

Answer

I hope in this case avoid DropDownList and use ListBox.

Take a look at the ListBox control to allow multi-select.

<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple">
    <asp:ListItem Text="One" Value="OneM" />
    <asp:ListItem Text="Two" Value="TwoM" />
    <asp:ListItem Text="Three" Value="ThreeM" />
</asp:ListBox> 

in the code behind

foreach(ListItem listItem in lblMultiSelect.Items)
{
   if (listItem.Selected == True)
   {
      var val = listItem.Value;
      var txt = listItem.Text; 
   }
}

Else go for Dropdown with checkboxes

jQuery Dropdown Check List

System.Web.UI.WebControls.CheckBoxList