{"id":1909,"date":"2012-01-31T18:46:45","date_gmt":"2012-01-31T22:46:45","guid":{"rendered":"http:\/\/www.iowawebnet.com\/ein\/?p=1909"},"modified":"2012-01-31T18:52:19","modified_gmt":"2012-01-31T22:52:19","slug":"vb-net-limited-user-checkbox-selection","status":"publish","type":"post","link":"https:\/\/www.iowawebnet.com\/ein\/2012\/01\/vb-net-limited-user-checkbox-selection\/","title":{"rendered":"VB.NET &#8211; Limited user (checkbox) selection"},"content":{"rendered":"<p>I found this script very helpful while designing surveys for my organization. This post applies to:<br \/>\n&#8211; MS SQL server 2005+<br \/>\n&#8211; VB.NET<br \/>\n&#8211; VS2010<br \/>\n&#8211; .NET 4.0 Framework<\/p>\n<pre>\r\nJavascript code\r\n<code>\r\n&lt;asp:content ID=\"Content1\" ContentPlaceHolderID=\"HeadContent\" Runat=\"Server\">\r\n    &lt;script type=\"text\/javascript\" language=\"javascript\">\r\n    function CheckCheck() {\r\n        var chkBoxList = document.getElementById('&lt; %=CheckBoxList1.ClientID %>'); \r\n                          var chkBoxCount = chkBoxList.getElementsByTagName(\"input\");\r\n        var btn = document.getElementById('&lt; %=SubmitBtn.ClientID %>');\r\n        var i = 0;\r\n        var tot = 0;\r\n        for (i = 0; i < chkBoxCount.length; i++) {\r\n            if (chkBoxCount[i].checked) {\r\n                tot = tot + 1;\r\n            }\r\n        }\r\n        if (tot > 5)  {\r\n            alert('Cannot check more than 5 check boxes'); btn.disabled = true;\r\n        }\r\n        else {\r\n            btn.disabled = false;\r\n        }\r\n    }\r\n&lt;\/script>\r\n&lt;\/asp:content>\r\n<\/code>\r\n<\/pre>\n<pre>\r\nForm page\r\n<code>\r\n     Chose top five &lt;%= rg %> for < %= Date.Today.Year %>:<br \/>\r\n       &lt;asp:CheckBoxList ID=\"CheckBoxList1\" runat=\"server\" \r\n             onclick=\"javascript:CheckCheck();\" class=\"datagrid\">\r\n            &lt;asp:ListItem>American Cancer Society&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Big Brothers\/Big Sisters of Central Iowa&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Blank Children\u2019s Hospital&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Susan G Komen Breast Cancer Foundation&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>American Lung Association of Iowa&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Habitat for Humanity&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>March of Dimes&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Children and Families of Iowa&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Candeo&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Neveln Center&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Orchard Place&lt;\/asp:ListItem>\r\n            &lt;asp:ListItem>Animal Rescue League of Iowa&lt;\/asp:ListItem>\r\n            &lt;\/asp:CheckBoxList> \r\n            &lt;table>\r\n                &lt;tr>\r\n                    &lt;td>\r\n                   Other &lt;asp:TextBox ID=\"TextBox1\" runat=\"server\">&lt;\/asp:TextBox>&lt;\/td>\r\n                &lt;\/tr>\r\n                &lt;tr>\r\n                    &lt;td>&lt;asp:Label ID=\"Label1\" runat=\"server\">&lt;\/asp:Label>&lt;\/td>\r\n                &lt;\/tr>\r\n                &lt;tr>\r\n                    &lt;td style=\"text-align: center;\">\r\n                    &lt;asp:Button ID=\"ResetBtn\" runat=\"server\" Text=\"Reset\" \/> \r\n                    &lt;asp:Button ID=\"SubmitBtn\" runat=\"server\" Text=\"Submit\" \/>&lt;\/td>\r\n                \r\n            &lt;\/table>\r\n<\/code>\r\n<\/pre>\n<pre>\r\nCode behind\r\n<code>\r\nImports System.IO\r\nImports System.Data.SqlClient\r\nImports System.Web.UI.WebControls\r\n\r\nPartial Class Docs_Apps_Surveys_EIC_Default\r\n    Inherits System.Web.UI.Page\r\n    Protected Sub ResetBtn_Click(sender As Object, e As System.EventArgs)_\r\n                  _ Handles ResetBtn.Click\r\n        Me.Server.Transfer(\"Central.aspx?rg=Charities\")\r\n    End Sub\r\n    Protected Sub SubmitBtn_Click(sender As Object, e As System.EventArgs)_\r\n                  _ Handles SubmitBtn.Click\r\n        'define controls\r\n        Dim Other As TextBox\r\n\r\n        'bind controls\r\n        Other = SubmitBtn.FindControl(\"TextBox1\")\r\n\r\n        'define strings\r\n        Dim dToday As String\r\n        Dim cRegion As String\r\n        Dim OtherEvent As String\r\n\r\n        'do the work\r\n        dToday = Date.Today.ToShortDateString\r\n        cRegion = \"Central\"\r\n        OtherEvent = Other.Text.ToString\r\n        \r\n        'define array strings\r\n        Dim GroupEvent As New StringBuilder(\"\")\r\n        Dim chosen As Integer = 0\r\n        Dim i As Integer = 0\r\n\r\n        'create array\r\n        For i = 0 To CheckBoxList1.Items.Count - 1\r\n            If (CheckBoxList1.Items(i).Selected = True) Then\r\n                chosen += 1\r\n                If (chosen > 1) Then GroupEvent.Append(\",\")\r\n                GroupEvent.Append(CheckBoxList1.Items(i).Text)\r\n            End If\r\n        Next\r\n\r\n        'do the sql staff\r\n        If dToday = \"\" Then\r\n            Label1.Text = \"Date inputbox cannot be empty!\"\r\n        Else\r\n\r\n            'create db connection string\r\n            Dim SQLString As String\r\n            Dim ConnString As String\r\n\r\n            ConnString = \"Data Source=my-super-srv;Initial_\r\n                          _Catalog=surveys;Persist Security Info=True;_\r\n                          _User ID=myUsr;Password=myPwd\"\r\n\r\n            Try\r\n                'SQL statement\r\n                SQLString = \"INSERT into eic_charities(date, region, events, other)_\r\n                              _ VALUES (@Date, @Region, @Events, @Other)\"\r\n                'write to SQL\r\n                Dim SQLConn As New SqlConnection()\r\n                Dim SQLCmd As New SqlCommand()\r\n\r\n                SQLConn.ConnectionString = ConnString\r\n                SQLConn.Open()\r\n\r\n                SQLCmd.Parameters.AddWithValue(\"@Date\", dToday.ToString)\r\n                SQLCmd.Parameters.AddWithValue(\"@Region\", cRegion.ToString)\r\n                SQLCmd.Parameters.AddWithValue(\"@Events\", GroupEvent.ToString)\r\n                SQLCmd.Parameters.AddWithValue(\"@Other\", OtherEvent.ToString)\r\n                SQLCmd.Connection = SQLConn\r\n                SQLCmd.CommandText = SQLString\r\n                SQLCmd.ExecuteNonQuery()\r\n                SQLConn.Close()\r\n\r\n            Catch ex As Exception\r\n                Label1.Text = \"Error: \" & ex.ToString()\r\n            End Try\r\n            Label1.Text = \"&lt;font color='green'>Success &lt;\/font>- click \r\n            &lt;a href='http:\/\/myHost.com\/Charities.aspx?rg=Central'>here&lt;\/a> \r\n            to continue.\"\r\n        End If\r\n    End Sub\r\nEnd Class\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I found this script very helpful while designing surveys for my organization. This post applies to: &#8211; MS SQL server 2005+ &#8211; VB.NET &#8211; VS2010 &#8211; .NET 4.0 Framework Javascript code &lt;asp:content ID=&#8221;Content1&#8243; ContentPlaceHolderID=&#8221;HeadContent&#8221; Runat=&#8221;Server&#8221;> &lt;script type=&#8221;text\/javascript&#8221; language=&#8221;javascript&#8221;> function CheckCheck() { var chkBoxList = document.getElementById(&#8216;&lt; %=CheckBoxList1.ClientID %>&#8217;); var chkBoxCount = chkBoxList.getElementsByTagName(&#8220;input&#8221;); var btn = document.getElementById(&#8216;&lt; [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,17,16,52,43,32,67,81,84],"tags":[82,83],"class_list":["post-1909","post","type-post","status-publish","format-standard","hentry","category-net-4-0","category-applications","category-net","category-controls","category-javascript-2","category-ms-sql","category-sql","category-survey-form","category-vs2010","tag-checkbox","tag-checkboxlist"],"_links":{"self":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1909"}],"collection":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/comments?post=1909"}],"version-history":[{"count":24,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1909\/revisions"}],"predecessor-version":[{"id":1934,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1909\/revisions\/1934"}],"wp:attachment":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/media?parent=1909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/categories?post=1909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/tags?post=1909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}