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

Nested RadListView and ReBind

2 Answers 315 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 18 Sep 2017, 04:25 PM

I have created a small sample that demonstrates a problem I am encountering in a much larger project.

The scenario is that I have a RadListView using LayoutTemplate/ItemTemplate and one of the controls in the ItemTemplate is a nested RadListView.  Both lists use NeedDataSource for their data binding and the nested list needs the ID value of the parent list row in order to query for the correct data.  So, in order to make the parent ID available to the child, I have a hiddenfield whose value is ',%# Eval("ID" %> and in the NeedDataSource of the child, I get the parent, do a FindControl on the hidden field and this gives me the ID I need.  If I isolate this in a sample project, it works on initial load, but if I have a need to ReBind the parent list, when the child NeedDataSource executes, it can find the hidden field, but its value is not set.

 

Here is the full sample code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="dpCACJTest.ascx.cs"
    Inherits="FACTS_WebApp.DataPanels.dpCACJTest" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
</telerik:RadAjaxManagerProxy>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<telerik:RadListView ID="listParent" runat="server" OnNeedDataSource="listParent_NeedDataSource"
    ItemPlaceholderID="ParentContainer" DataKeyNames="ID">
    <LayoutTemplate>
        <table width="210px">
            <colgroup>
                <col width="30px" />
                <col width="170px" />
            </colgroup>
            <thead>
                <tr>
                    <td >
                        <asp:Label ID="Label29" runat="server" Text="ID"></asp:Label>
                    </td>
                    <td >
                        <asp:Label ID="Label26" runat="server" Text="Values"></asp:Label>
                    </td>
                </tr>
            </thead>
            <tbody>
                <tr id="ParentContainer" runat="server">
                </tr>
            </tbody>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="Label39" runat="server" Text='<%#Eval("ID") %>' ></asp:Label>
                <asp:HiddenField ID="hfID" runat="server" Value='<%#Eval("ID") %>' />
            </td>
            <td id="cellValue" runat="server">
                <telerik:RadListView ID="listChild" runat="server" OnNeedDataSource="listChild_NeedDataSource"
                    ItemPlaceholderID="ChildContainer">
                    <LayoutTemplate>
                        <table width="100%">
                            <asp:PlaceHolder ID="ChildContainer" runat="server"></asp:PlaceHolder>
                        </table>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label ID="lblValue" runat="server" Text='<%#Eval("Value") %>' ></asp:Label>
                            </td>
                        </tr>
                    </ItemTemplate>
                </telerik:RadListView>
            </td>
        </tr>
    </ItemTemplate>
</telerik:RadListView>
<telerik:RadButton ID="btnRebind" runat="server" Text="Rebind" OnClick="btnRebind_Click">
</telerik:RadButton>

 

        protected void listParent_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            StringBuilder sbSQL = new StringBuilder(1000);
            DataTable dtList = new DataTable();
            sbSQL.AppendFormat(@"SELECT 1 as ID
                                    UNION
                                 SELECT 2 as ID
                                    UNION
                                 SELECT 3 as ID");
            dtList = DbHelper.GetDataTable(sbSQL.ToString(), null);
            (sender as RadListView).DataSource = dtList;
        }
        protected void listChild_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            StringBuilder sbSQL = new StringBuilder(1000);
            DataTable dtList = new DataTable();
            string ID = "0";
            RadListViewDataItem parentItem = (sender as RadListView).NamingContainer as RadListViewDataItem;
            HiddenField hfID = (HiddenField)parentItem.FindControl("hfID");
            if (hfID.Value != "")
                ID = hfID.Value;
            sbSQL.AppendFormat(@"SELECT '{0}' as ID, 'ID' + '{0}' + 'Value1' as Value
                                    UNION
                                 SELECT '{0}' as ID, 'ID' + '{0}' + 'Value2' as Value
                                ", ID);
            dtList = DbHelper.GetDataTable(sbSQL.ToString(), null);
            (sender as RadListView).DataSource = dtList;
        }
        protected void btnRebind_Click(object sender, EventArgs e)
        {
            listParent.Rebind();
        }

 

On initial load, the results display correctly:

ID     Values
1       ID1Value1
         ID1Value2
2       ID2Value1
         ID2Value2
3       ID3Value1
         ID3Value2

 

But, if I click the Rebind button and force listParent.ReBind();, then I get the following results:

ID     Values
1       ID0Value1
         ID0Value2
2       ID0Value1
         ID0Value2
3       ID0Value1
         ID0Value2

All the times the listChild_NeedsDataSource fires, the hiddenfield has no value so the string displayed is ID0 rather than ID1, ID2, and ID3.

 

Any idea on why the difference in behavior between initial load and ReBind()?

 

Thanks,

Jim

2 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 21 Sep 2017, 11:43 AM

Hi Jim,

I suggest you add the ID field to the DataKeyNames of the parent listview and use that collection to get the value in the NeedDataSource of the child. I created a KB article explaining the approach here: http://www.telerik.com/support/kb/aspnet-ajax/listview/details/use-parent-data-from-child-control.

Regards,

Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jim
Top achievements
Rank 1
answered on 21 Sep 2017, 02:00 PM

Marin,

Thanks for the reply.  I copied your code into a standalone project and encountered the same problem.  In you code, it manifested itself in the fact the GetDataKey threw an exception under the ReBind scenario.  So, since Telerik provided code has always worked for me in the past, I made a guess that the version I was using was the problem (we keep wanting to upgrade, but our project is still on a 2014 version).  I updated my standalone project to 2017 and, viola', it worked.  I then tried my original code under 2017 and it works as well.

So, looks like a version upgrade is finally in order.  

Thanks for you help,

Jim

Tags
ListView
Asked by
Jim
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Jim
Top achievements
Rank 1
Share this question
or