数据绑定中常用到格式化字串,呵呵,把MSDN及自己的收集整理了一下作为下一篇文章中,方便以后查找.

1.DataBinder.Eval()方法:
<%# DataBinder.Eval(Container.DataItem,"price","{0:c}")
3个参数:数据项目的容器名,数据字段名,格式字符串
在数据列表控件的模板中,容器名总为Container.DataItem;
Page是另一个常用的容器名
在不需要指定字符格式时少用(效率不高)
<asp:HyperLink BorderWidth="0" id=hylnk_newsp_image ImageUrl='<%# databinder.eval(container.dataitem, "logo") %>' NavigateUrl='<%# databinder.eval(container.dataitem, "id","pdfend.aspx?id={0}") %>' runat="server" Target="_blank" ToolTip='<%# databinder.eval(container.dataitem, "edit_time","{0:D}") %>'></asp:HyperLink>

2.普通绑定:
<asp: TextBox id="txtTitle" runat="server" Text='<%# Container.DataItem("title")%'/>

<%=expression%> 即Response.Write的简写形式
'///////////////////////////
DataBind()为Page对象及所有服务器控件都具有的方法.
父控件的DataBind()调用将激发其所有子控件的DataBind()方法.
如:DataList1.DataBind()将激发该控件模板内所有控件的DataBind()方法
如果激发的为Page.dataBind()则可简写为DataBind()
最佳位置:Page_Load中.
'////////////////////////
可以同时转换数据类型:
Text='<%# count.ToString()>'
'//////////////////////////////
绑定到集合:
例下拉列表的绑定
dim arrcity as ArrayList=new ArrayList()
arrCity.Add("北京")
arrCity.Add("上海")
arrCity.Add("南京")
arrCity.Add("广州")
DropDown1.DataSource=arrCity
DropDown1.DataBind
'/////////////////////////////////////
绑定到表达式或函数:
dim arrcity as ArrayList=new ArrayList()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
arrCity.Add("北京")
arrCity.Add("上海")
arrCity.Add("南京")
arrCity.Add("广州")
dtl.DataSource=arrCity
dtl.DataBind
    End Sub
Function Zone( strCity as string) as string
if strcity="北京" then
Return("北方")
else
Return("南方")
end if
end Function

<asp: DataList id="dtl" runat=server>
<template name="ItemTemplate">
城市:<%#container.dataitem%>
地区:<%#Zone(container.dataitem)%>
</asp:datalist>
'//////////////////////////////
绑定属性:
readonly Property Name() as string
Get
Return("小明")
End Get
End Property

Sub Page_load(....)
Page.DataBind()
End sub

姓名:<%# Name %>
'/////////////////////////////
绑定选择结果
<asp: label id="label1" text='<%# selCity.SelectedItem.Text%>' Runat=Server/>
<asp: DropDownList id="selCity" Runat=Server>
<asp: ListItem>北京</asp:ListItem>
<asp: ListItem>南京</asp:ListItem>
<asp: ListItem>上海</asp:ListItem>
<asp: ListItem>广州</asp:ListItem>
</asp:DropDownList >
'/////////////////////////////