从 Repeater 获取 ID

Obtaining ID from Repeater

我正在用 C# 和 MS SQL 开发一个消息墙,到目前为止我可以创建 posts 并使用中继器检索它们,现在我需要在每个转发器文本框元素的评论部分,为此我分配了一个 post id 来关联两个 tables.

问题是我无法从每个转发器元素中检索 post id,因此当我插入评论时 table 我可以包含它,然后再包含它一起找回所有东西。

这是我后面的相关代码:

public void postear()
{
    string emailcc = Session["EMAIL"].ToString();
    string user_id = Session["ID"].ToString();
    string usrnom = Session["NOMBRE"].ToString();
    string usrfoto = Session["FOTO_URL"].ToString();

    string post_contenido = txtpublica.Text.ToString();
    var post_fecha = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBD"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = (@"INSERT INTO MIEMBROS_Posts (POST_USER_ID, POST_CONTENIDO, POST_FECHA, POST_USER_NOMBRE, POST_USER_FOTO) VALUES ('"
            + user_id + "','" + post_contenido + "','" + post_fecha + "','" + usrnom + "','" + usrfoto + "');");
            cmd.Connection = conn;
            conn.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
        }
    }
    txtpublica.Text = "";
    traerposts();
}



public void comentar()
{
    string emailcc = Session["EMAIL"].ToString();
    string user_id = Session["ID"].ToString();
    string usrnom = Session["NOMBRE"].ToString();
    string usrfoto = Session["FOTO_URL"].ToString();

 // This is the problem area 
    foreach (RepeaterItem item in Repeater_UsrPosts.Items)
    {
        Label lbluserID = (Label)item.FindControl("lbluserid");
        string userid_post = lbluserID.Text;
    }

    string buscaid = (string)Repeater_UsrPosts.Items[lblid].FindControl("lbluserid");

    string COMM_contenido = txtpublica.Text.ToString();
    var COMM_fecha = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBD"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = (@"INSERT INTO MIEMBROS_Comments (COMM_USER_ID, COMM_CONTENIDO, COMM_FECHA, COMM_USER_NOMBRE, COMM_USER_FOTO, COMM_POST_ID) VALUES ('"
            + user_id + "','" + COMM_contenido + "','" + COMM_fecha + "','" + usrnom + "','" + usrfoto + "','" + userid_post + "');");
            cmd.Connection = conn;
            conn.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
        }
    }
    txtpublica.Text = "";
    traerposts();
}

和aspx:

<asp:Repeater ID="Repeater_UsrPosts" runat="server">
    <ItemTemplate>

        <!-- Post -->
        <div class="post clearfix">
            <div class="user-block">

                <asp:Label ID="lblid" runat="server" Text="<%#Eval("post_user_id")%>"></asp:Label>
                <img alt="" src="<%#Eval("post_user_foto")%>" class="img-circle img-bordered-sm" />

                <span class="username">
                    <a href="#"><%#Eval("post_user_nombre") %></a>
                    <a href="#" class="pull-right btn-box-tool"><i class="fa fa-times"></i></a>
                </span>
                <span class="description"><%#Eval("post_fecha") %></span>
            </div>
            <!-- /.user-block -->

            <p>

                <%#Eval("post_contenido") %>
            </p>

            <form class="form-horizontal">
                <div class="form-group margin-bottom-none">
                    <div class="col-sm-9">
                        <input class="form-control input-sm" placeholder="Respuesta">
                    </div>
                    <div class="col-sm-3">
                        <button type="submit" class="btn btn-danger pull-right btn-block btn-sm">Enviar</button>
                    </div>
                </div>
            </form>
        </div>
        <!-- /.post -->
    </ItemTemplate>
</asp:Repeater>

一个非常简单的例子。这使用带有 OnCommand 的按钮。这样你就可以发送一个 CommandArgument 和一个 CommandName。在 Repeater 中你可以绑定一个字段到 CommandArgument 并在方法中再次读取它。

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <%# Eval("myName") %> - <asp:Button ID="Button1" runat="server" Text="Button"
            OnCommand="Button1_Command" CommandName="myCommand"
            CommandArgument='<%# Eval("ID") %>' />
        <br />
    </ItemTemplate>
</asp:Repeater>

代码隐藏

protected void Button1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "myCommand")
    {
        Label1.Text = e.CommandArgument.ToString();
    }
}

别忘了删除那些 <form class="form-horizontal"> 标签。