【IT168 技术文档】
<%#name%>:将name绑定到该属性;
1.简单数据绑定
在页面源代码中添加一个Label控件,将页面的Name属性绑定到该控件上
<asp:Label ID="Label1" runat="server" Text=<%#Name %>></asp:Label>
简单属性绑定
//cs代码:
//设置一个Name属性,可以绑定的属性必须有get
public string Name
{
get
{
return "姓名";
}
}
protected void Page_Load(object sender, EventArgs e)
{
//将数据源绑定到控件
Page.DataBind();
}
//设置一个Name属性,可以绑定的属性必须有get
public string Name
{
get
{
return "姓名";
}
}
protected void Page_Load(object sender, EventArgs e)
{
//将数据源绑定到控件
Page.DataBind();
}
2.表达式的绑定
在页面源代码上添加两个TextBox控件,一个Label控件,将两个TextBox的值相乘后显示在Label上
<asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server">0</asp:TextBox>
//Decimal 表示十进制数,Decimal 类型不会消除对舍入的需要,而是将因舍入而导致的错误降到最少
<asp:Label ID="Label1" runat="server" Text=<%#"总数为:"+Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToDecimal(TextBox2.Text)) %>></asp:Label>
<asp:TextBox ID="TextBox2" runat="server">0</asp:TextBox>
//Decimal 表示十进制数,Decimal 类型不会消除对舍入的需要,而是将因舍入而导致的错误降到最少
<asp:Label ID="Label1" runat="server" Text=<%#"总数为:"+Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToDecimal(TextBox2.Text)) %>></asp:Label>
页面cs代码:Page.DataBind(); 如果希望页面初始及绑定,就放在Page_Load中,如果希望点击按钮计算,就放在Button的Click事件中。