In this article, we will create an example that performs the Addition, Subtraction, Multiplication and Division Operations with ASP.NET. In our example, we will use RadioButton Controls for the selection of Addition, Subtraction, Multiplication and Division. We will display the result according to the process selected in RadioButton and display it in Label control.
In order to perform our example, in Visual Studio program, we add Add – Web Form to File – New – Web Site by using Solution Explorer window.
We open the Web Application as follows. Then we add a WebForm into the application.
Add a WebForm to Project
We create our WebForm as follows:
The above webform design codes will be as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style1 { width: 49%; } .auto-style2 { width: 107px; } .auto-style3 { width: 141px; } .auto-style4 { width: 107px; height: 23px; } .auto-style5 { width: 141px; height: 23px; } .auto-style6 { height: 23px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="auto-style1"> <tr> <td class="auto-style2"> <asp:Label ID="Label1" runat="server" Text="Number 1"></asp:Label> </td> <td class="auto-style3"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td> <asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="calc" Text="Add" /> </td> </tr> <tr> <td class="auto-style2"> <asp:Label ID="Label2" runat="server" Text="Number 2"></asp:Label> </td> <td class="auto-style3"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> <td> <asp:RadioButton ID="RadioButton2" runat="server" GroupName="calc" Text="Sub" /> </td> </tr> <tr> <td class="auto-style2"> </td> <td class="auto-style3"> <asp:Button ID="Button1" runat="server" Text="Calculate" Width="125px" /> </td> <td> <asp:RadioButton ID="RadioButton3" runat="server" GroupName="calc" Text="Mul" /> </td> </tr> <tr> <td class="auto-style2"> </td> <td class="auto-style3"> </td> <td> <asp:RadioButton ID="RadioButton4" runat="server" GroupName="calc" Text="Div" /> </td> </tr> <tr> <td class="auto-style4"> <asp:Label ID="Label3" runat="server" Text="Result"></asp:Label> </td> <td class="auto-style5"> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> </td> <td class="auto-style6"></td> </tr> </table> </div> </form> </body> |
Button Click: After completing the adjustments in the Design section, we double-click on the Button control and write the following C # codes to the Click event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
protected void Button1_Click(object sender, EventArgs e) { double num1, num2, result = 0; num1 = Convert.ToDouble(TextBox1.Text); num2 = Convert.ToDouble(TextBox2.Text); if (RadioButton1.Checked) { result = num1 + num2; } else if (RadioButton2.Checked) { result = num1 - num2; } else if (RadioButton3.Checked) { result = num1 * num2; }//www.code4example.com else { result = num1 / num2; } Label4.Text = result.ToString(); } |
Output: