DataBinder绑定表达式
1) 普通的绑定表达式
<%# DataBinder.Eval(Container.DataItem, "ContactName") %>
2) 文本+绑定表达式
<asp:Label id=lblDate runat="server" Text='<%# "[" + DataBinder.Eval(Container, "DataItem.NewsCreatedate") + "]" %>' ForeColor="Red"></asp:Label>
3) 同时带有显示格式的绑定表达式
<%# DataBinder.Eval(Container,"DataItem.USActiveDate","{0:yyyy-MM-dd}") %>
4) 结合绑定表达式和模态框
<A href='<%# ShowModalWin(Convert.ToString(DataBinder.Eval(Container.DataItem, "PictureImage")),Convert.ToString(DataBinder.Eval(Container.DataItem, "DetailID")),Convert.ToString(DataBinder.Eval(Container.DataItem, "PictureID")))%>'>
其中:后台代码文件中ShowModalWin()方法的定义如下:
protected string ShowModalWin(string PictureImage,string DetailID,string PictureID)
{
return " window.showModalDialog(/"Customers/ShowPictureInfo.aspx?pid="+PictureImage+"&did="+DetailID+"&id="+PictureID+"/",/"/",/"dialogHeight:320px;dialogWidth:480px;center:yes;help:no;status:no;scroll:no/");";
}
或者将参数提取出来单独定义成一变量:
const string WINDOWPARAMSTRING="dialogWidth:540px;dialogHeight:420px;help:0;status:0;resizeable:1;scroll:no";
Page.RegisterStartupScript("functionscript","<script language='javascript'>window.showModalDialog('EditUserService.aspx?URID="+iURID+"','','"+WINDOWPARAMSTRING+"')</script>");
接收Bind的控件,一般有DropDownList,DataList,DataGrid,ListBox这些集合性质的控件,而被捆绑 的主要是ArrayList(数组),Hashtable(哈稀表),DataView(数据视图),DataReader这四个,以后我们就可以 对号入座,不会出现DataTable被捆绑的错误了:)
DataBind,获得的数据,系统会将其默认为String,要转化为其它的类型,就要利用如下表达式:
DataBinder.Eval(Container.DataItem,"转换的类型","格式")
最后一个"格式"是可选的,一般不用去管他,Container.DataItem是捆绑的数据项,"转换类型"指的是 Integer,String,Boolean这一类东西.
示例:
DataBinder.Eval(Container.DataItem,"char","{0}") ;
DataBinder.Eval(Container.DataItem,"float","{0:F}") ;
DataBinder.Eval(Container.DataItem,"date","{0:yyyy-MM-dd hh:mm:ss}") ;
格式化数值结果表可参考以下网址
亦可以使用转换函数,如:
DataBinder.Eval(Container.DataItem,"sex").ToString();
Convert.ToInt16(DataBinder.Eval(Container.DataItem,"sex"));
还可以使用code behide 里的自定义函数,如:
--aspx
<%# chg(DataBinder.Eval(Container.DataItem,"sex","{0}"))%>
--cs
protected string chg(string str)
//参数(str)的类型定义为上面表达式转换后的格式类型
//如果不作转换侧参数为object型
{
if(str=="1")
{
return "male";
}
else
{
return "female";
}
}