p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   Need to click radio button twice for event to fire (http://p2p.wrox.com/showthread.php?t=73597)

areed24 March 27th, 2009 11:15 AM

Need to click radio button twice for event to fire
 
I have a page that has a text field where users enter a number, then they can select either Dollars or Euros and the amount is converted. It works fine except that the user has to click the radio button twice the first time the page loads to get the conversion to fire. Below is my code:

ProtectedSub radio1_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio1.SelectedIndexChanged
If Page.IsValid Then
If radio1.SelectedItem.ToString = "Dollars"Then
Dim result AsSingle = 0
Dim crate AsSingle = 0.7836
Dim currency AsSingle = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text = System.Math.Round(Convert.ToDecimal(result), 2) &
" €"
ElseIf radio1.SelectedItem.ToString = "Euros"Then
Dim result AsSingle = 0
Dim crate AsSingle = 1.2762
Dim currency AsSingle = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text =
"$ " & System.Math.Round(Convert.ToDecimal(result), 2)
Else
lblcurrencyResult.Text = ""
EndIf
EndIf
EndSub

Any idea why this is happening?

robzyc March 27th, 2009 11:26 AM

This should help - 2nd hit has a response

areed24 March 27th, 2009 12:53 PM

Thank you but that doesn't offer any help. The problem seems to be with the If Page.IsValid statement. With that statement, when the page loads and I click on the radio button, it clicks but nothing happens. When I click on it a second time, the event fires. However when I remove the Page.IsValid Statement, it works fine.

Imar March 27th, 2009 12:59 PM

In that case, you'll need to post more code. Maybe there is a reason why the page is not valid the first time? If you do post the code, please use the Code button from the toolbar to format it.

Imar

areed24 March 27th, 2009 01:04 PM

Code:

<asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<tablewidth="100%">
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">Value</td>
<tdclass="style2">Unit of Measurement</td>
<td>Converted Value</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
Currency</td>
<tdclass="style4">
<asp:TextBoxID="txtcurrency"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"style="margin-left: 0px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:radiobuttonlistid="radio1"runat="server"AutoPostBack="True"CausesValidation="True">
<asp:listitemid="option1"runat="server"value="Dollars"/>
<asp:listitemid="option2"runat="server"value="Euros"/>
</asp:radiobuttonlist></td>
<td><asp:LabelID="lblcurrencyResult"runat="server"></asp:Label></td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style1"colspan="2">
<asp:RangeValidatorID="RangeValidator1"runat="server"ControlToValidate="txtcurrency"ErrorMessage="Please enter a valid number"MaximumValue="1000000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">&nbsp;</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
Temperature</td>
<tdclass="style4">
<asp:TextBoxID="txttemp"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:RadioButtonListID="radio2"runat="server"AutoPostBack="True">
<asp:ListItemID="option3"runat="server"value="&deg Fahrenheit"/>
<asp:ListItemID="option4"runat="server"value="&deg Celcius"/>
</asp:RadioButtonList>
</td>
<td>
<asp:LabelID="lbltempResult"runat="server"></asp:Label>
</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
<asp:RangeValidatorID="RangeValidator2"runat="server"ControlToValidate="txttemp"ErrorMessage="Please enter a valid number"MaximumValue="1000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">&nbsp;</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
Weight</td>
<tdclass="style4">
<asp:TextBoxID="txtweight"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:RadioButtonListID="radio3"runat="server"AutoPostBack="True">
<asp:ListItemID="option5"runat="server"value="Pounds"/>
<asp:ListItemID="option6"runat="server"value="Kilos"/>
</asp:RadioButtonList>
</td>
<td>
<asp:LabelID="lblweightResult"runat="server"></asp:Label>
</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
<asp:RangeValidatorID="RangeValidator3"runat="server"ControlToValidate="txtweight"ErrorMessage="Please enter a valid number"MaximumValue="1000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">&nbsp;</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
Height</td>
<tdclass="style4">
<asp:TextBoxID="txtheight"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:RadioButtonListID="radio4"runat="server"AutoPostBack="True">
<asp:ListItemID="option7"runat="server"value="Inches"/>
<asp:ListItemID="option8"runat="server"value="Meters"/>
</asp:RadioButtonList>
</td>
<td>
<asp:LabelID="lblheightResult"runat="server"></asp:Label>
</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
<asp:RangeValidatorID="RangeValidator4"runat="server"ControlToValidate="txtheight"ErrorMessage="Please enter a valid number"MaximumValue="1000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<tdclass="style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
&nbsp;</td>
<tdclass="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
Distance</td>
<tdclass="style4">
<asp:TextBoxID="txtdistance"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:RadioButtonListID="radio5"runat="server"AutoPostBack="True">
<asp:ListItemID="option9"runat="server"value="Miles"/>
<asp:ListItemID="option10"runat="server"value="Kilometers"/>
</asp:RadioButtonList>
</td>
<td>
<asp:LabelID="lbldistanceResult"runat="server"></asp:Label>
</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
<asp:RangeValidatorID="RangeValidator5"runat="server"ControlToValidate="txtdistance"ErrorMessage="Please enter a valid number"MaximumValue="1000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<tdclass="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
&nbsp;</td>
<tdclass="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
Cooking</td>
<tdclass="style4">
<asp:TextBoxID="txtcooking"runat="server"AutoPostBack="True"CausesValidation="True"Width="49px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:RadioButtonListID="radio6"runat="server"AutoPostBack="True">
<asp:ListItemID="option11"runat="server"value="Cups"/>
<asp:ListItemID="option12"runat="server"value="Milliliters"/>
</asp:RadioButtonList>
</td>
<td>
<asp:LabelID="lblcookingResult"runat="server"></asp:Label>
</td>
</tr>
<tr>
<tdclass="style5">
&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">
<asp:RangeValidatorID="RangeValidator6"runat="server"ControlToValidate="txtcooking"ErrorMessage="Please enter a valid number"MaximumValue="1000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td>
<tdclass="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</
asp:Content>



Page behind:
Code:

ProtectedSub radio1_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio1.SelectedIndexChanged
If Page.IsValid Then
If radio1.SelectedItem.ToString = "Dollars"Then
Dim result AsSingle = 0
Dim crate AsSingle = 0.7836
Dim currency AsSingle = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text = System.Math.Round(Convert.ToDecimal(result), 2) &
" &euro;"
ElseIf radio1.SelectedItem.ToString = "Euros"Then
Dim result AsSingle = 0
Dim crate AsSingle = 1.2762
Dim currency AsSingle = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text =
"$ " & System.Math.Round(Convert.ToDecimal(result), 2)
Else
lblcurrencyResult.Text = ""
EndIf
EndIf
EndSub
ProtectedSub radio2_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio2.SelectedIndexChanged
If Page.IsValid Then
If radio2.SelectedItem.ToString = "&deg Fahrenheit"Then
Dim result AsSingle = 0
Dim temp AsSingle = Convert.ToSingle(txttemp.Text)
result = (temp - 32) * 5 / 9
lbltempResult.Text = System.Math.Round(result) &
" &deg Celcius"
ElseIf Page.IsValid AndAlso radio2.SelectedItem.ToString = "&deg Celcius"Then
Dim result AsSingle = 0
Dim temp AsSingle = Convert.ToSingle(txttemp.Text)
result = (temp * 9 / 5) + 32
lbltempResult.Text = System.Math.Round(result) &
" &deg Fahrenheit"
Else
lbltempResult.Text = ""
EndIf
EndIf
EndSub
ProtectedSub radio3_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio3.SelectedIndexChanged
If Page.IsValid Then
If radio3.SelectedItem.ToString = "Pounds"Then
Dim result AsSingle = 0
Dim weight AsSingle = Convert.ToSingle(txtweight.Text)
result = weight * (0.45359)
lblweightResult.Text = System.Math.Round(result, 1) &
" Kilos"
ElseIf Page.IsValid AndAlso radio3.SelectedItem.ToString = "Kilos"Then
Dim result AsSingle = 0
Dim weight AsSingle = Convert.ToSingle(txtweight.Text)
result = weight * 2.2046
lblweightResult.Text = System.Math.Round(result, 1) &
" Pounds"
Else
lblweightResult.Text = ""
EndIf
EndIf
EndSub
ProtectedSub radio4_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio4.SelectedIndexChanged
If Page.IsValid Then
If radio4.SelectedItem.ToString = "Inches"Then
Dim result AsSingle = 0
Dim height AsSingle = Convert.ToSingle(txtheight.Text)
result = height * 0.0254
lblheightResult.Text = System.Math.Round(result, 2) &
" Meters"
ElseIf Page.IsValid AndAlso radio4.SelectedItem.ToString = "Meters"Then
Dim result AsSingle = 0
Dim height AsSingle = Convert.ToSingle(txtheight.Text)
result = height * 39.37
lblheightResult.Text = System.Math.Round(result, 1) &
" Inches"
Else
lblheightResult.Text = ""
EndIf
EndIf
EndSub
ProtectedSub radio5_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio5.SelectedIndexChanged
If Page.IsValid Then
If radio5.SelectedItem.ToString = "Miles"Then
Dim result AsSingle = 0
Dim distance AsSingle = Convert.ToSingle(txtdistance.Text)
result = distance * 1.60934
lbldistanceResult.Text = System.Math.Round(result, 2) &
" Kilometers"
ElseIf Page.IsValid AndAlso radio5.SelectedItem.ToString = "Kilometers"Then
Dim result AsSingle = 0
Dim distance AsSingle = Convert.ToSingle(txtdistance.Text)
result = distance * 0.62
lbldistanceResult.Text = System.Math.Round(result, 2) &
" Miles"
Else
lbldistanceResult.Text = ""
EndIf
EndIf
EndSub
ProtectedSub radio6_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles radio6.SelectedIndexChanged
If Page.IsValid Then
If radio6.SelectedItem.ToString = "Cups"Then
Dim result AsSingle = 0
Dim cooking AsSingle = Convert.ToSingle(txtcooking.Text)
result = cooking * 236.5882
lblcookingResult.Text = System.Math.Round(result, 2) &
" Milliliters"
ElseIf Page.IsValid AndAlso radio6.SelectedItem.ToString = "Milliliters"Then
Dim result AsSingle = 0
Dim cooking AsSingle = Convert.ToSingle(txtcooking.Text)
result = cooking * 0.00423675
lblcookingResult.Text = System.Math.Round(result, 3) &
" Cups"
Else
lblcookingResult.Text = ""
EndIf
EndIf
EndSub
End
Class



Imar March 27th, 2009 01:47 PM

Can you repost the code and click the Remove Text Formatting button first? It's quite impossible to read the code now...

Imar

Imar March 27th, 2009 01:59 PM

BTW: if you set a breakpoint in the server side handler, does it get hit the first time you click the control?

Imar

areed24 March 30th, 2009 05:35 AM

Sorry, is this better:

Code:

asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<tablewidth="100%">
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style4">Value</td>
<tdclass="style2">Unit of Measurement</td>
<td>Converted Value</td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
Currency</td>
<tdclass="style4">
<asp:TextBoxID="txtcurrency"runat="server"AutoPostBack="True"
CausesValidation="True"Width="49px"style="margin-left: 0px"></asp:TextBox>
</td>
<tdclass="style2">
<asp:radiobuttonlistid="radio1"runat="server"
AutoPostBack="True"CausesValidation="True">
<asp:listitemid="option1"runat="server"value="Dollars"/>
<asp:listitemid="option2"runat="server"value="Euros"/>
</asp:radiobuttonlist></td>
<td><asp:LabelID="lblcurrencyResult"runat="server"></asp:Label></td>
</tr>
<tr>
<tdclass="style5">&nbsp;</td>
<tdclass="style3">
&nbsp;</td>
<tdclass="style1"colspan="2">
<asp:RangeValidatorID="RangeValidator1"runat="server"
ControlToValidate="txtcurrency"ErrorMessage="Please enter a valid number"
MaximumValue="1000000"MinimumValue="0"Type="Double"></asp:RangeValidator>
</td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

Code Behind Page
Code:

Protected Sub radio1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radio1.SelectedIndexChanged
If Page.IsValid Then
If radio1.SelectedItem.ToString = "Dollars" Then
Dim result As Single = 0
Dim crate As Single = 0.7836
Dim currency As Single = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text = System.Math.Round(Convert.ToDecimal(result), 2) & " &euro;"
ElseIf radio1.SelectedItem.ToString = "Euros" Then
Dim result As Single = 0
Dim crate As Single = 1.2762
Dim currency As Single = Convert.ToSingle(txtcurrency.Text)
result = currency * crate
lblcurrencyResult.Text = "$ " & System.Math.Round(Convert.ToDecimal(result), 2)
Else
lblcurrencyResult.Text = ""
End If
End If
End Sub


Imar March 30th, 2009 01:30 PM

Not really. As you can see in the ASPX, all code is glued together, without any spaces. It's a stupid bug in this forum. To work around it, paste the code in notepad first, then copy and paste it again here and wrap it in code tags

Did you try setting a breakpoint? What was the outcome of that?

Imar

areed24 March 31st, 2009 11:50 AM

I set the breakpoint and when the page loaded it seemed to work fine. Now that I have removed the breakpoint, it continues to work fine. Wierd. As you can see, I will need to update the conversion rate daily. Without paying for an update service, what is the best way to update the rates? I have thought about creating a txt document that I could upload to the server each day to give the results. Do you have any suggestions?

Thank you very much for all of your help.


All times are GMT -4. The time now is 10:22 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.