S
Stephen Miller
I am using the OnItemDataBound event of Repeater control to nest a
DataGrid within the Repeater. When I attempt to bind to the DataGrid
using the DataSource method I get the error message "Object reference
not set to an instance of an object". This error message commonly
occurs when a server control is incorrecly declared, so naturally I
have double checked this.
To test this, I moved the aspx code for the DataGrid
('myNestedDataGrid') outside the Repeater ('myRepeater') and it bound
without problem.
I know that you can nest a DataGrid within a Repeater, but I must be
missing something:
<code_behind>
'-- Declare server controls
Protected WithEvents myRepeater As System.Web.UI.WebControls.Repeater
Protected WithEvents myNestedDataGrid As
System.Web.UI.WebControls.DataGrid
'-- Define page wide variable to hold nested data source
Private mDataView As DataView
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myDataSet As DataSet = getRepeater
mDataView = myDataSet.Tables("myTable").DefaultView
myRepeater.DataSource() = mDataView
myRepeater.DataBind()
End Sub
Public Sub myRepeater_ItemBound(ByVal sender As System.Object, ByVal
e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
myRepeater.ItemDataBound
Dim lngIdx As Integer = e.Item.ItemIndex
If lngIdx > -1 Then
myNestedDataGrid.DataSource =
getNestedDataGrid(mDataView(lngIdx).Row("ItemID")).Tables("myNestedTable").DefaultView
myNestedDataGrid.DataBind()
End If
End Sub
Private Function getRepeater() As DataSet
'-- Return DataSet
End Function
Private Function getNestedDataGrid(ByVal ItemID As Long) As DataSet
'-- Return DataSet, using ItemID as primary key
End Function
</code_behind>
<aspx_that_fails>
<asp:Repeater id="myRepeater" runat="Server"
OnItemDataBound="myRepeater_ItemBound">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "ItemName") %></b>
<asp:datagrid id="myNestedDataGrid">
<columns>
<asp:boundcolumn datafield="Field1"></asp:boundcolumn>
<asp:boundcolumn datafield="Field2"></asp:boundcolumn>
<asp:boundcolumn datafield="Field3"></asp:boundcolumn>
</columns>
</asp:datagrid>
</ItemTemplate>
</asp:Repeater>
</aspx_that_fails>
<aspx_that_works>
<asp:Repeater id="myRepeater" runat="Server"
OnItemDataBound="myRepeater_ItemBound">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "ItemName") %></b>
</ItemTemplate>
</asp:Repeater>
<asp:datagrid id="myNestedDataGrid">
<columns>
<asp:boundcolumn datafield="Field1"></asp:boundcolumn>
<asp:boundcolumn datafield="Field2"></asp:boundcolumn>
<asp:boundcolumn datafield="Field3"></asp:boundcolumn>
</columns>
</asp:datagrid>
</aspx_that_works>
DataGrid within the Repeater. When I attempt to bind to the DataGrid
using the DataSource method I get the error message "Object reference
not set to an instance of an object". This error message commonly
occurs when a server control is incorrecly declared, so naturally I
have double checked this.
To test this, I moved the aspx code for the DataGrid
('myNestedDataGrid') outside the Repeater ('myRepeater') and it bound
without problem.
I know that you can nest a DataGrid within a Repeater, but I must be
missing something:
<code_behind>
'-- Declare server controls
Protected WithEvents myRepeater As System.Web.UI.WebControls.Repeater
Protected WithEvents myNestedDataGrid As
System.Web.UI.WebControls.DataGrid
'-- Define page wide variable to hold nested data source
Private mDataView As DataView
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myDataSet As DataSet = getRepeater
mDataView = myDataSet.Tables("myTable").DefaultView
myRepeater.DataSource() = mDataView
myRepeater.DataBind()
End Sub
Public Sub myRepeater_ItemBound(ByVal sender As System.Object, ByVal
e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
myRepeater.ItemDataBound
Dim lngIdx As Integer = e.Item.ItemIndex
If lngIdx > -1 Then
myNestedDataGrid.DataSource =
getNestedDataGrid(mDataView(lngIdx).Row("ItemID")).Tables("myNestedTable").DefaultView
myNestedDataGrid.DataBind()
End If
End Sub
Private Function getRepeater() As DataSet
'-- Return DataSet
End Function
Private Function getNestedDataGrid(ByVal ItemID As Long) As DataSet
'-- Return DataSet, using ItemID as primary key
End Function
</code_behind>
<aspx_that_fails>
<asp:Repeater id="myRepeater" runat="Server"
OnItemDataBound="myRepeater_ItemBound">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "ItemName") %></b>
<asp:datagrid id="myNestedDataGrid">
<columns>
<asp:boundcolumn datafield="Field1"></asp:boundcolumn>
<asp:boundcolumn datafield="Field2"></asp:boundcolumn>
<asp:boundcolumn datafield="Field3"></asp:boundcolumn>
</columns>
</asp:datagrid>
</ItemTemplate>
</asp:Repeater>
</aspx_that_fails>
<aspx_that_works>
<asp:Repeater id="myRepeater" runat="Server"
OnItemDataBound="myRepeater_ItemBound">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "ItemName") %></b>
</ItemTemplate>
</asp:Repeater>
<asp:datagrid id="myNestedDataGrid">
<columns>
<asp:boundcolumn datafield="Field1"></asp:boundcolumn>
<asp:boundcolumn datafield="Field2"></asp:boundcolumn>
<asp:boundcolumn datafield="Field3"></asp:boundcolumn>
</columns>
</asp:datagrid>
</aspx_that_works>