Which Controls to Build a Parent/Child Web Form

Discussion in 'ASP .Net' started by Richard Kirk, Feb 4, 2010.

  1. Richard Kirk

    Richard Kirk 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.
    Richard Kirk, Feb 4, 2010
    #1
    1. Advertising

  2. On Feb 4, 6:06 pm, "Richard Kirk" <>
    wrote:
    > 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
    Alexey Smirnov, Feb 4, 2010
    #2
    1. Advertising

  3. Richard Kirk

    Richard Kirk 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 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.
    Richard Kirk, Feb 4, 2010
    #3
  4. On Feb 4, 11:18 pm, "Richard Kirk" <>
    wrote:
    > 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.
    Alexey Smirnov, Feb 4, 2010
    #4
  5. On Feb 4, 11:18 pm, "Richard Kirk" <>
    wrote:
    > 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.
    Alexey Smirnov, Feb 4, 2010
    #5
  6. Richard Kirk

    Richard Kirk 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.
    Richard Kirk, Feb 4, 2010
    #6
  7. On Feb 5, 12:02 am, "Richard Kirk" <>
    wrote:
    > 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
    Alexey Smirnov, Feb 14, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arulraja
    Replies:
    3
    Views:
    1,359
    William F. Robertson, Jr.
    Oct 17, 2003
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,084
    David Schwartz
    Dec 9, 2003
  3. Replies:
    0
    Views:
    193
  4. Noel Dolan
    Replies:
    0
    Views:
    216
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page