UpdatePanel内的标签文本不会刷新

时间:2013-11-24 11:32:02

标签: asp.net asp.net-ajax updatepanel

我在ListView ItemTemlate中有标签,我希望通过点击链接按钮刷新它也在ItemTemlate中。

页码:

<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>

代码背后:

        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


    }

这很好用。谢谢大家。你的建议非常有用。

1 个答案:

答案 0 :(得分:0)

考虑删除itemtemplate中的更新面板,而是将整个listview包装在updatepanel中

现在LinkBut​​ton点击实际上已经冒泡到列表视图控件,因此处理链接按钮单击将不会产生所需的效果。相反,您应该在listview上处理ItemCommand事件

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

代码背后:

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
}

查看文档以获取更多想法:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand(v=vs.110).aspx