Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 27th, 2009, 11:15 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
 
Old March 27th, 2009, 11:26 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

This should help - 2nd hit has a response
__________________
Rob
http://cantgrokwontgrok.blogspot.com
 
Old March 27th, 2009, 12:53 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old March 27th, 2009, 12:59 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 27th, 2009, 01:04 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
EndClass

 
Old March 27th, 2009, 01:47 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 27th, 2009, 01:59 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 30th, 2009, 05:35 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 30th, 2009, 01:30 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 31st, 2009, 11:50 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Link button click event sarah lee ASP.NET 2.0 Basics 5 May 18th, 2007 09:29 AM
Unable to fire a click event rfinks ASP.NET 1.0 and 1.1 Basics 2 September 1st, 2006 10:26 PM
Browser back button makes wrong event fire Surre ASP.NET 1.0 and 1.1 Professional 1 March 13th, 2006 03:40 PM
Event for radio button wes_king Classic ASP Databases 0 January 3rd, 2006 02:29 AM
Can't fire button event more than 1 time goatstudio ASP.NET 2.0 Basics 4 September 22nd, 2004 07:45 AM





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