How to duplicate a user control on a page?

E

Ed Klein

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?
 
M

Mark Fitzpatrick

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
 
E

Ed Klein

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top