Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 25, 2012 08:37 PM by Mikesdotnetting
Member
732 Points
449 Posts
Feb 25, 2012 07:28 PM|LINK
There's an example that lets users enter two numbers, then it adds them and displays the result in the web pages tutorial, (http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax)
Besides adding two numbers, when i add another line to subtract the numbers, how can i add another submit button to display the result?
<input type="submit" value="Subtract" />
total = num1.AsInt() - num2.AsInt();
All-Star
154927 Points
19867 Posts
Moderator
MVP
Feb 25, 2012 08:01 PM|LINK
You need to provide a name attribute to both submit buttons so that you can detect which one was clicked:
@{ var total = 0; var totalMessage = ""; if(IsPost) { // Retrieve the numbers that the user entered. var num1 = Request["text1"]; var num2 = Request["text2"]; // Convert the entered strings into integers numbers and add. if(Request["action"] == "Add"){ total = num1.AsInt() + num2.AsInt(); } else { total = num1.AsInt() - num2.AsInt(); } totalMessage = "Total = " + total; } } <!DOCTYPE html> <html lang="en"> <head> <title>Add Or Subtract Numbers</title> <meta charset="utf-8" /> <style type="text/css"> body {background-color: beige; font-family: Verdana, Arial; margin: 50px; } form {padding: 10px; border-style: solid; width: 250px;} </style> </head> <body> <p>Enter two whole numbers and then click <strong>Add</strong> or <strong>Subtract</strong>.</p> <form action="" method="post"> <p><label for="text1">First Number:</label> <input type="text" name="text1" /> </p> <p><label for="text2">Second Number:</label> <input type="text" name="text2" /> </p> <p><input type="submit" name="action" value="Add" /> <input type="submit" name="action" value="Subtract" /></p> </form> <p>@totalMessage</p> </body> </html>
Feb 25, 2012 08:30 PM|LINK
if i have to divide i added var x = 0.9999m; or var x = 1m;
to avoid divide by zero, is this correct for decimal numbers, not AsInt?
Feb 25, 2012 08:37 PM|LINK
There's an AsDecimal helper you can use. Test to see if the second number is zero before trying to divide. If it is zero, don't divide.
dow7
Member
732 Points
449 Posts
two submit buttons
Feb 25, 2012 07:28 PM|LINK
There's an example that lets users enter two numbers, then it adds them and displays the result in the web pages tutorial, (http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax)
Besides adding two numbers, when i add another line to subtract the numbers, how can i add another submit button to display the result?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: two submit buttons
Feb 25, 2012 08:01 PM|LINK
You need to provide a name attribute to both submit buttons so that you can detect which one was clicked:
@{ var total = 0; var totalMessage = ""; if(IsPost) { // Retrieve the numbers that the user entered. var num1 = Request["text1"]; var num2 = Request["text2"]; // Convert the entered strings into integers numbers and add. if(Request["action"] == "Add"){ total = num1.AsInt() + num2.AsInt(); } else { total = num1.AsInt() - num2.AsInt(); } totalMessage = "Total = " + total; } } <!DOCTYPE html> <html lang="en"> <head> <title>Add Or Subtract Numbers</title> <meta charset="utf-8" /> <style type="text/css"> body {background-color: beige; font-family: Verdana, Arial; margin: 50px; } form {padding: 10px; border-style: solid; width: 250px;} </style> </head> <body> <p>Enter two whole numbers and then click <strong>Add</strong> or <strong>Subtract</strong>.</p> <form action="" method="post"> <p><label for="text1">First Number:</label> <input type="text" name="text1" /> </p> <p><label for="text2">Second Number:</label> <input type="text" name="text2" /> </p> <p><input type="submit" name="action" value="Add" /> <input type="submit" name="action" value="Subtract" /></p> </form> <p>@totalMessage</p> </body> </html>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
dow7
Member
732 Points
449 Posts
Re: two submit buttons
Feb 25, 2012 08:30 PM|LINK
if i have to divide i added var x = 0.9999m; or var x = 1m;
to avoid divide by zero, is this correct for decimal numbers, not AsInt?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: two submit buttons
Feb 25, 2012 08:37 PM|LINK
There's an AsDecimal helper you can use. Test to see if the second number is zero before trying to divide. If it is zero, don't divide.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter