This is a migrated thread and some comments may be shown as answers.

RadListView DataBind problems

2 Answers 171 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Jure
Top achievements
Rank 1
Jure asked on 11 Apr 2011, 02:03 PM
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 Answers, 1 is accepted

Sort by
0
Jure
Top achievements
Rank 1
answered on 12 Apr 2011, 09:56 AM
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
0
Princy
Top achievements
Rank 2
answered on 12 Apr 2011, 10:14 AM
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.
Tags
ListView
Asked by
Jure
Top achievements
Rank 1
Answers by
Jure
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or