0
votes

I have label inside ListView ItemTemlate and i want it refresh by the click on linkbutton that is also inside ItemTemlate.

Page Code:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SongID" DataSourceID="AlbumSongsDataSource" OnItemCommand="ListView1_ItemCommand" >

          <EmptyDataTemplate>
              <span>No data was returned.</span>
          </EmptyDataTemplate>

          <ItemTemplate>

             <div href='<%# Eval("getSongPath") %>' style="width: 400px;" class="item">
              <div>
                  <div class="fr duration">02:06</div>
                  <div class="btn play"></div>
                  <div class="title"><b><asp:Label ID="ArtistLabel" runat="server" Text='<%# Eval("Artist") %>' /></b> -<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></div>
                  </div> 
                <div class="player inactive"></div>

   <asp:UpdatePanel ID="LikesUpdate" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True"  >
<ContentTemplate>
                 <div class="likes" runat="server"> <asp:Label ID="LikesLabel" runat="server"  Text='<%#Eval("getLikes") %>'/> <asp:LinkButton ID="LikeButton" runat="server" Text="Like" CommandName="Like" CommandArgument='<%# Eval("SongID") %>' ></asp:LinkButton></div>
                 </div> 

          </ItemTemplate>

      </asp:ListView>

Code behind:

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        int s = int.Parse(e.CommandArgument.ToString());

        ((LinkButton)e.Item.FindControl("LikeButton")).Text = "OK!";

        string UserId = User.Identity.GetUserId();
        MusicStoreEntities1 m = new MusicStoreEntities1();
        if (m.Likes.Where(x => (x.UserID == UserId) && (x.SongID == s)).Count() == 0)
        {
            Likes L = new Likes();
            L.SongID = s;
            L.UserID = UserId;
            L.PlaylistID = null;
            m.Likes.Add(L);
            m.SaveChanges();
        }
        else
        {
            Likes L = m.Likes.Where(x => (x.UserID == UserId) && (x.SongID == s)).FirstOrDefault();
            m.Likes.Remove(L);
            m.SaveChanges();

        }
        ((Label)e.Item.FindControl("LikesLabel")).Text = m.Likes.Where(x => (x.SongID == s)).Count().ToString();//Manually set likes


    }

This works fine. Thanks for all. Your advices were very useful.

1
Hi. It looks like you have a not-well-formed markup...there is a closing div tag inside the contenttemplate of the upatepanel... - deostroll
yes i fixed it but it doesn't change anything. Anyway, thanks - romandemidov
Please post markup that is formatted next time. - deostroll

1 Answers

0
votes

Consider getting rid of the update panel inside the itemtemplate, and instead wrapping the whole listview in updatepanel

Now the LinkButton click is actually bubbled up to the listview control, so handling the linkbutton click won't produce the desired effect. Instead you should handle the ItemCommand event on the listview

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SongID" 
    DataSourceID="AlbumSongsDataSource" OnItemCommand="ListView1_ItemCommand" >
    ...
    ...
    ...
</asp:ListView>

Code behind:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    //Handle button click here...
    //Use e.CommandName to figure which button triggered
    //use e.CommandArgument to figure which data item
}

Have a look at the docs for more ideas: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand(v=vs.110).aspx