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?
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 selectchsn
with values. Make sure that you are binding the HTMLSelect under!IsPostBack