Get selected item from asp:checkboxlist (C#) : CheckBoxList « Asp Control « ASP.Net






Get selected item from asp:checkboxlist (C#)

<script runat="server" language="C#">
  void Page_Load()
  {
    string msg = "You have selected the following items:<br />";

    if (check1.Items[0].Selected) { 
    msg = msg + check1.Items[0].Text + "<br />";
    }
    if (check1.Items[1].Selected) { 
    msg = msg + check1.Items[1].Text + "<br />";
    } 
    if (check1.Items[2].Selected) { 
    msg = msg + check1.Items[2].Text + "<br />";
    }
    
    Message.Text = msg;
  }    
</script>
<html>
<head>
  <title>Check Box List Example</title>
</head>
<body>
  <asp:label id="Message" runat="server" />
  <br /><br />
  Which city do you wish to look at hotels for?
  <br /><br />
  <form runat="server">
    <asp:checkboxlist id="check1" runat="server">
      <asp:listitem id="option1" runat="server" value="Madrid" />
      <asp:listitem id="option2" runat="server" value="Oslo" />
      <asp:listitem id="option3" runat="server" value="Lisbon" />
    </asp:checkboxlist>
    <br /><br />
    <input type="Submit">
  </form>
</body>
</html>

           
       








Related examples in the same category

1.Loop through asp:checkboxlist (VB.net)
2.Set asp:CheckBoxList Repeat Direction (VB.net)
3.Get selected value from asp:checkboxlist (VB.net)
4.Define and use asp checkboxlist in code behind (C#)