0

我只单击 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>
    &nbsp;</td>
    </tr>
    </table>
    <div align="justify">
    <%#DataBinder.Eval(Container.DataItem, "Message")%>
    <br />
    <br />
    </div>
    <span class="SmallBlackText">Posted On: &nbsp;</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>
    &nbsp;&nbsp;&nbsp;
    <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>



    &nbsp;&nbsp;
    <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")) %>' />--%>

    &nbsp;&nbsp;
    <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;
}
4

2 回答 2

3

为了在数据库中插入行,您为什么要使用 dataadapter。

当您使用 da.Fill(dt) 时,它已经在执行 sql 字符串。你再次以 cmd.ExecuteNonQuery() 的形式执行它

试试这样的代码

  public bool insert(string q)
  {   
     bool result=false;
     SqlCommand cmd = new SqlCommand(q,con);
       try
        {       
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();
          result=true
        }
       catch { };
       return result; 
      }
于 2013-02-16T11:17:26.070 回答
0

不要使用 GridViewRowCommand 事件,而是尝试在按钮上使用 OnClick 事件处理程序。这可能会解决问题。

于 2013-02-16T07:48:29.673 回答