I am using SelectedIndexChanged for Checkboxlist, need to get the value of selected item but I am not able to get it. Provide a appropriate solution for it.
In this scenario, consider there are 3 items in checkbox list, items 1 and 3 are already selected , if I am selecting item 2 , I need to get the value of the currently selected item or how will I know which item is selected now.
ListItem li = this.CheckBoxList1.Items.FindByText(this.HiddenField1.Value);
if (li != null)
{
Response.Write("Selected Item Value Is :" + li.Value);
Response.Write("</br>");
Response.Write("Selected Item Text Is :" + li.Text);
}
Checckboxlist itema are bound dynamically, I moved the this.CheckBoxList1.Items[index].Attributes.Add("onclick", "StoreCheckedItem(this)"); next to databind that resolved the issue,
Thanks very much for the quick response
Bala
Marked as answer by rbmbala on Nov 17, 2012 06:41 AM
rbmbala
Member
8 Points
30 Posts
Find which CheckBoxlist item has been clicked
Nov 17, 2012 03:46 AM|LINK
I am using SelectedIndexChanged for Checkboxlist, need to get the value of selected item but I am not able to get it. Provide a appropriate solution for it.
Thanks
Bala
shivalthakur
Participant
1863 Points
542 Posts
Re: Find which CheckBoxlist item has been clicked
Nov 17, 2012 03:53 AM|LINK
or past your code.
Response.Write("Success");
Best Of Luck
Shival Thakur
rbmbala
Member
8 Points
30 Posts
Re: Find which CheckBoxlist item has been clicked
Nov 17, 2012 04:28 AM|LINK
In this scenario, consider there are 3 items in checkbox list, items 1 and 3 are already selected , if I am selecting item 2 , I need to get the value of the currently selected item or how will I know which item is selected now.
karthicks
All-Star
32178 Points
5537 Posts
Re: Find which CheckBoxlist item has been clicked
Nov 17, 2012 05:12 AM|LINK
hi, refer below code
<script> function StoreCheckedItem(checkBoxItem) { document.getElementById('<%= HiddenField1.ClientID %>').value = checkBoxItem.parentElement.children[1].innerText; } </script> </head> <body> <form id="form1" runat="server"> <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"> <asp:ListItem>Item1</asp:ListItem> <asp:ListItem>Item2</asp:ListItem> </asp:CheckBoxList> <asp:HiddenField ID="HiddenField1" runat="server" /> </form> </body> protected void Page_Load(object sender, EventArgs e) { for (int index = 0; index < this.CheckBoxList1.Items.Count; index++) this.CheckBoxList1.Items[index].Attributes.Add("onclick", "StoreCheckedItem(this)"); }ListItem li = this.CheckBoxList1.Items.FindByText(this.HiddenField1.Value); if (li != null) { Response.Write("Selected Item Value Is :" + li.Value); Response.Write("</br>"); Response.Write("Selected Item Text Is :" + li.Text); }Karthick S
rbmbala
Member
8 Points
30 Posts
Re: Find which CheckBoxlist item has been clicked
Nov 17, 2012 06:02 AM|LINK
rbmbala
Member
8 Points
30 Posts
Re: Find which CheckBoxlist item has been clicked
Nov 17, 2012 06:40 AM|LINK
Checckboxlist itema are bound dynamically, I moved the this.CheckBoxList1.Items[index].Attributes.Add("onclick", "StoreCheckedItem(this)"); next to databind that resolved the issue,
Thanks very much for the quick response
Bala