我正在创建一个大学网站,有点像社交网站,人们可以在彼此的墙上发布剪贴画。我希望在每个废料中包括Like /不同或投票/投票。我创建了这个名为的表 tbl_like ......
LikeId | ScrapId | FromId | LikeStatus |时间
废料表: ScrapId | FromId | toId | msg | sendDate
接下来我该怎么办?
<asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False"
GridLines="None" Width="100%" ShowHeader="False" runat="server" AlternatingRowStyle-BackColor="#A5A5A5"
CellPadding="4" ForeColor="#333333">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="left" cellpadding="1" cellspacing="2">
<tr>
<td>
<a href='<%#getUserHREF(Container.DataItem)%>'>
<img align="middle" src='<%#getSRC(Container.DataItem)%>' border="0" width="50px" /></a>
</td>
<td>
</td>
</tr>
</table>
<div align="justify">
<%#DataBinder.Eval(Container.DataItem, "Message")%>
<br />
<br />
</div>
<span class="SmallBlackText">Posted On: </span>
<asp:Label ID="lblSendDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SendDate")%>'></asp:Label>
</span>
<br />
<asp:LinkButton ID="lnklike" runat="server">Like</asp:LinkButton>
<asp:LinkButton ID="lnkunlike" runat="server">unlike</asp:LinkButton>
<asp:Label ID="lbllike" runat="server" Text="likes:"></asp:Label>
<asp:Label ID="lbllikecount" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
}
}
public void GetUserScraps(int Id)
{
string getUserScraps = "SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [User] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" + Request.QueryString["Id"].ToString() + "'";
string getlikes = "select COUNT(*) from tbl_like inner join Scrap on tbl_like.scrapid=Scrap.Id where tbl_like.likestatus=1 and tbl_like.scrapid=23";
dt1=
dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
if (dt.Rows.Count > 0)
{
GridViewUserScraps.DataSource = dt;
GridViewUserScraps.DataBind();
}
}
答案 0 :(得分:1)
对OnItemCommand
使用GridView
事件。
在你的HTML中:
<asp:GridView .... OnItemCommand="CommandLinkClicked">
<asp:LinkButton ID="lnklike" runat="server" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>'>Like</asp:LinkButton>
<asp:LinkButton ID="lnkunlike" runat="server" CommandName="unlike" CommandArgument='<%# Eval("ScrapId")%>'>unlike</asp:LinkButton>
</asp:GridView>
并在代码背后:
protected void CommandLinkClicked(object sender, DataGridCommandEventArgs e)
{
var scrapId = Int32.Parse(e.CommandArgument.ToString());
switch (e.CommandName)
{
case "like" :
//do stuff
break;
case "unlike" :
//do stuff
break;
}
}
CommandArguemnt
应该是应该针对like /不同状态更新的项的id。
我清理了你的SQL,现在它还返回每个废料的评论数:
string getUserScraps = "SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId, ISNULL(COUNT(l.scrapid), 0) as CommentCount FROM Scrap as s INNER JOIN [User] as u on u.Id = s.FromId LEFT JOIN tbl_like as l on l.scrapid = s.Id and l.likestatus = 1 WHERE s.ToId = '" + Request.QueryString["Id"].ToString() + "' GROUP BY u.Id,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID";
在这里你有更多可读版本(它与上面相同!)
SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId, ISNULL(COUNT(l.scrapid), 0) as CommentCount
FROM Scrap as s
INNER JOIN [User] as u on u.Id = s.FromId
LEFT JOIN tbl_like as l on l.scrapid = s.Id and l.likestatus = 1
WHERE s.ToId = 23 --here goes id!
GROUP BY u.Id,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID
它计算每个废料的评论,如果没有评论则返回0。将您的GridView
列绑定到CommentCount,它应该可以正常工作