I have a WebForms application that generates dynamic controls in the Page_Init method. One of the controls is an HtmlSelect control which I populate dynamically with items. When this renders on the page, I run the ususal jQuery code that sets up this control with a checkbox next to each list item.
$("select[id*='team_filter']").multiselect({
nonSelectedText: 'All',
numberDisplayed: 1,
nSelectedText: 'selected',
allSelectedText: 'All'
});
The user then checks some items and posts the page.
On the post back, on the Page_Load event I check the Selected properties of some items by using this code:
HtmlSelect ctrl = (HtmlSelect)this.FindControlRecursive("team_filter_1");
var firstGame = ctrl.Items.FindByValue("game_1").Selected;
var secondGame = ctrl.Items.FindByValue("game_2").Selected;
What's strange is that when the user checks multiple options, only the first one returns true on the Page_Load, all the rest return as false. It looks like the ViewState for all the other checked items don't get returned to the server.
Here is what I have in the Page_Init, remember these are dynamic controls, so I have to put them in the Init:
HtmlSelect teamfilter = new HtmlSelect();
teamfilter.ID = "team_filter_" + studioId;
teamfilter.Attributes.Add("class", "col-md-9");
teamfilter.Attributes.Add("multiple", "true");
ListItem listItem = new ListItem("All " + studioName, "studio_" + studioId);
listItem.Attributes.Add("data-type", "studio");
teamfilter.Items.Add(listItem);
listItem = new ListItem("All Games", "game_0_" + studioId);
listItem.Attributes.Add("data-type", "game");
teamfilter.Items.Add(listItem);
Can you try setting the Multiple property on the HtmlSelect to true