数据绑定概述和语法 ASP.NET
引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例。
简单属性
Customer: <%# custID %> 集合 Orders: <asp:ListBox id="List1"
datasource='<%# myArray %>' runat="server"> 表达式 Contact: <%# (
customer.First Name + " " + customer.LastName ) %> 方法结果 Outstanding
Balance: <%# GetBalance(custID) %>
尽管该语法看起来与 ASP 的 Response.Write
快捷方式 <%= %> 相似,但其行为完全不同。ASP Response.Write 快捷方式语法在处理页时计算,而 ASP.NET
数据绑定语法仅在调用 DataBind 方法时计算。
DataBind 是页和所有服务器控件的方法。当在父控件上调用 DataBind
时,它级联到该控件的所有子控件。例如,DataList1.DataBind() 将因此对 DataList 模板中的每一控件调用 DataBind
方法。在页上调用 DataBind — Page.DataBind() 或只是 DataBind() — 会导致计算页上的所有数据绑定表达式。通常从
Page_Load 事件调用 DataBind,如下例所示。
protected void Page_Load(Object Src,
EventArgs E) { DataBind(); }
Protected Sub
Page_Load(Src As Object, E As EventArgs) DataBind() End
Sub
protected function Page_Load(Src:Object, E:EventArgs) : void
{ DataBind(); }
如果绑定表达式在运行时计算为预期的数据类型,则可以在 .aspx
页的声明节中的几乎任何位置使用绑定表达式。上面的简单属性、表达式和方法示例在计算时向用户显示文本。这些情况下,数据绑定表达式必须计算为 String
类型的值。在集合示例中,数据绑定表达式计算为 ListBox 的 DataSource
属性的有效类型值。您可能会发现有必要转换绑定表达式中的类型值以产生所需的结果。例如,如果 count 是整数:
Number of
Records: <%# count.ToString() %>
绑定到简单属性 ASP.NET
数据绑定语法支持绑定到公共变量、页的属性和页上其他控件的属性。
下面的示例说明如何绑定到公共变量和页上的简单属性。注意这些值在
DataBind()
调用前初始化。
<html> <head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
Page.DataBind();
}
string custID{ get
{ return
"ALFKI"; }
}
int orderCount{
get {
return 11;
} }
</script>
</head> <body>
<h3><font face="宋体">到页属性的数据绑定</font></h3>
<form runat=server>
客户:<b><%# custID %></b><br>
未结的订单:<b><%# orderCount
%></b>
</form>
</body> </html>
下面的示例说明如何绑定到另一控件的属性。
<html> <head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
//
仅调用“Page.DataBind”,而不是从“StateList” //
中显式取出变量,然后操作标签控件。 // 这将计算页内所有的 <%#
%> 表达式
Page.DataBind();
}
</script>
</head> <body>
<h3><font
face="宋体">到另一个服务器控件的属性的数据绑定</font></h3>
<form runat=server>
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="提交" onClick="SubmitBtn_Click"
runat=server/>
<p>
选定的州:<asp:label text='<%#
StateList.SelectedItem.Text %>' runat=server/>
</form>
</body> </html>
绑定到集合和列表
像 DataGrid、ListBox 和 HTMLSelect
这样的列表服务器控件将集合用作数据源。下面的示例说明如何绑定到通常的公共语言运行库集合类型。这些控件只能绑定到支持
IEnumerable、ICollection 或 IListSource 接口的集合。最常见的是绑定到
ArrayList、Hashtable、DataView 和 DataReader。
下面的示例说明如何绑定到 ArrayList。
<html> <head>
<script
language="C#" runat="server">
void
Page_Load(Object Sender, EventArgs E) {
if (!Page.IsPostBack) {
ArrayList values = new
ArrayList();
values.Add ("IN");
values.Add ("KS");
values.Add ("MD");
values.Add ("MI");
values.Add ("OR");
values.Add
("TN");
DropDown1.DataSource = values;
DropDown1.DataBind();
} }
void SubmitBtn_Click(Object sender, EventArgs e) {
Label1.Text = "您选择了:" +
DropDown1.SelectedItem.Text; }
</script>
</head> <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> </html>
下面的示例说明如何绑定到
DataView。注意 DataView 类在 System.Data 命名空间中定义。
<%@ Import
namespace="System.Data"
%>
<html> <head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e ) {
if
(!Page.IsPostBack) {
DataTable
dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("整数值",
typeof(Int32)));
dt.Columns.Add(new DataColumn("字符串值", typeof(string)));
dt.Columns.Add(new
DataColumn("日期时间值", typeof(DateTime)));
dt.Columns.Add(new DataColumn("布尔值",
typeof(bool)));
for (int i = 1; i <= 9; i++) {
dr =
dt.NewRow();
dr[0] = i;
dr[1] = "项 " +
i.ToString();
dr[2] = DateTime.Now;
dr[3] =
(i % 2 != 0) ? true : false;
dt.Rows.Add(dr);
}
dataGrid1.DataSource = new DataView(dt);
dataGrid1.DataBind();
} }
</script>
</head> <body>
<h3><font face="宋体">到 DataView
的数据绑定</font></h3>
<form
runat=server>
<asp:DataGrid
id="dataGrid1" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
HeaderStyle-BackColor="#aaaadd"
/>
</form>
</body> </html>
下面的示例说明如何绑定到
Hashtable。 <html> <head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
if (!Page.IsPostBack) {
Hashtable h = new Hashtable();
h.Add ("键 1", "值
1"); h.Add
("键 2", "值 2");
h.Add ("键 3", "值 3");
MyDataList.DataSource = h;
MyDataList.DataBind();
} }
</script>
</head> <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>
<%#
((DictionaryEntry)Container.DataItem).Key %> :
<%#
((DictionaryEntry)Container.DataItem).Value %>
</ItemTemplate>
</asp:DataList>
</form>
</body> </html>
|