472,347 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

Problem binding DataGrid nested inside Repeater

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>
Nov 17 '05 #1
2 3171
are you sure that you can an control inside another and yet still be able to
reference it??
i think i have tried this before without success either.

"Stephen Miller" <js******@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
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").D
efaultView 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>

Nov 17 '05 #2
It can be done (sort of)
In the HTML of your datagrid put a reference to a data source...
<asp:DataGrid id=DataGrid1 runat="server" AutogenerateColumns="False"
DataSource='<%# getGridDataSource( ctype(DataBinder.Eval(Container.DataItem,
"CustomerID"),string) ) %>'

Whats going on here is... When a row of your repeater binds, a data grid is
created & the data source calls a function called getGridDataSource. THE
IMPORTANT BIT is the DataBinder.Eval(Container.DataItem, "CustomerID") that
is passed to getGridDataSource. The Container.DataItem is the Current
Reapeater Row. so... You key together a piece of data from the current
Repeater row with the datasource of the data grid - so that the content of
the data grid is relevant to the current repeater row!

Now, when the datagrids bind, you can have a handler for
OnItemDataBound='<%# whatever....%> & get a reference to the grid.

"Stephen Miller" <js******@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
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").D
efaultView 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>

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Maziar Aflatoun | last post by:
Hi everyone, I have a datagrid that I create inside a Repeater at runtime. I have defined a checkbox beside each row so that users can select...
7
by: Scott Schluer | last post by:
Hi All, I have a functioning datagrid on "Page 1" that displays order information for a single order (this is for an e-commerce site). It's...
2
by: James Moore | last post by:
I've got a datagrid. One of the elements in the grid is a comma-seperated string. I'd like to split the string and create N linkbuttons, one for...
2
by: Colin Nicholls | last post by:
Platform: ASP.NET 1.1 I have a repeater nested inside another repeater. My outer repeater is looping fine. I am manually binding the inner...
3
by: Maverick | last post by:
Assume i have 2 tables stored in a single dataSet (ds) and the relationship is well added ds.Tables("product") ds.Tables("descriptors") what I...
1
by: Maziar Aflatoun | last post by:
Hi, I have tried setting up a nested repeater control and it works fine. But when I do 3 level nested repeater it fails. Does anyone know a site...
3
by: Maziar Aflatoun | last post by:
Hi everyone, I have a datagrid that I create inside a Repeater at runtime. I have defined a checkbox beside each row so that users can select...
12
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm doing a web app in VB/Dot Net 2.0. I'm probably a bit rusty and I have no experience using the repeater control. I have a user control I've...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.