Ghaamae
Ghaamae

Reputation: 81

Obtaining ID from Repeater

I'm developing a message wall with C# and MS SQL, so far I'm able to make posts and retrieve them using a repeater, now I need to make comments in the comment section on every repeater textbox element, for this I assigned a post id to relate both tables.

The problem is that I'm stuck in how to retrieve the post id from each repeater element, so that when I make an insert to the comments table I can include it and afterwards retrieve everything together.

This is my related code behind:

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();
}

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

Upvotes: 0

Views: 572

Answers (1)

VDWWD
VDWWD

Reputation: 35514

A very quick example. This uses a button with an OnCommand. With that you can send a CommandArgument and a CommandName. In the Repeater you can bind a field to the CommandArgument and read it again in the method.

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

Code behind

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

Don't forget to remove those <form class="form-horizontal"> tags.

Upvotes: 3

Related Questions