I'm thinking that you may be doing overkill by using code behind to do this checking. The easiest way would be add a validation control to each one of these checkbox lists you want to verify. Unless there is some justification for doing this in code behind.
Let me know ok?
I see what you're trying to do and it makes sense. You are trying to take the total of all the checkbox lists and adding them up for some type of survey. In that case, doing the validation in code behind may be a good way to handle it. Let me take this into
my lab and I'll be back.
I am also a proponent of using jQuery/JavaScript for client side validation. However, here is VB code behind to check your 15 RadioButtonLists to see which ones have been selected. Just in case you want to do VB code:
Sub ShowButtonCijfer(ByVal s As Object, ByVal e As EventArgs)
Dim rbSelected(14) As Boolean, rbSelectCount As Int16
rbSelectCount = 0
For i As Int16 = 0 To 14
Dim rblin As RadioButtonList = DirectCast(panel1.FindControl("RadioButtonList" & (i + 1).ToString), RadioButtonList)
chkRBLists(rblin, i)
Next
If rbSelectCount = 15 Then
panel1.Visible = False
panel2.Visible = True
Cijfer.Text = (((9 / 15) * ((Convert.ToInt32(RadioButtonList1.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList2.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList3.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList4.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList5.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList6.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList7.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList8.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList9.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList10.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList11.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList12.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList13.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList14.SelectedItem.Value)) +
(Convert.ToInt32(RadioButtonList15.SelectedItem.Value))))) +
1
Else
Cijfer.Text = ""
For I As Int16 = 0 To 14
Cijfer.Text += "RadiobuttonList" & (I + 1).ToString & rbSelected(I).ToString & "<br />"
Next
End If
End Sub
Protected Sub chkRBLists(ByVal rblistIn As RadioButtonList, ByVal idx As Integer)
If rblistIn.SelectedIndex <> -1 Then
rbSelected(idx) = True
rbSelectCount += 1
End If
End Sub
This counts the lists with selections and also keeps track of the state of each one.
Januz
Member
177 Points
69 Posts
Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 09:33 AM|LINK
Hi there!
I'm using radiobuttonlists and I want to check if in every list a radiobutton is checked.
If not checked the panels stay as they are and a message appears.
If checked the panels change and the outcome appears.
Can't be that hard i thougt... Well I can't seem to get it right and I tried numerous things.
The whole thing works, the part missing is checking the radiobuttonlists...
Here's my code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Technieklokaal.nl - Gereedschappen Toets 1" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLEDB" %>
<script runat="server">
Dim conTechnieklokaal As OLEDBConnection
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Technieklokaal.mdb;"
Dim cmdSelectGSToets1Load As OLEDBCommand
Dim OLEDBstrLoad As String = "Select AfbeeldingUrl From Gereedschappen Where Toets1=-1 Order By Naam ASC"
Dim dtrGSToets1 As OLEDBDataReader
Sub Page_Load()
conTechnieklokaal = New OLEDBConnection (connstr)
conTechnieklokaal.open()
cmdSelectGSToets1Load = New OLEDBCommand(OLEDBstrLoad, conTechnieklokaal)
dtrGSToets1 = cmdSelectGSToets1Load.ExecuteReader()
rptGSToets1.DataSource=dtrGSToets1
rptGSToets1.DataBind()
dtrGSToets1.Close()
conTechnieklokaal.Close()
End sub
Sub ShowButtonCijfer (s As Object, e As EventArgs)
panel1.visible=false
panel2.visible=true
Cijfer.Text = (((9/15)*((Convert.ToInt32(radiobuttonlist1.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist2.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist3.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist4.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist5.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist6.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist7.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist8.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist9.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist10.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist11.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist12.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist13.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist14.SelectedItem.Value)) +
(Convert.ToInt32(radiobuttonlist15.SelectedItem.Value)))))+
1
End sub
</script>
<asp:Content ContentPlaceHolderID="Titel" Runat="server">
<font class="headertext">Gereedschappen - Toets 1</font>
</asp:Content>
<asp:Content ContentPlaceHolderID="Body" Runat="server">
<asp:Panel id="panel1" runat="server">
<table class="tabletoets">
<tr>
<td class="tdtoetsAfb">
<asp:Repeater id="rptGSToets1" runat="server">
<ItemTemplate>
<img src="<%#Convert.ToString(Container.DataItem("AfbeeldingURL")) %>" class="image2" /><br/>
</ItemTemplate>
</asp:Repeater>
</td>
<td>
<table >
<tr>
<td class="tdtoetsVrg">
Vraag 1
<p/>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem id="vraag1a" Value="0">Klauwhamer</asp:ListItem>
<asp:ListItem Value="1">Bankhamer</asp:ListItem>
<asp:ListItem Value="0">Houten hamer</asp:ListItem>
<asp:ListItem Value="0">Penhamer</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 2
<p/>
<asp:RadioButtonList ID="RadioButtonList2" runat="server">
<asp:ListItem Value="1">Combinatietang</asp:ListItem>
<asp:ListItem Value="0">Zijkniptang</asp:ListItem>
<asp:ListItem Value="0">Waterpomptang</asp:ListItem>
<asp:ListItem Value="0">Striptang</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 3
<p/>
<asp:RadioButtonList ID="RadioButtonList3" runat="server">
<asp:ListItem Value="0">Combinatietang</asp:ListItem>
<asp:ListItem Value="0">Zijkniptang</asp:ListItem>
<asp:ListItem Value="0">Waterpomptang</asp:ListItem>
<asp:ListItem Value="1">Striptang</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 4
<p/>
<asp:RadioButtonList ID="RadioButtonList4" runat="server">
<asp:ListItem Value="0">Houtbeitel</asp:ListItem>
<asp:ListItem Value="1">Hobbymes (Stanleymes)</asp:ListItem>
<asp:ListItem Value="0">Breekmes</asp:ListItem>
<asp:ListItem Value="0">Kunststofsnijder (Dicut)</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 5
<p/>
<asp:RadioButtonList ID="RadioButtonList5" runat="server">
<asp:ListItem Value="1">Houtbeitel</asp:ListItem>
<asp:ListItem Value="0">Hobbymes (Stanleymes)</asp:ListItem>
<asp:ListItem Value="0">Breekmes</asp:ListItem>
<asp:ListItem Value="0">Kunststofsnijder (Dicut)</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 6
<p/>
<asp:RadioButtonList ID="RadioButtonList6" runat="server">
<asp:ListItem Value="0">Klauwhamer</asp:ListItem>
<asp:ListItem Value="0">Bankhamer</asp:ListItem>
<asp:ListItem Value="1">Houten hamer</asp:ListItem>
<asp:ListItem Value="0">Penhamer</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 7
<p/>
<asp:RadioButtonList ID="RadioButtonList7" runat="server">
<asp:ListItem Value="0">Handzaag</asp:ListItem>
<asp:ListItem Value="0">Kapzaag</asp:ListItem>
<asp:ListItem Value="0">Toffelzaag</asp:ListItem>
<asp:ListItem Value="1">Metaalzaag junior</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 8
<p/>
<asp:RadioButtonList ID="RadioButtonList8" runat="server">
<asp:ListItem Value="0">Handzaag & Kapzaag</asp:ListItem>
<asp:ListItem Value="1">Kapzaag & Toffelzaag </asp:ListItem>
<asp:ListItem Value="0">Handzaag & Toffelzaag</asp:ListItem>
<asp:ListItem Value="0">Kapzaag & Metaalzaag junior</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 9
<p/>
<asp:RadioButtonList ID="RadioButtonList9" runat="server">
<asp:ListItem Value="1">Klauwhamer</asp:ListItem>
<asp:ListItem Value="0">Bankhamer</asp:ListItem>
<asp:ListItem Value="0">Houten hamer</asp:ListItem>
<asp:ListItem Value="0">Penhamer</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 10
<p/>
<asp:RadioButtonList ID="RadioButtonList10" runat="server">
<asp:ListItem Value="0">Veiligheidsliniaal</asp:ListItem>
<asp:ListItem Value="1">Blokhaak</asp:ListItem>
<asp:ListItem Value="0">Stalen meetlat</asp:ListItem>
<asp:ListItem Value="0">Zweihaak</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 11
<p/>
<asp:RadioButtonList ID="RadioButtonList11" runat="server">
<asp:ListItem Value="0">Veiligheidsliniaal</asp:ListItem>
<asp:ListItem Value="0">Blokhaak</asp:ListItem>
<asp:ListItem Value="1">Stalen meetlat</asp:ListItem>
<asp:ListItem Value="0">Zweihaak</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 12
<p/>
<asp:RadioButtonList ID="RadioButtonList12" runat="server">
<asp:ListItem Value="0">Kraspen</asp:ListItem>
<asp:ListItem Value="0">Centerpunt</asp:ListItem>
<asp:ListItem Value="0">Kruishout</asp:ListItem>
<asp:ListItem Value="1">Steekpasser</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 13
<p/>
<asp:RadioButtonList ID="RadioButtonList13" runat="server">
<asp:ListItem Value="0">Houtschaaf</asp:ListItem>
<asp:ListItem Value="0">Rasp</asp:ListItem>
<asp:ListItem Value="1">Vijl</asp:ListItem>
<asp:ListItem Value="0">Schuurpapier</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 14
<p/>
<asp:RadioButtonList ID="RadioButtonList14" runat="server">
<asp:ListItem Value="0">Combinatietang</asp:ListItem>
<asp:ListItem Value="1">Zijkniptang</asp:ListItem>
<asp:ListItem Value="0">Waterpomptang</asp:ListItem>
<asp:ListItem Value="0">Kniptang</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="tdtoetsVrg">
Vraag 15
<p/>
<asp:RadioButtonList ID="RadioButtonList15" runat="server">
<asp:ListItem Value="0">Veiligheidsliniaal</asp:ListItem>
<asp:ListItem Value="0">Blokhaak</asp:ListItem>
<asp:ListItem Value="0">Stalen meetlat</asp:ListItem>
<asp:ListItem Value="1">Zweihaak</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p/>
<asp:Button id="ButtonCijfer" onclick="ShowButtonCijfer" runat="server" class="btn1" Text="Cijfer"></asp:Button>
<asp:Label id="lblbericht1" runat="server" text=""></asp:Label>
</asp:Panel>
<asp:Panel id="panel2" runat="server">
<asp:Label id="Cijfer" runat="server"></asp:Label>
</asp:Panel></asp:Content>
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:07 AM|LINK
I'm thinking that you may be doing overkill by using code behind to do this checking. The easiest way would be add a validation control to each one of these checkbox lists you want to verify. Unless there is some justification for doing this in code behind. Let me know ok?
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:11 AM|LINK
I see what you're trying to do and it makes sense. You are trying to take the total of all the checkbox lists and adding them up for some type of survey. In that case, doing the validation in code behind may be a good way to handle it. Let me take this into my lab and I'll be back.
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:14 AM|LINK
Ok, you can accomplish this much easier by using JQuery. Take a look at this link for the code:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Januz
Member
177 Points
69 Posts
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:27 AM|LINK
Well then it seems I have to take my first steps in using en understanding JQuery...
Was hoping there would be a simple vb.net solution.
Thx for helping out!
Januz
Member
177 Points
69 Posts
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:29 AM|LINK
btw.. Made me smile with "Your lab"... funny
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:29 AM|LINK
Well, you could do it via vb.net but it would require more code and JQuery is much faster for validation routines.
Januz
Member
177 Points
69 Posts
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 11:36 AM|LINK
Conclusion... Jquery is worth putting some time into
thx!
paindaasp
Star
12092 Points
2035 Posts
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 12:18 PM|LINK
I am also a proponent of using jQuery/JavaScript for client side validation. However, here is VB code behind to check your 15 RadioButtonLists to see which ones have been selected. Just in case you want to do VB code:
Sub ShowButtonCijfer(ByVal s As Object, ByVal e As EventArgs) Dim rbSelected(14) As Boolean, rbSelectCount As Int16 rbSelectCount = 0 For i As Int16 = 0 To 14 Dim rblin As RadioButtonList = DirectCast(panel1.FindControl("RadioButtonList" & (i + 1).ToString), RadioButtonList) chkRBLists(rblin, i) Next If rbSelectCount = 15 Then panel1.Visible = False panel2.Visible = True Cijfer.Text = (((9 / 15) * ((Convert.ToInt32(RadioButtonList1.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList2.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList3.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList4.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList5.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList6.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList7.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList8.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList9.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList10.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList11.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList12.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList13.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList14.SelectedItem.Value)) + (Convert.ToInt32(RadioButtonList15.SelectedItem.Value))))) + 1 Else Cijfer.Text = "" For I As Int16 = 0 To 14 Cijfer.Text += "RadiobuttonList" & (I + 1).ToString & rbSelected(I).ToString & "<br />" Next End If End Sub Protected Sub chkRBLists(ByVal rblistIn As RadioButtonList, ByVal idx As Integer) If rblistIn.SelectedIndex <> -1 Then rbSelected(idx) = True rbSelectCount += 1 End If End SubThis counts the lists with selections and also keeps track of the state of each one.
Januz
Member
177 Points
69 Posts
Re: Trouble checking if radiobutton is checked in radiobuttonlist
Oct 17, 2011 12:39 PM|LINK
Well Thank you very much!
I'm going to implement your code first and later on give jQuery a chance...
I was already playing around with jQuery but i'm stubmling on a lot of questions at this time.