2013-04-04 54 views
0

我开发了一个墙(嵌套评论),人们可以评论废料(职位)。它包括一个大拇指向上/向下的功能,问题是,当我点击拇指向上整个页面重新加载。我只想要显示票数(喜欢)的标签被更新,没有别的。我怎样才能做到这一点?这是我尝试它不工作.. ASPX:在GridView中更新标签?

<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(); 
    } 
} 
+0

你可以epxand你的意思是“不工作”?无论何时你说什么都不起作用,你应该总是清楚地定义预期行为和当前(不正确)的行为。 – jadarnel27 2013-04-04 13:48:59

+0

@ jadarnel27通过不工作我的意思是整个页面重新加载时,我使用上面的代码(或整个GridView)我只想刷新一个标签,显示该行中的计数,其中用户单击竖起大拇指或像imagebutton – Arbaaz 2013-04-04 15:33:01

回答

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调用,比如this

回报你R IN JSON新的数量和更新标签

Ajax调用

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 web服务看起来HERE

[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);" /> 

图像标识=您想更新等等状态

如果您还有不明白的让我知道了图像的ID。

+0

我从来没有用Ajax或Json来做这样的事情,请详细解释一下我应该如何做这件事?我检查了你的链接,我无法理解它,我如何在这种情况下实现该代码? – Arbaaz 2013-04-05 06:09:21

+0

看看上面的更新代码。 – derral 2013-04-05 16:07:17