ASP.NET 引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例。
简单属性 | Customer: <%# custID %> |
集合 | Orders: <asp:ListBox > |
表达式 | Contact: <%# ( customer.First Name + " " + customer.LastName ) %> |
方法结果 | Outstanding Balance: <%# GetBalance(custID) %> |
1. 绑定到属性
C#:
void Page_Load(Object sender, EventArgs e)
}
}
ASP:
<body>
<h3><font face="宋体">到页属性的数据绑定</font></h3>
<form runat=server>
<h3><font face="宋体">到页属性的数据绑定</font></h3>
<form runat=server>
2. 绑定到集合和列表
绑定到Array List
C# :
void Page_Load(Object Sender, EventArgs E)
}
}
ASP:
<body>
<h3><font face="宋体">数据绑定 DropDownList</font></h3>
<form runat=server>
<asp:DropDownList id="DropDown1" runat="server" />
<asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
<p>
<asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat="server" />
</form>
</body>
<h3><font face="宋体">数据绑定 DropDownList</font></h3>
<form runat=server>
<asp:DropDownList id="DropDown1" runat="server" />
<asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
<p>
<asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat="server" />
</form>
</body>
绑定到Hash Table
C#:
void Page_Load(Object sender, EventArgs e)
}
}
ASP:
3. 绑定到方法或表达式
<body>
<h3><font face="宋体">到哈希表的数据绑定</font></h3>
<form runat=server>
<asp:DataList id="MyDataList" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0"
>
<ItemTemplate>
<h3><font face="宋体">到哈希表的数据绑定</font></h3>
<form runat=server>
<asp:DataList id="MyDataList" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0"
>
<ItemTemplate>
绑定时的表达式:<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>
C#:
void Page_Load(Object Src, EventArgs E)
}
}
ASP:
<body>
<h3><font face="宋体">到方法和表达式的数据绑定</font></h3>
<form runat=server>
<asp:DataList id="DataList1" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
>
<ItemTemplate>
<h3><font face="宋体">到方法和表达式的数据绑定</font></h3>
<form runat=server>
<asp:DataList id="DataList1" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
>
<ItemTemplate>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1716585