Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET
Article

Duplicate WebControls at Runtime for Better Web Usability

Rate me:
Please Sign up or sign in to vote.
4.64/5 (22 votes)
2 May 2006CPOL3 min read 68.4K   442   34   17
A custom control to mirror frequently-used buttons at the top and bottom of your ASP.NET web forms.

Sample Image - mirror.jpg

Introduction

It is an unfortunate reality for web application users that web pages scroll. While this is "useful" for viewing documents, it can be annoying when you are forced to scroll down to save changes to a form. And it's really "really" annoying, when your job involves doing that maybe 1,000 times a day. Astutely, my clients have noticed this, and requested that I place the Save, Cancel, Help, etc. buttons at both the top and the bottom of every page.

Obviously, you can do this without much effort. Simply duplicate the controls at the top of the page, give them new ID's, and then reference the same event handlers that the other controls are using.

But we're programmers, right? And, genetically, we "loathe" duplicate code. Who really wants to call their controls btnSave1, and btnSave2? Totally uncool.

There must be "A Better Way".

A Better Way

What if you had a way to just duplicate a group of controls so that it appeared more than once on the same page? And to have the duplication performed completely at runtime, rather than at design time?

Enter... the Mirror control. The Mirror control is a very simple custom control, whose sole function is to re-render another control so that it appears in more than one location on the page.

It turns out that the .NET control-rendering model can be used to generate the HTML for a control more than once. Any WebControl, including custom controls, will have a RenderControl() method that the Page uses to generate the control's HTML. And you can use it too...

Using the Mirror Control

As with other WebControls, using the Mirror control is horribly complicated. It works like this;

ASP.NET
<cc1:Mirror id="Mirror1" ControlID="ButtonPanel1" runat="server" />

  • Give your control an ID, like Mirror1. If you do not, it is not strictly necessary, Visual Studio will create one for you.
  • Specify the ID of the WebControl you are mirroring through the ControlID attribute.
  • Yes, that's it.

At the top of your page, make certain to reference your custom control assembly, something like this;

ASP.NET
<%@ Register TagPrefix="cc1" Namespace="MirrorControl" 
                        Assembly="MirrorControl" %>

How It Works

Very simply, the Mirror control's Render() function;

  • locates the control you have identified in the ControlID attribute, using the Page.FindControl() method.
  • forces the identified control to render itself by calling its RenderControl() method.

The actual intelligence is just a few lines of code. This is the "entire" class definition for the Mirror control. Who said custom controls have to be complicated?

C#
/// <summary>
/// Mirror control.
/// Duplicates the rendered HTML of another control on the page.
/// </summary>
[ToolboxData("<{0}:Mirror runat=server></{0}:Mirror>")]
public class Mirror : WebControl
{
   // This will be automatically populated on each Page_Load
   // with the value of the Mirror control's ControlID attribute.
   public string ControlID = null;
   protected override void Render (HtmlTextWriter writer)
   {
      // Ensure that the ControlID was defined
      // otherwise abort the render
      if (ControlID == null)
       return;
      // Locate the control identified by ControlID
      Control c = Parent.FindControl (ControlID);
      // If the specified control was not found, abort
      if (c == null)
      return;
      // Call the control's Render function in order to
      // generate the Mirror control's HTML.
      // This, in a nutshell is the mirroring process.
      c.RenderControl (writer);
   }
}

Caveats and Limitations

  • The control you specify will be duplicated "precisely" as rendered elsewhere. Again, "precisely". This includes things like the ID attributes. If you have JavaScript that references those ID attributes, you will probably encounter issues.
  • Avoid mirroring data-entry controls (e.g. TextBox, ListBox, CheckBox, DropDownList, ...), as only the topmost one on the page will work properly.. During a postback, the ASP.NET postback handler will only pay attention to the first control matching the ID, and load its values from there. Thus, if you change the contents of a lower-in-the-page instance of the control, its value will be lost on postback.
  • If you use absolute positioning in your layouts, make sure that you do not use them on the mirrored controls. Otherwise both sets of controls will render in the same location, one on top of the other. [Special thanks to UnderWing for pointing this out]

Tips and Tricks

  • The Mirror control is designed to duplicate one WebControl only. If you wish to duplicate a group of controls, you can use several Mirror controls, or, if they are adjacent, simply wrap your controls in a Panel, and reference the ID of the Panel instead.
  • You can mirror the same control(s) more than once on the same page, if such a thing is useful to you.
  • Avoid mirroring data-entry controls (e.g. TextBox, ListBox, CheckBox, DropDownList, ...), as explained above.
  • Avoid absolute positioning of the mirrored controls.
  • If you need to access both instances of the control, the FindControl() function will only locate one of them. However you may be able to iterate through the Page.Controls collection and locate both.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Sygnal Technology
New Zealand New Zealand
Michael is a systems architect who, oddly, enjoys writing code and even managing large projects. Having spent some 25 years in various computing pursuits, Mike now programs exclusively in .NET, C#, and will never, never touch C++, VB, Java, QuickBasic, Pascal, LISP, or assembly ever again. Unless he gets bored, which seems unlikely.

Comments and Discussions

 
QuestionHow to refresh the mirror? Pin
Member 1210097515-Jul-20 19:44
Member 1210097515-Jul-20 19:44 
GeneralCustom control for Data Pager Pin
Benjamin Sewards8-Oct-13 9:00
Benjamin Sewards8-Oct-13 9:00 
GeneralNice idea and control! Pin
Sandeep Mewara27-Mar-10 5:25
mveSandeep Mewara27-Mar-10 5:25 
GeneralWow. This just...works... Pin
sbguy27-Sep-06 12:43
sbguy27-Sep-06 12:43 
QuestionDoesn't work with JavaScript event handlers Pin
WebMaster2-May-06 9:57
WebMaster2-May-06 9:57 
GeneralRe: Doesn't work with JavaScript event handlers Pin
Memetican2-May-06 14:48
Memetican2-May-06 14:48 
GeneralRe: Doesn't work with JavaScript event handlers Pin
WebMaster3-May-06 5:36
WebMaster3-May-06 5:36 
GeneralDoesn't work with STYLE attribute Pin
WebMaster2-May-06 9:55
WebMaster2-May-06 9:55 
GeneralRe: Doesn't work with STYLE attribute Pin
Memetican2-May-06 10:50
Memetican2-May-06 10:50 
GeneralBrilliantly Simple!!!! Pin
nutcase10-Apr-06 22:32
nutcase10-Apr-06 22:32 
GeneralNice :) Pin
dzCepheus3-Apr-06 10:34
dzCepheus3-Apr-06 10:34 
Hey this is pretty innovative. Smile | :) I had been running into this problem quite frequently as a matter of fact - this will clean my code up nicely. Thanks Smile | :)

PS: This is what part of the alphabet would look like if the letters Q and R were removed.
GeneralId rather use an frameset or Iframe Pin
DeKale3-Apr-06 7:02
DeKale3-Apr-06 7:02 
GeneralRe: Id rather use an frameset or Iframe Pin
James Curran3-Apr-06 9:18
James Curran3-Apr-06 9:18 
GeneralRe: Id rather use an frameset or Iframe Pin
DeKale3-Apr-06 9:42
DeKale3-Apr-06 9:42 
GeneralRe: Id rather use an frameset or Iframe Pin
Memetican3-Apr-06 12:57
Memetican3-Apr-06 12:57 
GeneralRe: Id rather use an frameset or Iframe Pin
DeKale4-Apr-06 0:25
DeKale4-Apr-06 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.