Here is the Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="A1Example.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<h1>Calculator Example</h1>
<div>
<table>
<tr>
<td colspan="2">
<asp:TextBox ID="txtDisplay" runat="server">
</asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="btn1" runat="server" Text="1"
CssClass="button" OnClick="number_click" /></td>
<!--These buttons share the same event-->
<td>
<asp:Button ID="btn2" runat="server" Text="2"
CssClass="button" OnClick="number_click"/></td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="+" CssClass="button"
onclick="btnAdd_Click"/></td>
<td>
<asp:Button ID="btnClear" runat="server"
Text="Clear" CssClass="button"/></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnEqual" runat="server" Text="="
CssClass="button"
onclick="btnEqual_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void number_click(object sender, EventArgs e)
{
//this handles the number buttons
//determine which number was clicked
//and get the text from it
Button b = (Button)sender;
txtDisplay.Text += b.Text; //add to existing text
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//get the number from the textbox
//using the GetNumber() method
double num = GetNumber();
//check to see if there is a number in the session
if (Session["number"] != null)
{
//add the new number to the one in the session
double n1 = (double)Session["number"];
Session["number"] = n1 + num;
}
else
{
//otherwise just the store number
Session["number"] = num;
}
//clear the text
txtDisplay.Text = "";
//store the operator in the session
Session["operator"] = "+";
}
protected void btnEqual_Click(object sender, EventArgs e)
{
//get the number stored in the session
double num1 = (double)Session["number"];
//get the last operator
string op = Session["operator"].ToString();
//Get the number from the text box
double num2 = GetNumber();
//test which it operator is
if(op.Equals("+"))
{
//do the operation and display it
txtDisplay.Text=(num1 + num2).ToString();
}
if(op.Equals("*"))
{
txtDisplay.Text=(num1* num2).ToString();
}
//clear the sessions
Session["number"] = null;
Session["operator"] = null;
}
protected double GetNumber()
{
//get the value from the text box
string number = txtDisplay.Text;
//double num = double.Parse(number);
double num=0;
//try to parse the number
//if it is good it returns true and assigns the value to num
bool isGood = double.TryParse(number, out num);
//if false let the user know
if (isGood == false)
{
//creates a javascript alert
Response.Write
("<script type='text/JavaScript'>alert('Learn your numbers');</script>");
txtDisplay.Text = "";
}
//return the number
return num;
}
}
A1Stylesheet.css
body {
}
td
{
width:30px;
}
p
{
font-size: large;
}
h1
{
color:Navy;
font-size:150%;
}
.button
{
width:40px;
}
No comments:
Post a Comment