Create simple Calculator in ASP.Net with C#

Today, we will learn to create simple one digit calculator control in ASP.Net with C#.

Here we will make sum, multiplication and Subtraction between two single digit value in asp.net C#.

First we design asp.net web page, the HTML code for design calculator is:

<table id=”TABLE1″ style=”z-index: 100; border-left-color: #000000; left: 368px;
border-bottom-color: #000000; border-top-style: solid; border-top-color: #000000;
border-right-style: solid; border-left-style: solid; position: absolute; top: 144px;
border-right-color: #000000; border-bottom-style: solid” language=”javascript”>
<tr>
<td colspan=”3″ style=”border-bottom-color: #000000; text-align: center; border-bottom-style: solid”>
<strong><span style=”font-size: 25px; color: #cc0066″>Calculator in ASP.Net C#<br />
<br />
</span></strong>
</td>
</tr>
<tr>
<td colspan=”3″ style=”border-bottom-color: #000000; border-bottom-style: solid”>
<asp:TextBox ID=”txtanswer” runat=”server” BorderColor=”#C00000″ Font-Size=”X-Large”
Height=”32px” Width=”192px”></asp:TextBox>&nbsp;<asp:Button ID=”btnresult” runat=”server”
BorderColor=”Red” Font-Bold=”True” Font-Size=”X-Large” ForeColor=”Red” OnClick=”btnresult_Click”
Text=”=” Width=”56px” /></td>
</tr>
<tr>
<td style=”width: 100px; height: 50px”>
<asp:Button ID=”btn1″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn1_Click” Text=”1″ Width=”45px” /></td>
<td style=”width: 100px; height: 50px”>
<asp:Button ID=”btn2″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn2_Click” Text=”2″ Width=”45px” /></td>
<td style=”width: 100px; height: 50px”>
<asp:Button ID=”btn3″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn3_Click” Text=”3″ Width=”45px” /></td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Button ID=”btn4″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn4_Click” Text=”4″ Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btn5″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn5_Click” Text=”5″ Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btn6″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn6_Click” Text=”6″ Width=”45px” /></td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Button ID=”btn7″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn7_Click” Text=”7″ Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btn8″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn8_Click” Text=”8″ Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btn9″ runat=”server” BorderColor=”Purple” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Purple” Height=”48px” OnClick=”btn9_Click” Text=”9″ Width=”45px” /></td>
</tr>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
<asp:Button ID=”btnplus” runat=”server” BorderColor=”Blue” Font-Bold=”True” Font-Size=”X-Large”
ForeColor=”Blue” Height=”48px” OnClick=”btnplus_Click” Text=”+” Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btnmultiplication” runat=”server” BorderColor=”Blue” Font-Bold=”True”
Font-Size=”XX-Large” ForeColor=”Blue” Height=”48px” OnClick=”btnmultiplication_Click”
Text=”*” Width=”45px” /></td>
<td style=”width: 100px”>
<asp:Button ID=”btnminus” runat=”server” BorderColor=”Blue” Font-Bold=”True” Font-Size=”XX-Large”
ForeColor=”Blue” Height=”48px” OnClick=”btnminus_Click” Text=”-” Width=”45px” /></td>
</tr>
</table>

The Calculator in ASP.Net Example output is:

create calculator in asp.net with c#

create calculator in asp.net with c#

Now, we need to here write code for all Button control, we have 1 to 9 Digit Button control and Three Arithmetic Sign Button and one is Equal Button for displaying answer to Answer TextBox in ASP.Net.

so, write code like below on all Digit Button control:

 protected void btn1_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “1″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “1″;
}
}
protected void btn2_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “2″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “2″;
}
}
protected void btn3_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “3″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “3″;
}
}
protected void btn4_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “4″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “4″;
}
}
protected void btn5_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “5″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “5″;
}
}
protected void btn6_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “6″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “6″;
}
}
protected void btn7_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “7″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “7″;
}
}
protected void btn8_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “8″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “8″;
}

}
protected void btn9_Click(object sender, EventArgs e)
{
if (Session["No"] == null)
{
txtanswer.Text = “9″;
Session["No"] = txtanswer.Text;
}
else
{
txtanswer.Text = “9″;
}

}

Now, After writing above code press any Digit Button and you will have below screen.

Here asp.net example we press 4 Digit Button:

create calculator in asp.net with C#

create calculator in asp.net with C#

In above screen show that we have pressed Digit 4, After Press any Digit Press the Sign Button which you want to make.

Write the Below code on Sign Button (Plus, Minus and Multiplication)

protected void btnplus_Click(object sender, EventArgs e)
{
Session["Sign"] = “+”;
}
protected void btnmultiplication_Click(object sender, EventArgs e)
{
Session["Sign"] = “*”;
}
protected void btnminus_Click(object sender, EventArgs e)
{
Session["Sign"] = “-”;
}

Now, When we press any Sign Digit Button the Sign Will be stored in Session["Sing"] and then press any other Digit.

Here, we press 8 Digit sign after pressing Plus sign.

create calculator in asp.net with C#

create calculator in asp.net with C#

In above asp.net Example we make sum of 4 and 8 Digit the Result will be 12 Displayed in Result TextBox on Equal to Button.

Write below code on Equal to Button for generate result of our calculator in asp.net.

protected void btnresult_Click(object sender, EventArgs e)
{
if (Session["Sign"].ToString() == “+”)
{
int sum = Convert.ToInt32(Session["No"].ToString()) + Convert.ToInt32(txtanswer.Text);
txtanswer.Text = sum.ToString();
}
else if (Session["Sign"].ToString() == “*”)
{
int sum = Convert.ToInt32(Session["No"].ToString()) * Convert.ToInt32(txtanswer.Text);
txtanswer.Text = sum.ToString();
}
else if (Session["Sign"].ToString() == “-”)
{
int sum = Convert.ToInt32(Session["No"].ToString()) – Convert.ToInt32(txtanswer.Text);
txtanswer.Text = sum.ToString();
}
Session["No"] = null;
Session["Sign"] = null;
}

The ASP.Net Calculator control output of sum of 4 and 8, the answer is 12 Display on result textbox.

create calculator in asp.net with C#

create calculator in asp.net with C#

I hope this asp.net tutorials C# will help you…….

 

 

2 Responses to Create simple Calculator in ASP.Net with C#

  1. Naveen says:

    Hi,
    what is “Session” here.

    kindly explain..

    it says “Does not exist in the current context”..

    thanks in advance.

    Naveen.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 + 3 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">