数据绑定的格式
<%#operation(ddlOperator.SelectedValue) %>
新建网站:
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2">
绑定方法调用的结果</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
第一个数:</td>
<td style="width: 100px">
<asp:TextBox ID="txtNum1" runat="server" Width="60px">0</asp:TextBox></td>
<td style="width: 100px">
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNum1"
ErrorMessage="输入数字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td>
</tr>
<tr>
<td style="width: 100px">
第二个数:</td>
<td style="width: 100px">
<asp:TextBox ID="txtNum2" runat="server" Width="60px">0</asp:TextBox></td>
<td style="width: 100px">
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtNum2"
ErrorMessage="输入数字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td>
</tr>
<tr>
<td style="width: 100px; height: 24px">
运算符号:</td>
<td style="width: 100px; height: 24px">
<asp:DropDownList ID="ddlOperator" runat="server">
<asp:ListItem>--请选择运算符号--</asp:ListItem>
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>/</asp:ListItem>
</asp:DropDownList></td>
<td style="width: 100px; height: 24px">
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
<asp:Button ID="btnOk" runat="server" Text="确定" /></td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 100px">
运算结果:</td>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text='<%#operation(ddlOperator.SelectedValue) %>'/></td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
public string operation(string VarOperator)
{
double num1=Convert.ToDouble (txtNum1.Text);
double num2=Convert.ToDouble (txtNum2.Text);
double result = 0;
switch (VarOperator)
{
case "+":
result = num1 + num2;
break ;
case "-":
result = num1 - num2;
break ;
case "*":
result = num1 * num2;
break ;
case "/":
result = num1 / num2;
break ;
}
return result.ToString ();
}
}