Which Controls to Build a Parent/Child Web Form

R

Richard Kirk

Hi there,

I'm quite new to asp.net, but know exactly how to do this using classic ASP,
but would like some pointers for aspnet2.

I've got data being returned from an SP which looks like this:

HeaderItem ChildItem
A A1
A A2
A A3
B B1
etc

I want to display the data like this

A - This is the header record
A1
A2
A3
B - Header 2
B1
etc.

In classic ASP I'd write out my header row html when HeaderItem changes
value and then write the Child Rows.

As I've only got one DataSource to work with it doesn't look like I can use
embedded Repeaters (I can't get A and then get all the children).

Any suggestions as to which controls I should be using, and some pointers on
what I should be doing in my RowDataBound, etc.

Any help (or example links) most welcome.

Thanks.
 
G

Guest

Hi there,

I'm quite new to asp.net, but know exactly how to do this using classic ASP,
but would like some pointers for aspnet2.

I've got data being returned from an SP which looks like this:

HeaderItem    ChildItem
A                    A1
A                    A2
A                    A3
B                    B1
etc

I want to display the data like this

A - This is the header record
    A1
    A2
    A3
B - Header 2
    B1
etc.

In classic ASP I'd write out my header row html when HeaderItem changes
value and then write the Child Rows.

As I've only got one DataSource to work with it doesn't look like I can use
embedded Repeaters (I can't get A and then get all the children).

Any suggestions as to which controls I should be using, and some pointers on
what I should be doing in my RowDataBound, etc.

Any help (or example links) most welcome.

Thanks.

You can use Repeater and a nested List/GridView Control. The Repeater
will be used to show A..B and the GridView would list A1..A2..A3 etc.
In this case you would also need your stored procedure to return 2
datasets: 1st will be used to return data for the Repeater (A..B) and
2nd to return data for the GridView.

Just a simple example:

string sql = "SELECT * FROM Headers;
SELECT * FROM Items"

DataSet ds = ...

// Attach the relationship to the dataSet
ds.Relations.Add(new DataRelation("ItemsRelation",
ds.Tables[0].Columns["HeaderId"],
ds.Tables[1].Columns["HeaderId"]));

Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();

This would bind your Repeater Control to 1st table with A..B

Then add OnItemDataBound binding to your Repeater Control markup

<asp:Repeater ID="Repeater1" runat="server"
OnItemDataBound="Repeater1_ItemDataBound">

and in the codebehind

protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
GridView GridView1 = e.Item.FindControl("GridView1") as GridView;
GridView1.DataSource = drv.CreateChildView("ItemsRelation");
GridView1.DataBind();
}
}

You can find many examples in the internet, just search for "nested
gridview" or something like this.

Hope this helps
 
R

Richard Kirk

Thanks for the reply.

Yes I'd looked at nested repeaters, but it didn't really solve my problem.

It's probably explained better by this article I found
http://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-datatable-nested-repeaters.aspx

The temporary table returned from my stored procedure is a very heavy duty
duplicate locator and I would like to use it as it is without adding extra
processing time.

It seems like a quite common and simple thing to want to do, and I've been
doing this in various languages for the last 20 years, i.e.

1. Read record.

2. Has the value in the first field changed? If so write a header record.

3. Write detail line.

Does this really require restructuring the SPs and SQL in ASP.NET? I feel
like a novice and that I'm missing something really obvious.

Thanks again.
 
G

Guest

Thanks for the reply.

Yes I'd looked at nested repeaters, but it didn't really solve my problem..

It's probably explained better by this article I foundhttp://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-data...

The temporary table returned from my stored procedure is a very heavy duty
duplicate locator and I would like to use it as it is without adding extra
processing time.

It seems like a quite common and simple thing to want to do, and I've been
doing this in various languages for the last 20 years, i.e.

1. Read record.

2. Has the value in the first field changed?  If so write a header record.

3. Write detail line.

Does this really require restructuring the SPs and SQL in ASP.NET?  I feel
like a novice and that I'm missing something really obvious.

Thanks again.

Hi Richard,

sure, you can do it as in classic ASP.

DataTable tbl = ...
string x = "";
for (int i = 0; i < tbl.Rows.Count; i++)
{
DataRow myRow = tbl.Rows;
string MyValue = myRow["datafieldname"];
if (MyValue == x) {
....
} else {
Response.Write(MyValue);
x = MyValue;
}
}

but in this case you can't use Repeater. The only way I see is to
enumerate all rows (like above) and create new dataset with
HeaderItems. Then this new dataset could be used to bind 1st repeater
and the original dataset will be used for a nested control.
 
G

Guest

Thanks for the reply.

Yes I'd looked at nested repeaters, but it didn't really solve my problem..

It's probably explained better by this article I foundhttp://weblogs.sqlteam.com/jeffs/archive/2007/11/02/parent-child-data...

Yes, what he does in his example is exactly what I said, he created an
additional table, and a relation to the first (original) table.
 
R

Richard Kirk

Thanks again Alexey,

It's my lack of understanding as I would have thought that there would be a
mechanism to do this without response.writing.

I'll look into either creating a new DataSet with just the header items, or
look at rewriting the SP to return two results. Hopefully I can do this
without running through the Levenstien compare twice, so it's very slow.

Cheers.
 
G

Guest

Thanks again Alexey,

It's my lack of understanding as I would have thought that there would be a
mechanism to do this without response.writing.

I'll look into either creating a new DataSet with just the header items, or
look at rewriting the SP to return two results.  Hopefully I can do this
without running through the Levenstien compare twice, so it's very slow.

Cheers.

Also look at the following article at MSDN
http://msdn.microsoft.com/en-us/magazine/cc500643.aspx?pr=blog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top