2013-04-04 80 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