Chosen Select Selected Item Returns False

631 views Asked by At

I'm trying to get selected items from chosen select in server-side.

Here is my HTML:

<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<link href="Content/chosen.css" rel="stylesheet" />

<form id="form1" runat="server">
    <select id="chsn" runat="server" class="chzn-select" multiple="true" name="faculty" style="width: 200px;">
    </select>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <script src="Scripts/chosen.jquery.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".chzn-select").chosen();
            $(".chosen-select").chosen();
        });
    </script>
</form>

Here is server-side:

protected void Button1_Click(object sender, EventArgs e)
    {
        List<ListItem> tmpLst = new List<ListItem>();
        for (int i = 0; i < chsn.Items.Count; i++)
        {
            if (chsn.Items[i].Selected)
                tmpLst.Add(chsn.Items[i]);
        }
    }

chsn.Items[i].Selected always returns false. Is there a better way to get selected items?

2

There are 2 answers

0
codeandcloud On BEST ANSWER

The problem is not with your Button1_Click event. ( I have tested it ).

The problem must be with the Page_Load event where you bind the select chsn with values. Make sure that you are binding the HTMLSelect under !IsPostBack

0
waheed Asghar On

Naveen is right .. I am just answering this as i though you might not get Naveens point.

Everything in you pageload event. put it under this check

if(!isPostBack)
{
//here comes the code you have in pageload event.
}