views:

13

answers:

1

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>
            <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.

+1  A: 

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>
jarrett
Thanks. This gives me more to think about. I cannot see how this code snippet would generate a Link for the case where "ImageDesc" is null or empty. Looks to me like it generates only a DIV with text element value of "My pseudo-descriptor". Need to be able to link to photos that either have captions or don't.
John Galt
That is correct. I didn't really understand what you wanted inside that div, but you can replace that text with anything. You seem to understand databinding already so I figured it wouldn't be a problem for you. Please clarify your question if you need more help. If you want to know how to put an index number in your datasource then please post the code for your datasource.
jarrett
I see what you mean now, Jarrett. I understand databinding only at the most basic level. I was thinking it is probably a better practice to put logic statements like this in the code behind file but I don't know how to do that. Do you have any recommendations of any great articles that explain this deeper level of databinding?
John Galt