我只单击 GridView 中的链接按钮一次,但每次都会插入两次记录。我尝试应用断点,但仍然无法弄清楚。我做错了什么?
资源:
<asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False"
GridLines="None" Width="100%" ShowHeader="False" runat="server" AlternatingRowStyle-BackColor="#A5A5A5"
CellPadding="4" ForeColor="#333333" DataKeyNames="ScrapId"
OnRowCommand="GridViewRowCommand">
<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"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="LikeCmd">Like</asp:LinkButton>
<asp:LinkButton ID="lnkunlike" runat="server">unlike</asp:LinkButton>--%>
<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:Label ID="lbllike" runat="server" Text="likes:"></asp:Label>
<%-- <asp:Label ID="lbllikecount" runat="server" Text="0"></asp:Label>--%>
<asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />
<%--<asp:Label ID="Label1" runat="server" Text='<%# WebPageName.StaticMethodName((int)Eval("ScrapId")) %>' />--%>
<asp:Label ID="lblthumbsdown" runat="server" Text="ThumbsDown:"></asp:Label>
<asp:Label ID="Label2" runat="server"
Text='<%# Controls_GetUserScraps.xyz((int)Eval("ScrapId")) %>'></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 GridViewRowCommand(Object sender, GridViewCommandEventArgs e)
{
var scrapId = Int32.Parse(e.CommandArgument.ToString());
switch (e.CommandName)
{
case "like":
string chklike = "select likestatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int a = dbo.GetLikesMethod(chklike);
string chkthumbsdown = "select thumbsdownstatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int b = dbo.GetLikesMethod(chkthumbsdown);
if (a == 0 && b==0)
{
string sendlike = "insert into tbl_like (ScrapId,FromId,LikeStatus) values('" + scrapId + "','" + Session["UserId"] + "',1)";
dbo.insert(sendlike);
GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
}
else if(a!=0)
{
Response.Write("already liked");
}
else if (b != 0)
{
Response.Write("you can not like something you already downvoted!");
}
break;
case "unlike":
string chkthumbsdown1 = "select thumbsdownstatus from tbl_like where fromid='"+Session["UserId"]+"' and scrapid='"+scrapId+"'";
int b1 = dbo.GetLikesMethod(chkthumbsdown1);
if (b1 == 0)
{
string sendthumbsdown = "insert into tbl_like (ScrapId,FromId,thumbsdownstatus) values('" + scrapId + "','" + Session["UserId"] + "',1)";
dbo.insert(sendthumbsdown);
GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
}
else
{
Response.Write("already THumbsDowned!");
}
break;
}
}
public DataTable insert(string q)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(q,con);
try
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch { };
return dt;
}