On the aspx page you have a list:
<asp:CheckBoxList ID="YrChkBox" runat="server" onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList> <asp:Button ID="button" runat="server" Text="Submit" />
In your code behind the aspx.cs page, you have the following:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Populate the CheckBoxList items only when it not a postback. YrChkBox.Items.Add(new ListItem("Item 1", "Item1")); YrChkBox.Items.Add(new ListItem("Item 2", "Item2")); } } protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e) { // Create the list to store. List<String> YrStrList = new List<string>(); // Loop through each item. foreach (ListItem item in YrChkBox.Items) { if (item.Selected) { // If the item is selected, add the value to the list. YrStrList.Add(item.Value); } else { // Item is not selected, do something else. } } // Join the string together using the ; delimiter. String YrStr = String.Join(";", YrStrList.ToArray()); // Write to the page the value. Response.Write(String.Concat("Selected Items: ", YrStr)); }
Make sure you use the if (!IsPostBack) { } condition, because if you load it on every updated page, it actually destroys the data.
Walk Mar 01 2018-12-12T00: 00Z
source share