2

Looked and found solutions with broken links or that did not work for me, that said I am using the multiselect found here http://abeautifulsite.net/blog/2008/04/jquery-multiselect/ I need to get the selected checkboxes when I hit a button to use them in c#, I am totally stuck on this one and the example used in the demo that does it is in php :-(

JQuery:

  $(document).ready(function () {
        $("#EGM").multiSelect({ selectAll: false, oneOrMoreSelected: '*' });
    });

Control:

 <select id="EGM" multiple="multiple" style="width: 100px;">
        <optgroup label="EGM">
         <option>data</option>           
        <option>driven</option>           
        <option>dropdown</option>      
        </optgroup>
        <optgroup  label="System Type">SystemType
         <option>data</option>          
         <option>driven</option>           
         <option>dropdown</option>          
         </optgroup>            
         </select>

Where I need my selected values in the code behind:

  protected void SubmitButton_Click(object sender, EventArgs e)
{

     //what goes here?!?

}
Contristo
  • 315
  • 1
  • 3
  • 17
  • You said 'control'. Is that HTML the HTML the control generated or have you manually written it? – Joshua H Sep 28 '11 at 02:05
  • there is another thread with the same question http://stackoverflow.com/questions/2700010/asp-net-jquery-multi-select-dropdownlist-get-selected-values – Agamand The True Sep 28 '11 at 02:17
  • @AgamandTheTrue, as I mentioned, I have checked for this here, if you go to your link and see the selected answer you can see the provided link there is broken as mentioned in the 1st sentence of my question. I really do try to find before I post. – Contristo Sep 28 '11 at 03:21

1 Answers1

2

The proper way to do this would be to change <select ...> to <asp:ListBox ...> and update your script to

$(document).ready(function() {
    $('#<%=this.EGM.ClientID%>').multiSelect(...);
});

But the <asp:ListBox> control doesn't natively support optgroup inner elements. There are workarounds using ControlAdapters (see: Dropdownlist control with <optgroup>s for asp.net (webforms)? - VB.NET). Using this, you can access the ListItems directly.

That said, a less proper yet less time consuming way, you could also just dump $('#EGM').val() into an <asp:HiddenField> by specifying a callback method in the multiSelect initializer and enumerate the array in the code behind.

Community
  • 1
  • 1
lukiffer
  • 11,025
  • 8
  • 46
  • 70