views:

440

answers:

2

I have never written a lick of ASP.NET in my life before this week so go easy on me, please.

I have an ASP.NET page that displays pictures from a database. I create a SqlDataReader and bind the repeater to this reader. The page displays a table with three columns: a thumbnail of the pic (path from the database), the name of the camera the pic was taken with (from the database), and a comment about the picture.

The comment will be null when I upload pics to the database. I want the table to display the comment if it exists, or if the comment field is null in the database, display an ASP textbox and button for the user to enter a comment about the picture.

It turns out this is not nearly as easy as I thought it would be. Can anyone suggest to me:

A) an explicit test for determining if the "comment" column of this row is null

B) how to conditionally display either text, or a textbox/button combination (texbox having an id from the "ID" column of the database)

I believe I have the skills to write the button click handler to update the database with the comment... if I can get it to display.

Many thanks in advance, and I promise to decline ASP.NET projects in the future!

+1  A: 

Since you're in a databinding context, you can use databinding expressions on the visible properties of a placeholder (or, any other control for that matter).

<asp:PlaceHolder runat="server" visible='<%# Convert.IsDbNull( Eval("comment") )%>'>
  <asp:TextBox ID="txtNewComment"... />
  <asp:Button ... />
</asp:PlaceHolder>

For not null, just slap an exclaimation point infront of the "Convert", and it shows when it's not null.

As for the clickhandler, since "sender" will be the button, you can always do a simple

((Button)sender).Parent.FindControl("txtNewComment")

And you have your textbox.

Alex Papadimoulis
A: 

You can use Alex's suggestion to display either the comment or the textbox/button combo if the comment field contains a NULL value in the database. I think you will need to bind to the 'OnItemCommand' to handle button clicks within the repeater.

Here's an example.

<asp:Repeater ID="myRepeater" runat="server" OnItemCommand="myRepeater_ItemCommand">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>    
        <tr>
            <td>
                <asp:Image ID="imgThumbNail" runat="server" ImageUrl='<%# Eval("path") %>' />
            </td>
            <td>
                <asp:Label ID="lblCamera" runat="server" Text='<%# Eval("camera") %>'></asp:Label>
            </td>
            <td>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Convert.IsDBNull( Eval("comment") )%>'>
                    <asp:Button ID="btnAddComment" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="AddComment" Text="Add Comment" />
                    <asp:TextBox ID="txtComment" runat="server" </asp:TextBox>             
                </asp:PlaceHolder>     
                <asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# !Convert.IsDBNull(Eval("comment"))%>'>
                    <asp:Label ID="lblComment" runat="server" Text='<%# Eval("comment") %>'></asp:Label>
                </asp:PlaceHolder>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "AddComment")
    {
        TextBox txtComment = (TextBox)e.Item.FindControl("txtComment");
        int id = Convert.ToInt32(e.CommandArgument);
        // use the record id to update the comment in the database with the value contained in the txtComment.Text property here
    }
}
Phaedrus