<%@ Page Language="C#" AutoEventWireup="true" CodeFile="caculate.aspx.cs" Inherits="caculate" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>/</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="计算" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class caculate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int a = int.Parse(this.TextBox1.Text);//获取第一个文本框的输入值并转为整形
int b = int.Parse(this.TextBox2.Text);//获取第二个文本框的输入值并转为整形
switch (this.DropDownList1.SelectedValue) { //以下拉列表的选择符号为判断依据
case "+":
this.Label1.Text = (a + b).ToString();//运算后转为字符串赋值给标签控件文本显示
break;
case "-":
this.Label1.Text = (a - b).ToString();
break;
case "*":
this.Label1.Text = (a * b).ToString();
break;
case "/":
this.Label1.Text = (a / b).ToString();
break;
}
}
}