Addition - Help!

  • Thread starter Thread starter Eirik Røthe
  • Start date Start date
E

Eirik Røthe

What's wrong? I am trying to add 2 radiobuttonvalues.

Any idea?





<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script language="vb" runat="server">

Sub rbl1b_OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim Int1a as integer
Dim Int1b as integer

Dim RadioButtonListla as integer
Dim RadioButtonListlb as integer

Int1a = RadioButtonListla.SelectedItem.Text
Int1b = RadioButtonListlb.SelectedItem.Text

lblLabel1.Text = Int1a + Int1b
End Sub

</script>

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body><form action="" method="post" name="form1" runat="server">
<table width="606" border="0">
<tr>
<td width="10">&nbsp; </td>
<td width="185">
<asp:RadioButtonList id="RadioButtonList1a" runat="server"
RepeatDirection="Horizontal" >
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:RadioButtonList></td>
<td width="22">&nbsp;</td>
<td width="189">
<asp:RadioButtonList id="RadioButtonListlb" runat="server"
RepeatDirection="Horizontal"
OnSelectedIndexChanged="rbl1b_OnSelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
</asp:RadioButtonList></td>
<td width="117"><asp:Label ID="lblLabel1" runat="server" /></td>
<td width="57">&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>
 
Hi Eirik,

Because you are using RadioButtonLists, you have to loop through each item
in the list to see if it is selected. Then, if it is you capture the text
for that item. Once you have the text, you need to go one step further, to
convert the text to an integer.

Try this?

Dim Int1a As Integer
Dim Int1b As Integer
Dim intCounter As Integer
For intCounter = 0 To RadioButtonList1a.Items.Count - 1
If RadioButtonList1a.Items(intCounter).Selected = True Then
Int1a =
Convert.ToInt32(RadioButtonList1a.Items(intCounter).Text)
End If
Next
For intCounter = 0 To RadioButtonListlb.Items.Count - 1
If RadioButtonListlb.Items(intCounter).Selected = True Then
Int1b =
Convert.ToInt32(RadioButtonListlb.Items(intCounter).Text)
End If
Next
lblLabel1.Text = (Int1a + Int1b).ToString

Does this help?

Ken
MVP [ASP.NET]
 
Back
Top