ASP.NET basic calculator


Basic calculator

In this first example project, you will do simple things with ASP.NET controls, and events by building a basic calculator to sum two numbers. You will need one textbox (txtinput) to display the numbers and result, two hidden text boxes to store the numbers entered by the user, ten buttons to label numbers from 0 to 9 (bnt0 to bnt9), and the other three buttons to sum, show the result, and to clear text from the result textbox.

Each button is attached with a procedure to perform action when it is clicked. For example, when you click a button labeled 0, the value 0 will be displayed in the textbox. To allow a button to perform action when it is clicked, you need to attach the function name to the OnClick event of the button.


<asp:Button id="btn0" Text="0" runat="server" OnClick="getvalue0" />

When a number button is clicked, the number is appended to the text box value by using string concatenation operator (&). By doing this, the old numerical character is not replaced by the new character. The Text property of a text box control allows you to read and write value to the textbox.


txtinput.Text=txtinput.Text & btn0.Text

The number in the text box is a string value. To be able to sum the values together, you need to convert the sting value to a number.


dim result=CDbl(val1.Text)+CDbl(val2.Text)

This expression converts the values of val1 and val2 text boxes to floating-point numbers that can be used in the calculation.

Each ASP.NE control to be placed on the page must specify runat="server". This expression will tell the browser to process them on the server.

ASP.NET calculator

asp_basic_calculator.aspx (VB)

<script runat="server">

sub getvalue0(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn0.Text

end sub


sub getvalue1(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn1.Text

end sub


sub getvalue2(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn2.Text

end sub


sub getvalue3(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn3.Text

end sub


sub getvalue4(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn4.Text

end sub


sub getvalue5(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn5.Text

end sub


sub getvalue6(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn6.Text

end sub


sub getvalue7(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn7.Text

end sub


sub getvalue8(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn8.Text

end sub


sub getvalue9(s as Object, e as EventArgs)

txtinput.Text=txtinput.Text & btn9.Text

end sub


sub check(s as Object, e as EventArgs)



end sub


sub sum(s as Object, E as EventArgs)


dim result=CDbl(val1.Text)+CDbl(val2.Text)


end sub


sub clear(s as Object, e as EventArgs)




end sub





<title> Basic Calculator</title>



<form runat="server">

<h1>Basic Calculator</h1>

<asp:TextBox id="txtinput" runat="server" />

<table style="background:#00ffff">


<td><asp:Button id="btn0" Text="0" runat="server" OnClick="getvalue0" /></rd>

<td><asp:Button id="btn1" Text="1" runat="server" OnClick="getvalue1" /></rd>

<td><asp:Button id="btn2" Text="2" runat="server" OnClick="getvalue2"/></rd>


<td><asp:Button id="btnsum" Text="+" runat="server" OnClick="check"/></rd>


<td><asp:Button id="btn3" Text="3" runat="server" OnClick="getvalue3" /></rd>

<td><asp:Button id="btn4" Text="4" runat="server" OnClick="getvalue4"/></rd>

<td><asp:Button id="btn5" Text="5" runat="server" OnClick="getvalue5"/></rd>


<td><asp:Button id="btnequal" Text="=" runat="server" OnClick="sum" /></rd>


<td><asp:Button id="btn6" Text="6" runat="server" OnClick="getvalue6"/></rd>

<td><asp:Button id="btn7" Text="7" runat="server" OnClick="getvalue7"/></rd>

<td><asp:Button id="btn8" Text="8" runat="server" OnClick="getvalue8"/></rd>


<td><asp:Button id="btnclear" Text="Clear" runat="server" OnClick="clear"/></rd>


<tr><td><asp:Button id="btn9" Text="9" runat="server" OnClick="getvalue9"/></td></tr>


<asp:Textbox id="val1" Visible="false" runat="server"/>

<asp:Textbox id="val2" visible="false" runat="server"/>






S K SINGH comment



maxxsaiyan123 comment

 maxxsaiyan123 is so more comfortable for networking process in LAN .........
[url="] MAURYA SOFTWARE [/url]


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.