Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have developed a simple wall (nested comments) where people can comment on scraps(posts). It includes a thumbs up/down feature, the problem is that when i click on thumbs up the entire page reloads. I want just the label which is displaying number of votes(likes) to be updated and nothing else. How can i do this? This is my attempt which is not working..
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")) %>' />
 
Code Behind:
 protected void GridViewRowCommand(Object sender, GridViewCommandEventArgs e)
    {
 
        var scrapId = Int32.Parse(e.CommandArgument.ToString());
        GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
        int index = gvr.RowIndex;
 
        switch (e.CommandName)
        {
            case "like":
                //GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                //int index = gvr.RowIndex;

                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);
                   int likes= abc(scrapId);
                    //GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
                    Label lnklike = (Label)GridViewUserScraps.Rows[index].FindControl("Label1");
                        lnklike.Text= likes.ToString();
                }
                else if (a != 0)
                {
 
                    Response.Write("already liked");
                }
                else if (b != 0)
                {
                    Response.Write("you can not like something you already downvoted!");
                }
 
                break;
Method to get number of thumbs up /likes:
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;
    }
 
Method to Load Wall:
<pre lang="xml">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();
 

       }
 
   }
Posted 4-Apr-13 1:59am
Edited 4-Apr-13 3:48am
Nelek63.2K
v5

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try UpdateMode = "conditional" property in you UpdatePanel control.
  Permalink  
Comments
arbaaz jalil at 4-Apr-13 7:24am
   
still not working, i have updated my the code
Zafar Sultan at 4-Apr-13 8:17am
   
First, you are passing "like" as commandname and in switch case statement you have only on case "childlike". Second, where is "onclick" event in your markup for ImageButton? Third, when you are done with registering onclick event for your ImageButton, no need to specify event name in AsyncPostBack trigger. Just the controlid will suffice.
arbaaz jalil at 4-Apr-13 8:34am
   
i have updated the code
arbaaz jalil at 4-Apr-13 8:36am
   
i had posted the wrong code earlier

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100