0

我开发了一堵墙(嵌套评论),人们可以在其中评论剪贴簿留言(帖子)。它包括一个竖起大拇指的功能,问题是当我点击竖起大拇指时,整个页面会重新加载。我只想更新显示投票数(喜欢)的标签,而不是别的。我怎样才能做到这一点?这是我的尝试,但不起作用.. ASPX:

<asp:ImageButton ID="lnklike" runat="server" ImageUrl="~/Images/thumbsup.png" height="20px" Width="20px" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>'/> &nbsp;
<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="lnklike" eventname="click"  />
    </Triggers>
    <ContentTemplate>  <asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />

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);
                //abc(scrapId);
                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;
    }
}

获得点赞数/点赞数的方法:

public static int abc(int scrpid)
{    
     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='" + scrpid + "'";

     dboperation dbo = new dboperation();
     int a = dbo.GetLikesMethod(getlikes);

     return a;
}

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 [tbl_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='"+<%#DataBinder.Eval(Container.DataItem,"ScrapId")%>+"'";
    //  <%#DataBinder.Eval(Container.DataItem,"ScrapId")%>

    dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
    if (dt.Rows.Count > 0)
    {
        GridViewUserScraps.DataSource = dt;
        GridViewUserScraps.DataBind();
    }
}
4

1 回答 1

1

在网格中,我会添加这样的链接:

<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-Width="35px">
    <ItemTemplate>
       <a href="javascript:void(0);" onclick="Link to your javascript method/ajax method">
                                         </a>
    </ItemTemplate>
    <ItemStyle Wrap="False"></ItemStyle>
</asp:TemplateField>

然后像这样使用jquery ajax调用

以 JSON 格式返回新计数并更新标签

阿贾克斯调用

function UpdateLikeStatus(imageID, labelid)
{
      $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                url: 'Services/MiscService.asmx/UpdateLikeStatus',
                data: "{'imageid':'" + imageID + "'}",
                dataType: "json",
                success: function (data) {
                    //This is the label you want to update with the new count.

                    $('#labelid').html(data.d);

                }
            });
}

这将是您的 Web 服务调用,也可以在 WCF 服务中使用。要了解如何实现 AJAX 网络服务,请查看这里

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string UpdateLikeStatus(string imageid)
{
   string returnedData = "";         
   //Make call to stored procedure that does the update
   returnedData = Storedprocedurecall.UpdateLikeStatus(imageid); //Updates the status and returns a count 
     //Now return the new count.
   return returnedData;  

}

在图像的单击事件或您用来更新类似状态的任何内容上。

<img src="" id="genericimage" border="0" onclick="UpdateLikeStatus('<%#Eval("imageid") %>', this);" />

imageid = 您要更新类似状态的图像的 id

如果你还是不明白,请告诉我。

于 2013-04-04T21:21:28.413 回答