How to affect differences in row content when binding a dataset to a ListView in ASP.NET 3.5
I have added a ListView to a web form and this code works when the data columns in the bound dataset have values:
<asp:ListView ID="picturesListView" runat="server"
GroupPlaceholderID="groupsGoHere"
ItemPlaceholderID="itemsGoHere"
GroupItemCount="1" >
<LayoutTemplate>
<p id="pictureList">Picture List:</p>
<ol id="imagegallery">
<asp:PlaceHolder ID="groupsGoHere" runat="server" />
</ol>
</LayoutTemplate>
<GroupTemplate>
<asp:PlaceHolder runat="server" ID="itemsGoHere" />
</GroupTemplate>
<ItemTemplate>开发者_C百科;
<li>
<a href="<%# Eval("ImageURL")%>" title="<%#Eval("ImageDesc") %>">
<%#Eval("ImageDesc")%>
</a>
</li>
</ItemTemplate>
</asp:ListView>
Then in the codebehind file, I have just these 2 lines to populate the Listview:
picturesListView.DataSource = dsImages
picturesListView.DataBind()
All rows in the dataset have a value for the ImageURL column but the ImageDesc column can be null or an empty string. How could I check to see if ImageDesc for a given row is empty and construct a "pseudo-descriptor" such as "untitled picture no. N" where N is the relative row number in the dataset. I suppose I could change the stored procedure that returns this dataset...but this seems as if it should be possible.
This could be done more gracefully, but should answer your question:
<div runat="server" visible='<%# !string.IsNullOrEmpty(Eval("ImageDesc","{0}") )%>' >
<a href="<%# Eval("ImageURL")%>" title="<%#Eval("ImageDesc") %>">
<%#Eval("ImageDesc")%>
</a>
</div>
<div runat="server" visible='<%# string.IsNullOrEmpty(Eval("ImageDesc","{0}") )%>' >
My psuedo-descriptor
</div>
精彩评论