2009-09-02 56 views
1

在本周之前,我从来没有在我的生活中写过一缕ASP.NET,所以请在我身上轻松一下。检查ASP Repeater控件中的空数据库值

我有一个ASP.NET页面显示来自数据库的图片。我创建一个SqlDataReader并将转发器绑定到这个阅读器。该页面显示一个包含三列的表格:pic的缩略图(来自数据库的路径),照片的拍摄名称(来自数据库)以及有关照片的注释。

当我上载图片到数据库时,评论将为空。我希望表格显示注释(如果存在),或者注释字段在数据库中为空,则显示ASP文本框和按钮以供用户输入关于图片的注释。

事实证明这并不像我想象的那么容易。任何人都可以建议我:

A)的明确的测试以确定是否该行的“注释”一栏为空

B)如何有条件地显示任何文本或文本框/按钮组合(有texbox来自数据库的“ID”列的ID)

我相信我有技巧来编写按钮点击处理程序来更新数据库与评论...如果我能得到它显示。

非常感谢,并承诺未来会拒绝ASP.NET项目!

回答

2

由于您处于数据绑定上下文中,因此可以在占位符(或任何其他控件)的可见属性上使用数据绑定表达式。

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

对于not null,只需要拍一下“Convert”的exclaimation point,它会显示它何时不是null。

至于对clickHandler,因为“发件人”将是按钮,你总是可以做一个简单的

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

,你有你的文本框。

0

如果注释字段在数据库中包含NULL值,则可以使用Alex的建议来显示注释或文本框/按钮组合。我想你需要绑定到'OnItemCommand'来处理中继器中的按钮点击。

下面是一个例子。

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