c# - Listbox in asp.net not getting selected items -
i have multiple dropdown & listbox in webpage.
i trying list of categoryid
lstcatid
listbox able populate listbox category name.
if remember correctly in first attempt code worked fine, after made change stated first item selected x no. of time
<asp:listbox id="lstcatid" runat="server" datatextfield="categoryname" datavaluefield="categoryid" selectionmode="multiple" cssclass="lstlistbox"> </asp:listbox> protected void button1_click(object sender, eventargs e) { string catid = string.empty; foreach (listitem li in lstcatid.items) { if (li.selected == true) { // response.write(); catid += lstcatid.selecteditem.value + ","; } } response.write(catid); }
i not sure going wrong checkd msdn show same way of doing it.
may doing wrong.
just add using firefox able see multiple selected value have selected property.
<option value="3" selected="selected">one</option> <option value="2">two</option> <option value="29" selected="selected">three</option> <option value="25" selected="selected">four</option> <option value="22" >five</option>
my output in case 3,3,3
i appreciate in regard
you setting same value every time:
foreach (listitem li in lstcatid.items) { if (li.selected == true) { // using lstcatid.selecteditem.value. catid += lstcatid.selecteditem.value + ","; } }
when want value of item in loop selected:
foreach (listitem li in lstcatid.items) { if (li.selected == true) { // value of item in loop catid += li.value + ","; } }
Comments
Post a Comment