Users Pricing

articles

home / developersection / articles / calculator in asp.net without javascript

Calculator in ASP.Net without Javascript

Anonymous User 11850 27 Jul 2010 Updated 04 Mar 2020

 Calculator in ASP.Net without Javascript

Code for the above design (Default.aspx)

 

<body>
    <form id="form1" runat="server">  
    <asp:Button ID="btn2" runat="server" Height="30px"
        style="z-index1left240pxtop123pxpositionabsoluteheight33pxwidth37px"
        Text="2" Width="30px" onclick="btn_Click" />
    <asp:TextBox ID="txtValue" runat="server"
       
        style="z-index1left191pxtop86pxpositionabsolutewidth184pxtext-align:right"></asp:TextBox>
    <asp:Button ID="btn3" runat="server" Height="30px"
        style="z-index1left290pxtop123pxpositionabsoluteheight33pxwidth37px"
        Text="3" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn8" runat="server" Height="30px"
        style="z-index1left240pxtop215pxpositionabsoluteheight33pxwidth37pxbottom88px"
        Text="8" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btnAdd" runat="server" Height="30px"
        style="z-index1left339pxtop123pxpositionabsoluteheight33pxwidth37px"
        Text="+" Width="30px" onclick="btnAdd_Click" />
    <asp:Button ID="btn7" runat="server" Height="30px"
        style="z-index1left190pxtop215pxpositionabsoluteheight33pxwidth37px"
        Text="7" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn6" runat="server" Height="30px"
        style="z-index1left290pxtop170pxpositionabsoluteheight33pxwidth37px"
        Text="6" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn5" runat="server" Height="30px"
        style="z-index1left240pxtop170pxpositionabsoluteheight33pxwidth37px"
        Text="5" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn4" runat="server" Height="30px"
        style="z-index1left190pxtop170pxpositionabsoluteheight33pxwidth37px"
        Text="4" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn1" runat="server" Height="30px"
        style="z-index1left190pxtop123pxpositionabsoluteheight33pxwidth37px"
        Text="1" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn9" runat="server" Height="30px"
        style="z-index1left290pxtop215pxpositionabsoluteheight33pxwidth37px"
        Text="9" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn0" runat="server" Height="30px"
        style="z-index1left240pxtop260pxpositionabsoluteheight33pxwidth37pxright515px;"
        Text="0" Width="30px" onclick="btn_Click" />
      
    <asp:Button ID="btnDiv" runat="server" Height="30px"
        style="z-index1left340pxtop259pxpositionabsoluteheight33pxwidth37px"
        Text="/" Width="30px" onclick="btnDiv_Click" />
    <asp:Button ID="btnMul" runat="server" Height="30px"
        style="z-index1left339pxtop214pxpositionabsoluteheight33pxwidth37px"
        Text="x" Width="30px" onclick="btnMul_Click" />
    <asp:Button ID="btnSub" runat="server" Height="30px"
        style="z-index1left339pxtop167pxpositionabsoluteheight33pxwidth37px"
        Text="-" onclick="btnSub_Click" />
    <p>
    <asp:Button ID="btnDec" runat="server" Height="30px"
        style="z-index1left191pxtop260pxpositionabsoluteheight33pxwidth37px"
        Text="." Width="30px" onclick="btnDec_Click" />
    <asp:Button ID="btnEqual" runat="server" Height="30px"
        style="z-index1left292pxtop260pxpositionabsoluteheight33pxwidth37px"
        Text="=" Width="30px" onclick="btnEqual_Click" />
    </p>
      
    <asp:Button ID="btnClose" runat="server" Text="Close"
       
        style="position:absolutetop303pxleft293pxwidth53pxheight34px;"
        onclick="btnClose_Click"/>
      
    <asp:Button ID="btnClear" runat="server" Text="Clear"
        style="position:absolutetop303pxleft221pxwidth57pxheight34px;"
        onclick="btnClear_Click"/>
      
    </form>
</body>
C Sharp code for above design(Default.aspx.cs)

 

             //declaring globalvalues
             //val for value
   Double val=0;
        enum operators { Add=1, Sub, Mul,Div, Equal };
           //operator_clicked to checked last operator clicked.
        int operator_clicked = 0;
          //pressed to check for number button pressed
        Boolean pressed;
 
         //equal function which is used with every math operator
        void equal()
        {
             //initilizing value of viewstate in val to get the previous value of val
             //before clicking any button
            val = (double)ViewState["val"];
              //fetching value of operator button last clicked in o
            int o = (int)ViewState["operator"];
              //performing task according to the value of o
            switch (o)
            {
                case (int)operators.Add:
                    val = (val + Convert.ToDouble(txtValue.Text));
                    break;
                case (int)operators.Sub:
                    val = (val - double.Parse(txtValue.Text));
                    break;
                case (int)operators.Mul:
                    val = (val * double.Parse(txtValue.Text));
                    break;
                case (int)operators.Div:
                    val = (val / double.Parse(txtValue.Text));
                    break;
                default:
                    if (txtValue.Text != "")
                        val = Convert.ToDouble(txtValue.Text);
                    else
                        val = 0;
                    break;
            }
//assigning textbox text to the val computed above
            txtValue.Text = val.ToString();
//storing values in view state.
            ViewState["val"] = val;
            ViewState["dec_pressed"] = false;
            ViewState["pressed"] = false;
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
         //checking condition whether page is reloaded or not if not reloaded ie. If
           //it is loaded for the first time then the below statements will execute.
            if (!IsPostBack)
            {
             //
                val = 0;
                ViewState["val"] = val;
                ViewState["operator"] = 0;
                ViewState["pressed"] = false;
                ViewState["dec_pressed"] = false;
                txtValue.Text = "0";
            }
        }
 
            //on number button click event this function will execute.
        protected void btn_Click(object sender, EventArgs e)
        {
            //checking whether number button is pressed first time after clicking any
             //operator or not
            pressed = (Boolean)ViewState["pressed"];
            if (pressed==false)
                txtValue.Text = "";
            ViewState["pressed"] = true;
            //concatinating text of button clicked to the text of text box.
            txtValue.Text += ((Button)sender).Text;
        }
 
        protected void btnAdd_Click(object sender, EventArgs e)
        {           
            equal();
            //setting operator clicked value to value of operators.add ie.1 and storing
              //in viewstate for further use.
            operator_clicked = (int)operators.Add;                 
            ViewState["operator"] = operator_clicked;          
        }
 
        protected void btnSub_Click(object sender, EventArgs e)
        {
            equal();
            operator_clicked = (int)operators.Sub;         
            ViewState["operator"] = operator_clicked;         
        }
 
        protected void btnMul_Click(object sender, EventArgs e)
        {
            equal();
            operator_clicked = (int)operators.Mul;          
            ViewState["operator"] = operator_clicked;         
        }
 
        protected void btnDiv_Click(object sender, EventArgs e)
        {
            equal();
            operator_clicked = (int)operators.Div;    
            ViewState["operator"] = operator_clicked;        
        }
 
        protected void btnEqual_Click(object sender, EventArgs e)
        {
            equal();           
           //setting value of val to 0 and storing in viewstate and operator_clicked
             //value to value of equal ie. 5
            val = 0;
            ViewState["val"] = val;
            operator_clicked = (int)operators.Equal;
            ViewState["operator"] = operator_clicked;         
        }
 
                //this will clear text box and assign initial values to viewstate.
        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtValue.Text = "";
            ViewState["val"] = 0;
            ViewState["operator"]= 0;
            ViewState["pressed"]= false;
            ViewState["dec_pressed"]= false;
        }
 
 
        protected void btnClose_Click(object sender, EventArgs e)
        {
              //java script is used to close window.
            Response.Write("<script language='javascript'> {window.close();}</script>");
        }
 
             //put decimal point in text box if it does not contain any.
        protected void btnDec_Click(object sender, EventArgs e)
        {
            pressed = (Boolean)ViewState["pressed"];
            if (pressed == false)
                txtValue.Text= "";
             //checking whether decimal is ther in text box or not.
            if ((bool)ViewState["dec_pressed"] == false)
            {
                if (txtValue.Text == "")
                    txtValue.Text = "0.";
                else
                    txtValue.Text += ".";
            }
              //setting deciaml pressed view satate and number pressed view state to true.
            ViewState["dec_pressed"] = true;
            ViewState["pressed"] = true;
        }
 

  Snap shot

 Calculator in ASP.Net without Javascript

 


I am a content writter !


2 Comments