Home » .NET 4.0 » VB.NET – Limited user (checkbox) selection

VB.NET – Limited user (checkbox) selection

I found this script very helpful while designing surveys for my organization. This post applies to:
– MS SQL server 2005+
– VS2010
– .NET 4.0 Framework

Javascript code

<asp:content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <script type="text/javascript" language="javascript">
    function CheckCheck() {
        var chkBoxList = document.getElementById('< %=CheckBoxList1.ClientID %>'); 
                          var chkBoxCount = chkBoxList.getElementsByTagName("input");
        var btn = document.getElementById('< %=SubmitBtn.ClientID %>');
        var i = 0;
        var tot = 0;
        for (i = 0; i < chkBoxCount.length; i++) {
            if (chkBoxCount[i].checked) {
                tot = tot + 1;
        if (tot > 5)  {
            alert('Cannot check more than 5 check boxes'); btn.disabled = true;
        else {
            btn.disabled = false;

Form page

     Chose top five <%= rg %> for < %= Date.Today.Year %>:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" onclick="javascript:CheckCheck();" class="datagrid"> <asp:ListItem>American Cancer Society</asp:ListItem> <asp:ListItem>Big Brothers/Big Sisters of Central Iowa</asp:ListItem> <asp:ListItem>Blank Children’s Hospital</asp:ListItem> <asp:ListItem>Susan G Komen Breast Cancer Foundation</asp:ListItem> <asp:ListItem>American Lung Association of Iowa</asp:ListItem> <asp:ListItem>Habitat for Humanity</asp:ListItem> <asp:ListItem>March of Dimes</asp:ListItem> <asp:ListItem>Children and Families of Iowa</asp:ListItem> <asp:ListItem>Candeo</asp:ListItem> <asp:ListItem>Neveln Center</asp:ListItem> <asp:ListItem>Orchard Place</asp:ListItem> <asp:ListItem>Animal Rescue League of Iowa</asp:ListItem> </asp:CheckBoxList> <table> <tr> <td> Other <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td> </tr> <tr> <td><asp:Label ID="Label1" runat="server"></asp:Label></td> </tr> <tr> <td style="text-align: center;"> <asp:Button ID="ResetBtn" runat="server" Text="Reset" /> <asp:Button ID="SubmitBtn" runat="server" Text="Submit" /></td> </table>
Code behind

Imports System.IO
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Partial Class Docs_Apps_Surveys_EIC_Default
    Inherits System.Web.UI.Page
    Protected Sub ResetBtn_Click(sender As Object, e As System.EventArgs)_
                  _ Handles ResetBtn.Click
    End Sub
    Protected Sub SubmitBtn_Click(sender As Object, e As System.EventArgs)_
                  _ Handles SubmitBtn.Click
        'define controls
        Dim Other As TextBox

        'bind controls
        Other = SubmitBtn.FindControl("TextBox1")

        'define strings
        Dim dToday As String
        Dim cRegion As String
        Dim OtherEvent As String

        'do the work
        dToday = Date.Today.ToShortDateString
        cRegion = "Central"
        OtherEvent = Other.Text.ToString
        'define array strings
        Dim GroupEvent As New StringBuilder("")
        Dim chosen As Integer = 0
        Dim i As Integer = 0

        'create array
        For i = 0 To CheckBoxList1.Items.Count - 1
            If (CheckBoxList1.Items(i).Selected = True) Then
                chosen += 1
                If (chosen > 1) Then GroupEvent.Append(",")
            End If

        'do the sql staff
        If dToday = "" Then
            Label1.Text = "Date inputbox cannot be empty!"

            'create db connection string
            Dim SQLString As String
            Dim ConnString As String

            ConnString = "Data Source=my-super-srv;Initial_
                          _Catalog=surveys;Persist Security Info=True;_
                          _User ID=myUsr;Password=myPwd"

                'SQL statement
                SQLString = "INSERT into eic_charities(date, region, events, other)_
                              _ VALUES (@Date, @Region, @Events, @Other)"
                'write to SQL
                Dim SQLConn As New SqlConnection()
                Dim SQLCmd As New SqlCommand()

                SQLConn.ConnectionString = ConnString

                SQLCmd.Parameters.AddWithValue("@Date", dToday.ToString)
                SQLCmd.Parameters.AddWithValue("@Region", cRegion.ToString)
                SQLCmd.Parameters.AddWithValue("@Events", GroupEvent.ToString)
                SQLCmd.Parameters.AddWithValue("@Other", OtherEvent.ToString)
                SQLCmd.Connection = SQLConn
                SQLCmd.CommandText = SQLString

            Catch ex As Exception
                Label1.Text = "Error: " & ex.ToString()
            End Try
            Label1.Text = "<font color='green'>Success </font>- click 
            <a href='http://myHost.com/Charities.aspx?rg=Central'>here</a> 
            to continue."
        End If
    End Sub
End Class