【问题标题】:Checking for null database value in ASP Repeater control在 ASP 中继器控件中检查空数据库值
【发布时间】:2009-09-02 00:14:16
【问题描述】:

在这周之前,我从来没有写过一点 ASP.NET,所以请放轻松。

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

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

事实证明,这并不像我想象的那么容易。谁能给我建议:

A) 用于确定该行的“comment”列是否为空的显式测试

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

我相信我有能力编写按钮单击处理程序以使用评论更新数据库......如果我可以让它显示的话。

在此先感谢,我保证以后会拒绝 ASP.NET 项目!

【问题讨论】:

    标签: asp.net database repeater


    【解决方案1】:

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

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

    如果不为空,只需在“转换”前面打一个感叹号,当它不为空时显示。

    至于点击处理程序,由于“发送者”将是按钮,你总是可以做一个简单的

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

    你有你的文本框。

    【讨论】:

      【解决方案2】:

      如果评论字段在数据库中包含 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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多