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

RadListViewDataItem Retrieval

7 Answers 434 Views
ListView
This is a migrated thread and some comments may be shown as answers.
robertw102
Top achievements
Rank 1
robertw102 asked on 19 Nov 2009, 07:17 PM
I'm using the RadListView in my project which is setup like so:
<telerik:RadListView ID="lvCalendars" runat="server" AllowPaging="True" DataKeyNames="Listing_ID" 
            DataSourceID="dsListing" OnItemDataBound="lvCalendars_ItemDataBound"
            <LayoutTemplate> 
                <telerik:RadDataPager ID="dpTopPager" runat="server" PagedControlID="lvCalendars" 
                    PageSize="7" Skin="Office2007"
                    <Fields> 
                        <telerik:RadDataPagerButtonField FieldType="Numeric" PageButtonCount="10" /> 
                    </Fields> 
                </telerik:RadDataPager> 
                <table cellpadding="0" cellspacing="0" width="100%" class="bgStandardMedium"
                    <tr id="itemPlaceholder" runat="server"
                    </tr> 
                </table> 
                <telerik:RadDataPager ID="dpBottomPager" runat="server" PagedControlID="lvCalendars" 
                    PageSize="7" Skin="Office2007"
                    <Fields> 
                        <telerik:RadDataPagerButtonField FieldType="Numeric" PageButtonCount="10" /> 
                    </Fields> 
                </telerik:RadDataPager> 
            </LayoutTemplate> 
            <ItemTemplate> 
                <tr> 
                    <td class="bgStandardDark propertySection"
                        <asp:Image ID="imgProperty" runat="server" Width="130px" Height="90px" AlternateText='<%#Eval("Name") %>' 
                            ToolTip='<%#Eval("Name") %>' /> 
                        <span class="propertyName"
                            <%#DataBinder.Eval(Container, "DataItem.Name").ToString() %> 
                        </span> 
                        <asp:HyperLink ID="lnkBookProperty" runat="server" CssClass="buttonStandard" Text="<span>Book Property</span>" 
                            Width="170px"></asp:HyperLink> 
                        <br /> 
                        <br /> 
                        <asp:LinkButton ID="lnkPrintCalendar" runat="server" CssClass="buttonStandard" Width="170px" 
                            Text="<span>Print Reservation List</span>"></asp:LinkButton> 
                    </td> 
                    <td class="calendarSection"
                        <telerik:RadAjaxPanel ID="apBookingCalendar" runat="server" LoadingPanelID="LoadingPanel1"
                            <telerik:RadCalendar ID="calBooking" runat="server" MultiViewColumns="4" MultiViewRows="1" 
                                EnableMultiSelect="false" ShowRowHeaders="False" ShowOtherMonthsDays="False" 
                                AutoPostBack="True" CssClass="bookingCalendar" OnDefaultViewChanged="calBooking_DefaultViewChanged"
                                <CalendarTableStyle Width="175px" Height="196px" /> 
                            </telerik:RadCalendar> 
                            <div class="button-row" style="text-align: center;"
                                <asp:LinkButton ID="lnkbtnViewFullYear" runat="server" CssClass="buttonStandard" 
                                    Text="<span>View Full Year</span>" Width="150px" /> 
                            </div> 
                        </telerik:RadAjaxPanel> 
                        </li> 
                    </td> 
            </ItemTemplate> 
        </telerik:RadListView> 

Now in my calendar's OnDefaultViewChanged event I attempt to retrieve the RadListViewDataItem it's contained in like so:

protected void calBooking_DefaultViewChanged(Object sender, Telerik.Web.UI.Calendar.DefaultViewChangedEventArgs e) 
    { 
        try 
        { 
            // get calendar 
            RadCalendar calBooking = (RadCalendar)sender; 
            // get RadListViewDataItem 
            RadListViewDataItem dtItem = (RadListViewDataItem)calBooking.Parent.Parent; 
 
            // hold Sub Listing 
            SubListing subListing = new SubListing(); 
 
            // hold Listing 
            Listing listing; 
 
            // check if data item is a listing 
            if (dtItem.DataItem is Listing) 
            { 
                // get listing 
                listing = (Listing)dtItem.DataItem; 
            } 
            else 
            { 
                // get sub listing 
                subListing = (SubListing)dtItem.DataItem; 
                // get listing 
                listing = subListing.Listing; 
            } 
 
            //...calendar setup code follows
 
        } 
        catch (Exception ex) 
        { 
        } 
    } 

Now apparently DataItem does not exist, I get a null value. I was able to retrieve the DataItem object in the ItemDataBound event of the RadListView control but not in the calendar event. 

Is there a reason for this or is it some bug in the RadListView control?

I am using the latest version of the controls 3.1105 (.NET 3.5).

7 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 20 Nov 2009, 08:56 AM
Hi,

As you may know when using DataBoundControls, Item's DataItem property is only populated when control is created from the accosted datasource and not when the control is recreated from its ViewState. Therefore if you want to use some part of the object associated with an Item of ListView control, on an event which will not cause the control to be repopulated, you should use some other approach get the object. For example you can use DataKeyNames. This way you may retrieve the full object by its key or use the value directly if it is sufficient for the scenario in question.

Kind regards,
Rosen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
cute gal
Top achievements
Rank 1
answered on 26 Mar 2010, 10:54 AM
hi.. i want to print contents populated within radlistview
i wonder to get data and activate printer settings on the clink event of linkbutton..

 <asp:Panel ID="ListViewPanel1" runat="server"
                    <telerik:RadListView ID="RadListView1" runat="server" DataKeyNames="PrivacyId" DataSourceID="source1" 
                        ItemPlaceholderID="ListViewContainer" RegisterWithScriptManager="true" allowsorting="true" 
                        AllowPaging="True" Font-Names="Arial"
                        <LayoutTemplate> 
                            <asp:PlaceHolder ID="ListViewContainer" runat="server" /> 
                        </LayoutTemplate> 
                        <ItemTemplate> 
                            <table> 
                                <tr> 
                                    <td> 
                                        &nbsp; 
                                    </td> 
                                </tr> 
                                <tr> 
                                    <td> 
                                        <%#Eval("Heading")%> 
                                    </td> 
                                </tr> 
                                <tr> 
                                    <td> 
                                        &nbsp; 
                                    </td> 
                                </tr> 
                                <tr> 
                                    <td> 
                                        <%#Eval("Description")%> 
                                    </td> 
                                </tr> 
                            </table> 
                        </ItemTemplate> 
                    </telerik:RadListView> 
                </asp:Panel> 
 
<asp:SqlDataSource ID="source1" runat="server" ConnectionString="<%$ ConnectionStrings:helpConnectionString %>" 
                    SelectCommand="SELECT * FROM [Privacy]"></asp:SqlDataSource> 
 
<asp:LinkButton ID="lnkPrint" runat="server" >Print this Page</asp:LinkButton> 

0
Rosen
Telerik team
answered on 29 Mar 2010, 07:22 AM
Hi,

There is no such functionality built into the RadListView. However you may look at this code library which demonstrates how to achieve similar thing with RadGrid and adapt the code to fit your case.

All the best,
Rosen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Kiran
Top achievements
Rank 1
answered on 04 Nov 2019, 02:24 PM

Hi Rosen,

Can you please post an example on how to retrieve datakeyvalue on the raddatapager change? I have radlistview with raddatapager. One item per page. On clicking on the next button of the raddatapager I want to grab the value (datakeyvalue).

Thank you.

0
Rumen
Telerik team
answered on 07 Nov 2019, 12:56 PM

Hi Kiran,

You can attach to the OnCommand server event of the RadDataPager to check for the PageCommandName and extract the value of the ListBoxDataItem like this:

var dataKeyValue = (RadListView1.Items[0] as RadListViewDataItem).GetDataKeyValue("CustomerID").ToString();

Here is an example based on the RadListView overview demo:

ASPX

<telerik:RadDataPager RenderMode="Lightweight" ID="RadDataPager1" runat="server" PagedControlID="RadListView1" OnCommand="RadDataPager1_Command"
    PageSize="2" CssClass="pagerStyle">
    <Fields>
        <telerik:RadDataPagerButtonField FieldType="FirstPrev"></telerik:RadDataPagerButtonField>
        <telerik:RadDataPagerButtonField FieldType="Numeric" PageButtonCount="4"></telerik:RadDataPagerButtonField>
        <telerik:RadDataPagerButtonField FieldType="NextLast"></telerik:RadDataPagerButtonField>
        <telerik:RadDataPagerPageSizeField PageSizeComboWidth="60" PageSizeText="Page size: "></telerik:RadDataPagerPageSizeField>
        <telerik:RadDataPagerGoToPageField CurrentPageText="Page: " TotalPageText="of" SubmitButtonText="Go"></telerik:RadDataPagerGoToPageField>
    </Fields>
</telerik:RadDataPager>

Codebehind:

        protected void RadDataPager1_Command(object sender, RadDataPagerCommandEventArgs e)
        {
            if (e.CommandName == RadDataPager.PageCommandName) {
                var dataKeyValue = (RadListView1.Items[0] as RadListViewDataItem).GetDataKeyValue("CustomerID").ToString();
            }
        }

Best Regards,
Rumen
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kiran
Top achievements
Rank 1
answered on 07 Nov 2019, 02:05 PM
Thank you so much Rumen.
0
Rumen
Telerik team
answered on 07 Nov 2019, 04:53 PM

Great! Keep up the good work.

Regards,
Rumen
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
ListView
Asked by
robertw102
Top achievements
Rank 1
Answers by
Rosen
Telerik team
cute gal
Top achievements
Rank 1
Kiran
Top achievements
Rank 1
Rumen
Telerik team
Share this question
or