Thursday 4 August 2016

write a program to create calculator in asp.net

calculator.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calculator.aspx.cs" Inherits="our_program.calculator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Calculator</title>
    <center><h1>Calculator</h1></center>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:DropDownList ID="ddl" runat="server">
      <asp:ListItem>--select operation--</asp:ListItem>
    <asp:ListItem>Addition</asp:ListItem>
    <asp:ListItem>Substraction</asp:ListItem>
    <asp:ListItem>Multiplication</asp:ListItem>
    <asp:ListItem>Division</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="btn" runat="server" Text="Submit" onclick="btn_Click" />
    <br />
    <br />
    <asp:Label ID="lbl1" runat="server"></asp:Label>
    </div>
    </center>
    </form>
</body>
</html>

calculator.asp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace our_program
{
    public partial class calculator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            string a = txt1.Text;
            string b = txt2.Text;
            int c = Convert.ToInt32(a);
            int d = Convert.ToInt32(b);
            if (ddl.SelectedItem.ToString() == "Addition")
            {
                int f = c + d;
                string g = Convert.ToString(f);
                lbl1.Text = c+"+"+d+"="+g;
            }
            if (ddl.SelectedItem.ToString() == "Substraction")
            {
                int f = c - d;
                string g = Convert.ToString(f);
                lbl1.Text = c + "-" + d + "=" + g;
            }
            if (ddl.SelectedItem.ToString() == "Multiplication")
            {
                int f = c * d;
                string g = Convert.ToString(f);
                lbl1.Text = c + "*" + d + "=" + g;
            }
            if (ddl.SelectedItem.ToString() == "Division")
            {
                int f = c / d;
                string g = Convert.ToString(f);
                lbl1.Text = c + "/" + d + "=" + g;
            }
        }
    }
}

No comments:

Post a Comment