繁体   English   中英

如何使用c#asp.net循环中继器控件内部的div部分?

[英]how to loop div section which is inside repeater control using c# asp.net?

我正在使用带有多个<div>的转发器控件,我获得了除注释之外的所有值。

div 只获取单个记录。 尽管有中继器,但值没有循环。 有什么解决方案可以让我重复评论部分吗?

请问有什么帮助吗?

我的标记:

<asp:Repeater ID="rpt_post" runat="server" OnItemDataBound="rpt_post_ItemDataBound" >
    <ItemTemplate>
        <div class="user-thumb">
            <img src='<%# Eval("Profile_Pic") %>'  class="img-responsive" />
        </div>
        <div class="user-information">
            <p><%# Eval("Name") %> </p>
            <small><%# Eval("Timestamp") %> </small>
            <asp:Label ID="lblpost_id" runat="server" Visible="false" Text='<%# Eval("Post_Id") %>' > 
            </asp:Label>
        </div>
        <div class="comment col-md-12">//this section is not repeating
            <h4>taruni </h4>
                <asp:Label runat="server" ID="lblcomment" Text='<%# Eval("Comment") %>'></asp:Label>
    </ItemTemplate>
</asp:Repeater> 
      

代码隐藏:

protected void rpt_post_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    try
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            SqlConnection con = new SqlConnection(constr);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT pc.Comment, pc.Post_Id FROM Post_Comment pc INNER JOIN Posts kp ON kp.Post_Id = pc.Post_Id WHERE pc.Post_Id = @postid";
            cmd.Parameters.AddWithValue("@postid", postid);
            cmd.Connection = con;

            SqlDataAdapter da = new SqlDataAdapter();

            con.Open();
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                Label lblcom = (Label)e.Item.FindControl("lblcomment");
                lblcom.Text = dt.Rows[0]["Comment"].ToString();
            }
        } 
    }
}

您没有使用中继器

这是你的代码

               if (dt.Rows.Count > 0)
                {
                    Label lblcom = (Label)e.Item.FindControl("lblcomment");
                    lblcom.Text = dt.Rows[0]["Comment"].ToString();
                }

您正在将该标签的文本设置为第一条评论。 这就是为什么您只看到一条评论。

你可能想要这样的东西:

    rpt_post.DataSource = dt;  
    rpt_post.DataBind();  

还有这个

select pc.Comment,pc.Post_Id 
from Post_Comment pc
INNER JOIN Posts kp on kp.Post_Id=pc.Post_Id 
 where pc.Post_Id=@postid;

和这个一样

select pc.Comment,pc.Post_Id 
from Post_Comment pc
where pc.Post_Id=@postid;

将数据行放在 DataRowView 中,然后获取/设置您的评论

  //put in row datarow view
  DataRowView dataRow = (DataRowView)e.Item.DataItem;

  //get your label
  Label lblcom = (Label)e.Item.FindControl("lblcomment");

  //set the label 
  lblcom.Text = dataRow["Comment"].ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM