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