How to duplicate a user control on a page?

Discussion in 'ASP .Net Web Controls' started by Ed Klein, Dec 5, 2003.

  1. Ed Klein

    Ed Klein Guest

    I have a custom page-navigation control (similar to the one from the
    DataGrid class) implemented as a UserControl. I'd like to place it at both
    the top and bottom of the container that lists my data items, in the same
    way that the DataGrid's "TopAndBottom" pager style works. I'm not using a
    DataGrid, so I need to do this myself. The obvious way, which seems
    tedious, is like this:

    <MyNamespace:MyPager id="Pager1" runat="server" />
    <MyNamespace:MyDataList id="MyList" runat="server" />
    <MyNamespace:MyPager id="Pager2" runat="server" />

    And in the code:

    Pager1.PageSize = 10;
    Pager2.PageSize = 10;
    Pager1.CurPos = 30;
    Pager2.CurPos = 30;
    Pager1.DataBind();
    Pager2.DataBind();

    and so on. Both instances of "MyPager" look exactly the same and are
    databound with exactly the same data. Seems like it would be easier to just
    have one instance of the pager, but allow it to somehow render itself twice
    on the containing page, so I wouldn't have to have all these duplicate lines
    of code. Is this possible in ASP.NET?

    --
    Ed
    Ed Klein, Dec 5, 2003
    #1
    1. Advertising

  2. Ed,
    You could try doing this in code. Instead of your pager control,
    create placeholder controls in the same place. Then programatticaly create a
    single instance of your pager control. Then use the placeholder's
    Controls.Add method to add the control to the placeholders (don't forget to
    do it for each individual placeholder). Not sure if this will work, but it's
    worth a shot.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "Ed Klein" <> wrote in message
    news:...
    > I have a custom page-navigation control (similar to the one from the
    > DataGrid class) implemented as a UserControl. I'd like to place it at

    both
    > the top and bottom of the container that lists my data items, in the same
    > way that the DataGrid's "TopAndBottom" pager style works. I'm not using a
    > DataGrid, so I need to do this myself. The obvious way, which seems
    > tedious, is like this:
    >
    > <MyNamespace:MyPager id="Pager1" runat="server" />
    > <MyNamespace:MyDataList id="MyList" runat="server" />
    > <MyNamespace:MyPager id="Pager2" runat="server" />
    >
    > And in the code:
    >
    > Pager1.PageSize = 10;
    > Pager2.PageSize = 10;
    > Pager1.CurPos = 30;
    > Pager2.CurPos = 30;
    > Pager1.DataBind();
    > Pager2.DataBind();
    >
    > and so on. Both instances of "MyPager" look exactly the same and are
    > databound with exactly the same data. Seems like it would be easier to

    just
    > have one instance of the pager, but allow it to somehow render itself

    twice
    > on the containing page, so I wouldn't have to have all these duplicate

    lines
    > of code. Is this possible in ASP.NET?
    >
    > --
    > Ed
    >
    >
    >
    >
    Mark Fitzpatrick, Dec 5, 2003
    #2
    1. Advertising

  3. Ed Klein

    Ed Klein Guest

    Thanks for the suggestion. Apparently, though, ASP.NET doesn't allow a
    single control instance to be rendered in more than one place on the page.
    I tried your method but it only rendered the bottom pager; it's as if the
    second invocation of "Controls.Add" had the side-effect of removing the
    control from the first placeholder. No big deal; I'll just go back to two
    pager instances and write a function that sets parameters for each, which
    will at least eliminate some duplication.

    --
    Ed


    "Mark Fitzpatrick" <> wrote in message
    news:...
    > Ed,
    > You could try doing this in code. Instead of your pager control,
    > create placeholder controls in the same place. Then programatticaly create

    a
    > single instance of your pager control. Then use the placeholder's
    > Controls.Add method to add the control to the placeholders (don't forget

    to
    > do it for each individual placeholder). Not sure if this will work, but

    it's
    > worth a shot.
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    > "Ed Klein" <> wrote in message
    > news:...
    > > I have a custom page-navigation control (similar to the one from the
    > > DataGrid class) implemented as a UserControl. I'd like to place it at

    > both
    > > the top and bottom of the container that lists my data items, in the

    same
    > > way that the DataGrid's "TopAndBottom" pager style works. I'm not using

    a
    > > DataGrid, so I need to do this myself. The obvious way, which seems
    > > tedious, is like this:
    > >
    > > <MyNamespace:MyPager id="Pager1" runat="server" />
    > > <MyNamespace:MyDataList id="MyList" runat="server" />
    > > <MyNamespace:MyPager id="Pager2" runat="server" />
    > >
    > > And in the code:
    > >
    > > Pager1.PageSize = 10;
    > > Pager2.PageSize = 10;
    > > Pager1.CurPos = 30;
    > > Pager2.CurPos = 30;
    > > Pager1.DataBind();
    > > Pager2.DataBind();
    > >
    > > and so on. Both instances of "MyPager" look exactly the same and are
    > > databound with exactly the same data. Seems like it would be easier to

    > just
    > > have one instance of the pager, but allow it to somehow render itself

    > twice
    > > on the containing page, so I wouldn't have to have all these duplicate

    > lines
    > > of code. Is this possible in ASP.NET?
    > >
    > > --
    > > Ed
    > >
    > >
    > >
    > >

    >
    >
    Ed Klein, Dec 5, 2003
    #3
    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. Dan
    Replies:
    1
    Views:
    5,372
    Christophe Gijbels
    Jan 4, 2004
  2. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    533
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  3. Mad Scientist Jr
    Replies:
    0
    Views:
    617
    Mad Scientist Jr
    Mar 22, 2006
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    518
    Jonathan Wood
    Feb 2, 2010
  5. Kiran More
    Replies:
    2
    Views:
    301
    Kiran More
    Nov 14, 2006
Loading...

Share This Page