我正在使用C#和MS SQL开发消息墙,到目前为止,我能够使用转发器发布帖子并检索它们,现在我需要在每个转发器文本框的评论部分中发表评论element,为此我分配了一个post id来关联两个表。
问题在于我如何从每个转发器元素中检索post id,因此当我对comments表进行插入时,我可以将其包含在内,然后将所有内容一起检索。
这是我的相关代码:
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>
答案 0 :(得分:2)
一个非常快速的例子。这使用带CommandArgument
的按钮。有了它,您可以发送CommandName
和<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>
。在Repeater中,您可以将字段绑定到CommandArgument并在方法中再次读取它。
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "myCommand")
{
Label1.Text = e.CommandArgument.ToString();
}
}
背后的代码
<form class="form-horizontal">
请勿忘记删除这些TableView
代码。