您当前位置: 拓展知识> 数据绑定----bind与Eval

数据绑定----bind与Eval

发布时间:2016-05-30 11:04 浏览次数:6769    
字体大小 默认
  • 默认
  • 13pt
  • 14pt
  • 15pt
  • 16pt
  • 17pt
  • 18pt
  • 19pt
  • 20pt
  • 21pt
  • 22pt
  • 23pt
  • 24pt
  • 25pt
字体颜色

默认

  • 默认
背景颜色

默认

  • 默认

数据绑定----bindEval

Asp.net中DataBinder.Eval用法的總結

適合元件: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}") %>


資料繫結須知

<%# %> 語法

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中的EvalDataBinder.Eval方法

bind是双向绑定,但需数据源可更改才能用。ASP.NET 2.0改善了模板中的数据绑定操作,把v1.x中的数据绑定语法DataBinder.EvalContainer.DataItem fieldname)简化为Evalfieldname)。Eval方法与DataBinder.Eval一样可以接受一个可选的格式化字符串参数。缩短的Eval语法与DataBinder.Eval的不同点在于,Eval会根据最近的容器对象(例如DataListItem)的DataItem属性来自动地解析字段,而DataBinder.Eval需要使用参数来指定容器。由于这个原因,Eval只能在数据绑定控件的模板中使用,而不能用于Page(页面)层。当然,ASP.NET 2.0页面中仍然支持DataBinder.Eval,你可以在不支持简化的Eval语法的环境中使用它。

下面的例子演示了如何使用新的简化的Eval数据绑定语法绑定到DataList数据项模板(ItemTemplate)中的ImageLabelHyperLink控件。

以下为引用的内容:

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.

 

自己实际使用中的脚本:

 

<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:yyyyMd}").ToString() %>' />

                  <asp:Label ID="newstitleLabel" runat="server" Text='<%# Eval("newstitle").ToString() %>'/>

           <!-- <asp:Label ID="newstitleLabel2" runat="server" Text='<%# (Eval("newstitle").ToString()+"???"+ Eval("itemdate","{0:yyyyMd}").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>

 

 

引用網址: http://blog.csdn.net/happymagic/article/details/6732853

 

操作成功!此窗口3秒钟后自动关闭!
立即关闭