我有一个评论系统,我想插入喜欢的表格 点击按钮后。
问题是我不知道如何在特定评论中插入类似的内容 用户点击了。
评论系统是一个asp.net转发器。
<asp:Repeater ID="repRequests" runat="server">
<ItemTemplate>
<div class="media CommentHeader">
<h6 class="media-heading">
<asp:Label ID="HeaderUserCom" Style="font-weight: 600; font-size: 14px;" runat="server" Text='<%#Eval("user_name")%>'></asp:Label>
<asp:Label ID="UserCom" runat="server" Style="font-weight: 600" Text='<%#Eval("comment")%>'></asp:Label>
<asp:Button ID="btnLike" runat="server" Text="Like" OnClick="btnLike_Click1" />
</div>
</ItemTemplate>
/asp:Repeater>
c#(将like插入到comment id = @comment id的likes表中)
protected void btnLike_Click1(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("insert into likes
select users.user_id, comments.comment_id where users.userName = @userName comments.comment_id = @comment", con);
cmd.Parameters.AddWithValue("@userName", Session["userName"].ToString());
cmd.Parameters.AddWithValue("@comment_id", **what to put here ??**);
con.Open();
cmd.ExecuteNonQuery();
}
}
答案 0 :(得分:1)
您的OnClick事件需要与方法名称匹配。您有btnLike_Click
和btnLIke_Click1
。让它们匹配。
要传递注释ID,您可以将其设置为按钮上的命令参数。假设您将转发器绑定到的数据源中的“comment_id”字段可用。
<asp:Button ID="btnLike" runat="server" Text="Like" bOnClick="btnLike_Click" CommandArgument='<%#Eval("comment_id")%>' />
然后在事件处理程序中检索它。
protected void btnLike_Click1(object sender, EventArgs e)
{
Button btn=sender as Button;
if(btn!=null)
{
string comment_id=btn.CommandArgument;
//perform the database insertion here
}
}