CSDN博客

img bgu

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

发表于2004/9/21 20:03:00  750人阅读

<%@ 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>

0 0

相关博文

我的热门文章

img
取 消
img