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