[我的ASP.net学习历程]sender对象

类别:Asp 点击:0 评论:0 推荐:

<%@ Page Language="C#" Debug="True" %>
<script runat="server">
 
    void Calc(object sender, EventArgs e)
    {
        double answer;
        Button PressedButton = (Button)sender;
        switch (PressedButton.ID)
        {
            case "btnAdd":
                answer = Convert.ToDouble(txtInput1.Text) +
                         Convert.ToDouble(txtInput2.Text);
                lblAnswer.Text = answer.ToString();
                break;
            case "btnSubtract":
                answer = Convert.ToDouble(txtInput1.Text) -
                         Convert.ToDouble(txtInput2.Text);
                lblAnswer.Text = answer.ToString();
                break;
            case "btnMultiply":
                answer = Convert.ToDouble(txtInput1.Text) *
                         Convert.ToDouble(txtInput2.Text);
                lblAnswer.Text = answer.ToString();
                break;
            case "btnDivide":
                answer = Convert.ToDouble(txtInput1.Text) /
                         Convert.ToDouble(txtInput2.Text);
                lblAnswer.Text = answer.ToString();
                break;
        }
        PressedButton.BackColor = System.Drawing.Color.Yellow;
    }

</script>
<html>
<head>
    <title>Chapter 6 - Calculator example v2</title>
</head>
<body>
    <form runat="server">
        <h2>Calculator Version 2
        </h2>
        <asp:TextBox id="txtInput1" runat="server"></asp:TextBox>
        <asp:Button id="btnAdd" onclick="Calc" runat="server" Text=" + "></asp:Button>
        <asp:Button id="btnSubtract" onclick="Calc" runat="server" Text=" - "></asp:Button>
        <br />
        <asp:TextBox id="txtInput2" runat="server"></asp:TextBox>
        <asp:Button id="btnMultiply" onclick="Calc" runat="server" Text=" x "></asp:Button>
        <asp:Button id="btnDivide" onclick="Calc" runat="server" Text=" ÷ "></asp:Button>
        <br />
        <strong>Answer = <asp:Label id="lblAnswer" runat="server"></asp:Label></strong>
    </form>
</body>
</html>

本文地址:http://com.8s8s.com/it/it8564.htm