RadListView DataBind problems

3 posts, 0 answers
  1. Jure
    Jure avatar
    18 posts
    Member since:
    Jul 2010

    Posted 11 Apr 2011 Link to this post

    Hey!

    The list displays message text and has 2 buttons "+" and "-", so users vote if they like or dislike the message.

    I tried 2 solutions but have problems with both.

    1st solution is without needdatasource:

    Here is the ListView:

    <telerik:RadListView ID="rlvTweets" runat="server" Width="100%" DataKeyNames="Tweet_ID"
        AllowPaging="True" PageSize="5" ItemPlaceholderID="ObjectHolder"
        OnItemCommand="rlvTweets_ItemCommand"
        OnItemDataBound="rlvTweets_ItemDataBound" >
      <LayoutTemplate>
        <asp:Panel ID="ObjectHolder" runat="server" />
        <telerik:RadDataPager ID="rdpTweets" runat="server" PagedControlID="rlvTweets" PageSize="5" Visible='<%# rlvTweets.PageCount != 1%>' >
          <Fields>
            <telerik:RadDataPagerButtonField FieldType="Numeric" />
          </Fields>
        </telerik:RadDataPager>
      </LayoutTemplate>
      <ItemSeparatorTemplate>
        <div class="separator"></div>
      </ItemSeparatorTemplate>
      <ItemTemplate>
     
        <%--Image Avatar--%>
        <asp:HyperLink ID="hlAvatar" runat="server" ImageUrl='<%#Eval("trTwitterUser.Avatar")%>' />
        <%--Link Username--%>
        <asp:HyperLink ID="hlUsername" runat="server" Text='<%#Eval("trTwitterUser.Username")%>' />
        <%--Buttons follow/stop following--%>
        <asp:Button ID="btnFollow" runat="server" Text="<%$ Resources:Localization, Follow  %>" ToolTip="<%$ Resources:Localization, Follow  %>" CommandName="Follow" />
        <asp:Button ID="btnStopFollowing" runat="server" Text="<%$ Resources:Localization, StopFollowing  %>" ToolTip="<%$ Resources:Localization, StopFollowing  %>" CommandName="StopFollowing" />
        <br />
        <%--Text message--%>
        <%#Eval("Text")%>
        <br />
        <%--Rank and plus/minus buttons--%>
        <asp:Label ID="lblRank" runat="server" Text='<%#Eval("Rank")%>' />
        <asp:Button ID="btnPlus" runat="server" Text="+" CommandName="PlusVote" />
        <asp:Button ID="btnMinus" runat="server" Text="-" CommandName="MinusVote" />
        <br />
        <%--TweetRank--%>
        <asp:Label ID="lblTweetRank" runat="server" Text='<%#Eval("TweetRank")%>' />
        <br />
        <%--Klout icon and KloutScore--%>
        <asp:Image ID="imgKlout" imageUrl="~/Resources/Images/KloutScore.png" runat="server" />
        <%#Eval("trTwitterUser.KloutScore")%>
        <br />
        <%--Date--%>
        <%#Eval("Date")%>
     
      </ItemTemplate>
      <EmptyDataTemplate>
        <asp:Label ID="lblNoTweets" runat="server" Text="<%$ Resources:Localization, NoTweetsFound  %>" />
      </EmptyDataTemplate>
    </telerik:RadListView>


    I databind the list in page_load;

    rlvTweets.DataSource = _db.trTweets.Where(tw => tw.Date > dt).OrderByDescending(t => t.Date);
    rlvTweets.DataBind();

    Everything works fine on the first page, the itemcommand fires, tweet is set properly... However if I use the pager the ItemCommand doesn't even fire.

    protected void rlvTweets_ItemCommand(object sender, RadListViewCommandEventArgs e)
    {
          RadListViewDataItem item = (RadListViewDataItem)e.ListViewItem;
          trTweet tweet = (trTweet)item.DataItem;
            if (e.CommandName == "PlusVote")
            {
                 ....
            }
    }


    The second solution is with using the needdatasource:

    ListIs the same except for needdatasource event, itemcommand is the same, the itemcommand fires everywhere now except that the item.DataItem is always null...

    I removed the databinding from the page load and added it to the needdata source. I feel like I have to call the list's DataBind function somewhere, but I don't know here...

    this is what i do in needdatasource:

    protected void rlvTweets_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
            {
                DateTime dt = DateTime.Now.AddDays(-30);
                rlvTweets.DataSource = _db.trTweets.Where(tw => tw.Date > dt).OrderByDescending(t => t.Date);
            }


    Thanks for you help!

    Sincerely, Jure


  2. Jure
    Jure avatar
    18 posts
    Member since:
    Jul 2010

    Posted 12 Apr 2011 Link to this post

    I attached a sample project so you can see what I mean, if I have the rlvTweets.Rebind() in page_load the item command doesn't fire, if I remove it the ItemCommand event will fire but the data won't be bound so tweet will be null and everything will fail :/

    I can work around this by adding a hiden control on the list item that stores the ID of the tweet which I can get from database, but that is not what I am looking for, I wan't a nice solution without workarounds...

    You can find the sample project here: Solution
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Apr 2011 Link to this post

    Hello Jure,

    I have found some forums discussing the similar scenario (i.e., accessing DataItem in the ItemCommand event). Please have a look at this.
    Is there no DataItem object?
    RadListViewDataItem Retrieval

    Thanks,
    Princy.
Back to Top