適合元件:
DataList, Repeater, 或想使用String Format的字串也可以參考
<%# Bind("Subject") %> //綁定字段
<%# Container.DataItemIndex + 1%> //實現自動編號
<%# Container.ItemIndex %> //Repeater自動編號
<%# DataBinder.Eval(Container.DataItem, "[n]") %>
通常使用的方法(這三個性能最好)
<%# DataBinder.Eval(Container.DataItem, "ColumnName") %>
<%# DataBinder.Eval(Container.DataItem, "ColumnName", null) %>
<%# DataBinder.Eval(Container, "DataItem.ColumnName", null) %>
其他用法
<%# ((DataRowView)Container.DataItem)["ColumnName"] %>
<%# ((DataRowView)Container.DataItem).Row["ColumnName"] %>
<%# ((DataRowView)Container.DataItem)["adtitle"] %>
<%# ((DataRowView)Container.DataItem)[n] %>
<%# ((DbDataRecord)Container.DataItem)[0] %>
<%# (((自定義類型)Container.DataItem)).屬性.ToString() %>//如果屬性為字符串類型就不用ToString()了
DataBinder.Eval用法範例
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式化字符串參數是可選的。如果忽略參數,DataBinder.Eval 返回對像類型的值,
//顯示二位小數
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>
//{0:G}代表顯示True或False
AlternateText='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %>'
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "~/images/{0:G}.gif") %>' />
//轉換類型
((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4)
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
{0:c} 貨幣樣式
<%#Container.DataItem("price","{0:¥#,##0.00}")%>
<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>
Specifier Type Format Output (Passed Double 1.42) Output (Passed Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal {0:d} System.FormatException -12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.FormatException
x Hexadecimal {0:x4} System.FormatException cf90
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
樣式取決於 Web.config 中的設置
{0:c} 或 {0:£0,000.00} 貨幣樣式 標準英國貨幣樣式
顯示為 £3,000.10
{0:c} 或 string.Format("{0:C}", price); 中國貨幣樣式
顯示為 ¥3,000.10
{0:c} 或 string.Format("{0:C}", price); 美國貨幣樣式
資料繫結須知
<%# %> 語法
ASP.NET 引進新的宣告語法:<%# %>。這是 .aspx 網頁中使用資料繫結的基本語法。這些字元必須包含所有資料繫結運算式。下列清單包含多重來源的簡單資料繫結範例:
簡單屬性 (客戶的語法):
<%# custID %>
集合 (訂單的語法):
<asp:ListBox id="List1" datasource='<%# myArray %>' runat="server">
運算式 (連絡人的語法):
<%# ( customer.First Name + " " + customer.LastName ) %>
方法結果 (未清餘額的語法):
<%# GetBalance(custID) %>
上述範例中,<%# %> 標籤會指出特定資料來源資訊在 .aspx 網頁中的位置。下列資料繫結範例會使用TextBox Web 伺服器控制項:
<asp:textbox id=txt text="<%# custID %>" runat=server />
Eval(" ")绑定两个字段:
CommandArgument='<%#Eval("dyid").ToString()+Eval("dyid1").ToString()+… %>'这种形式就行
你把Eval("").ToString()当成一个普通字符串,而'<%# %>'保留就行,你高兴怎么处理就怎么处理,
所有字符串的操作都有效,你还可以用静态函数来处理这些字符串。
<asp:TextBox ID="TextBox5" runat="server" class="inputwidth100"
Text='<%# Eval("RZMJ").ToString()+Eval("LDDW").ToString() %>'></asp:TextBox>
ASP.NET中的Eval和DataBinder.Eval方法
bind是双向绑定,但需数据源可更改才能用。ASP.NET 2.0改善了模板中的数据绑定操作,把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fieldname)。Eval方法与DataBinder.Eval一样可以接受一个可选的格式化字符串参数。缩短的Eval语法与DataBinder.Eval的不同点在于,Eval会根据最近的容器对象(例如DataListItem)的DataItem属性来自动地解析字段,而DataBinder.Eval需要使用参数来指定容器。由于这个原因,Eval只能在数据绑定控件的模板中使用,而不能用于Page(页面)层。当然,ASP.NET 2.0页面中仍然支持DataBinder.Eval,你可以在不支持简化的Eval语法的环境中使用它。
下面的例子演示了如何使用新的简化的Eval数据绑定语法绑定到DataList数据项模板(ItemTemplate)中的Image、Label和HyperLink控件。
以下为引用的内容:
<asp:DataList
ID= "DataList1 " RepeatColumns = "5 " Width= "600 " runat= "server " DataSourceID = "ObjectDataSource1 "> <ItemTemplate> <asp:HyperLink ID= "HyperLink1 " runat = "server " NavigateUrl= '<%# Eval( "PhotoID ", "PhotoFormViewPlain.aspx?ID={0} ") %>
'> <asp:Image ID= "Image1 " Runat = "server " ImageUrl= '<%# Eval( "FileName ", "images/thumbs/{0} ") %> ' /></asp:HyperLink> <asp:Label ID= "CaptionLabel " runat= "server " Text = '<%# Eval( "Caption ") %> ' /> </ItemTemplate>
</asp:DataList><br /> <asp:ObjectDataSource ID= "ObjectDataSource1 " runat = "server " TypeName = "DataComponentTableAdapters.PhotosTableAdapter " SelectMethod= "GetPhotosForAlbum ">
据绑定也可以作为控件的主题定义(theme definition)的一部分,这样我们就可以通过改变主题来随意地改变模板化控件的布局和外观。但是Theme(主题)模板中只能使用Eval(或者后面讨论的Bind)。绑定到任意的用户代码是被禁止的。
using if with eval("") in asp.net
I am using repeater to display the news on the news section. In my news section i have got 2 labels(title, Description) and one image field. Below is the code which i am using to populate the repeater:
<asp:Repeater ID="rptNews" runat="server">
<ItemTemplate>
<asp:Image ID="newsImage" runat="server" ImageUrl='<%#String.format("../Images/News/{0}", Eval("newsImage")) %>' />
<asp:Label ID="newsTitle" runat="server" Text='<%#Eval("newsTitle") %>'></asp:Label>
<br />
<asp:Label ID="newsDescription" runat="server" Text='<%#Eval("newsDescription") %>'></asp:Label>
<br />
<div class="clear"> </div>
</ItemTemplate>
</asp:Repeater>
I want to use if statement with the , for instance if the Eval("newsImage") is null then i want to disable the image control and just show the title and description of news . Any suggestions on how to acheive this.
should be like... Visible='<%# Eval("newsImage").ToString() != "Null" %>'
<asp:Image ID="newsImage" runat="server" Visible='<%# Eval("newsImage").ToString() == "Null" %>' ImageUrl='<%#String.format("../Images/News/{0}", Eval("newsImage")) %>' />
Maybe you could add visible to the Image, like this:
<asp:Image ID="newsImage" runat="server" Visible='<%# Eval("newsImage").ToString().Length > 0 %>' ImageUrl='<%#String.format("../Images/News/{0}", Eval("newsImage")) %>' />
If an image name exists then Visible will be true, otherwise false.
If you want to check for null than you need check for DBNull.Value and not just null
<asp:Image ID="newsImage" runat="server" Visible='<%# Eval("newsImage") != DBNull.Value %>' ImageUrl='<%#String.format("../Images/News/{0}", Eval("newsImage")) %>' />
Add the Visible attribute to your Image tag:
Visible="<%# Eval("newsImage") != null %>"
Although in such cases it might be better to use the ItemDataBound event, it's very easy to use.
自己实际使用中的脚本:
[html] view plaincopy
<div class="bound">
<div class="latest-news">
<div class="title"><img alt=""
src="style/hw_090457.gif" /></div>
<div class="content">
<asp:DataList ID="DataList1" runat="server" DataKeyField="news_ID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<div><a href="http://10.0.0.5/NewsView.aspx?newsid=<%# Eval("news_ID")%>">
<asp:Label ID="newsdateLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"itemdate","{0:yyyy年M月d日}").ToString() %>' />:
<asp:Label ID="newstitleLabel" runat="server" Text='<%# Eval("newstitle").ToString() %>'/>
<!-- <asp:Label ID="newstitleLabel2" runat="server" Text='<%# (Eval("newstitle").ToString()+"???"+ Eval("itemdate","{0:yyyy年M月d日}").ToString()) %>'/> -->
</a></div>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:intranet %>"
SelectCommand="SELECT TOP 3 * FROM [news] ORDER BY [itemdate] DESC"></asp:SqlDataSource>
</div></div></div>
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow