Read input and do the calculation in Button action listener (C#) : Button « Asp Control « ASP.Net






Read input and do the calculation in Button action listener (C#)

<%@ Page Language="C#" Debug="True" %>
<script runat="server">

    void Page_Load()
    {
        btnAdd.BackColor = System.Drawing.Color.LightGray;
        btnSubtract.BackColor = System.Drawing.Color.LightGray;
        btnMultiply.BackColor = System.Drawing.Color.LightGray;
        btnDivide.BackColor = System.Drawing.Color.LightGray;
    }
    
    void Add(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) +
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnAdd.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Subtract(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) -
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnSubtract.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Multiply(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) *
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnMultiply.BackColor = System.Drawing.Color.Yellow;
    }
    
    void Divide(object sender, EventArgs e)
    {
        double answer = Convert.ToDouble(txtInput1.Text) /
                        Convert.ToDouble(txtInput2.Text);
        lblAnswer.Text = answer.ToString();
        btnDivide.BackColor = System.Drawing.Color.Yellow;
    }

</script>
<html>
<head>
    <title>Calculator example v1</title>
</head>
<body>
    <form runat="server">
        <h2>Calculator Version 1 
        </h2>
        <asp:TextBox id="txtInput1" runat="server"></asp:TextBox>
        <asp:Button id="btnAdd" onclick="Add" runat="server" Text=" + "></asp:Button>
        <asp:Button id="btnSubtract" onclick="Subtract" runat="server" Text=" - "></asp:Button>
        <br />
        <asp:TextBox id="txtInput2" runat="server"></asp:TextBox>
        <asp:Button id="btnMultiply" onclick="Multiply" runat="server" Text=" x "></asp:Button>
        <asp:Button id="btnDivide" onclick="Divide" runat="server" Text="/"></asp:Button>
        <br />
        <strong>Answer = <asp:Label id="lblAnswer" runat="server"></asp:Label></strong>
    </form>
</body>
</html>

           
       








Related examples in the same category

1.Event in code behind file (VB.net)
2.asp:button: command name and arguments (VB.net)
3.Event driven page (VB.net)
4.Set Button text (VB.net)
5.asp:button event to change the label text (VB.net)
6.Button action Command and Parameter (VB.net)
7.Add action listener to a server Button (VB.net)
8.asp button postbackurl (C#)
9.asp button on client click (C#)
10.Button counter: click and change button label (C#)
11.Add action listener to asp Button by delegate (C#)
12.Button event Handler (C#)
13.onmouseover and onmouseout events
14.OnClientClick event
15.CausesValidation="False"