开发者

Getting Selected values from Jquery multiselct in asp.net

开发者 https://www.devze.com 2023-04-09 10:02 出处:网络
Looked a开发者_如何学Pythonnd 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/

Looked a开发者_如何学Pythonnd 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?!?

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号