Ianna Ett Ianna Ett - 1 year ago 114
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>


and the javascript :

$(document).ready(function () {

and i have a html table like this:

<th >Something</th>
<th>Something2 </th>
<th >Something3 </th>
<th>Something4 </th>

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

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 Source

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" />

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download