Click here to Skip to main content
Licence CPOL
First Posted 22 Aug 2002
Views 114,157
Bookmarked 48 times

Positioning your UI elements in a WebControl

By | 23 Aug 2002 | Article
Howto solve the problem of positioning your UI elements in a WebControl.

Introduction

There are two ways of creating controls that can be used in an ASP.NET application. The first way is an UserControl (.ascx) and is fairly easy to create as you can use the Visual Studio to position your controls. The disadvantage of UserControls is that they can't be reused easily as you have to copy them from one web application to another web application. The second way is to create a WebControl, these controls are more portable as they exist in a separate DLL and can be added to your palette. However you cannot use Visual Studio to design them via drag & drop.

First attempt

Because I personally prefer the reuse aspect, I decided that I would always create my controls as WebControls as also the future benefit would outweigh the initial costs. So I created my first WebControl which was a DropDownList with a Label control.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ManyMonkeys.Web.ControlLibrary
{
    /// <summary>
    /// A DropDownList with a named label.
    /// </summary>
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:LabelledDropDownList 
       runat="server"></{0}:LabelledDropDownList>")]
    public class LabelledDropDownList : 
       System.Web.UI.WebControls.DropDownList, INamingContainer
    {
        Label lblHeader = new Label();
    
        [Bindable(false), 
        Category("Appearance"), 
        DefaultValue("")] 
        public string Text 
        {
            get
            {
                return lblHeader.Text;
            }

            set
            {
                lblHeader.Text = value;
            }
        }

        /// <summary> 
        /// Render this control.
        /// </summary>
        /// <param name="output"> 
        /// The HTML writer to write out to </param>
        protected override void Render(HtmlTextWriter output)
        {
            lblHeader.RenderControl(output);
            output.WriteLine("<br>");
            base.Render(output);
        }
    }
}

Which once added to the palette and then dragged onto a form, looked like this.

Well that wasn't so hard, was it? Lets run it up and view it in IE.

Ah. Now as you can see from above WYSISYG is not a term we can use anymore, when dealing with Visual Studio. But how do we fix it?

Solution

The clue is in the positioning of the DropDownList control. It has style tags that cause it to be positioned at a specific location on the screen. But they are not being applied in the same way as they are, when viewed in Visual Studio. To fix this, we need to apply the style tags to both the elements that represents the Label and the DropDownList. However it is easier to wrap both the Label and the DropDownList in a <div> tag and apply the styles to it.

    /// <summary> 
    /// Render this control.
    /// </summary>
    /// <param name="output"> 
    /// The HTML writer to write out to </param>
    protected override void Render(HtmlTextWriter output)
    {
        // get the styles that we want to apply to the div tag
        output.AddStyleAttribute("LEFT",this.Style["LEFT"]);
        output.AddStyleAttribute("TOP",this.Style["TOP"]);
        output.AddStyleAttribute("POSITION", this.Style["POSITION"]);

        // remove the styles we have already applied
        this.Style.Remove("LEFT");
        this.Style.Remove("TOP");
        this.Style.Remove("POSITION");

        // draw our control
        output.RenderBeginTag("div");
        lblHeader.RenderControl(output);
        output.WriteLine("<br>");
        base.Render(output);
        output.RenderEndTag();
    }

Now when you view the control in Visual Studio it looks exactly the same as it did before, however when you view it in IE all is well.

Now it should be noted that you have to call AddStyleAttribute before you start to render you tag using BeginRenderTag, in order to apply the styles to it.

Conclusion

I hope that this small article will help other developers avoid the grief I initially had, when creating WebControls and that they will not be turned off from creating controls that can be reused. I have added this simple fix to many controls that I had originally created as UserControls and now they all exist in a library that I can apply to many web applications. The added advantage is that if I find a bug in a control (usually in the client-side JavaScript), I only have to fix it in one location.

Please spend a moment to rate this article. Your feedback is important to me.

License

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

About the Author

Shaun Wilde

Architect

United Kingdom United Kingdom

Member

All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.
 
Been involved in programming from the early '80s. First on my Spectrum, then an Amstrad. Did lots of Fortran while at University before becoming involved in Forth, Prolog, Pascal, Occam, C and eventually C++. Eventually started programming on the Windows platform using Borland’s OWL framework, during my postgraduate years. When I started work in '94, I learnt MFC followed by COM and ATL and never looked back. Now working exclusively in .NET, working on WinForms, ASP.NET and the Compact Framework using C# and VB.NET. Using every bit of the .NET framework I can such as WebServices and writing my own controls for my .NET Portal, and for the Compact Framework. Still lots I don't know, but the end of the universe hasn't happened yet - I wonder if I'll have time?
 
I was a permanent employee for a number of companies until Dec 2000 before going independent after the DotCom I ws involved with went DotBomb. Now supplying consulting services to various institutions in London and surrounding regions i.e. UK Smile | :) .
 
Now living and working in Australia, trying to be involved in the local .NET and Agile communities. Also maintaining an open source code coverage tool called PartCover whilst thinking of developing a new one.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood tip Pinmemberabhiace9:36 2 Mar '06  
GeneralPositioning WebControls (checkbox) in an ASPX Page PinmemberXavito7:20 27 Sep '04  
GeneralUploading and Downloading of files between a Browser and Server PinmemberVenkataSridharRao Chadalla0:16 21 Jun '04  
GeneralRe: Uploading and Downloading of files between a Browser and Server PinmemberShaun Wilde8:35 21 Jun '04  
GeneralRe: Uploading and Downloading of files between a Browser and Server PinmemberVenkataSridharRao Chadalla3:09 22 Jun '04  
GeneralRe: Uploading and Downloading of files between a Browser and Server PinmemberShaun Wilde9:54 22 Jun '04  
Questionthis is the control class. but how do i use it from my app ???? Pinmemberiddo2k0:37 9 Oct '03  
AnswerRe: this is the control class. but how do i use it from my app ???? PinmemberShaun Wilde8:31 9 Oct '03  
GeneralCSS PinsitebuilderPaul Watson21:22 17 Aug '03  
GeneralRe: CSS PinmemberShaun Wilde1:31 18 Aug '03  
GeneralPositioning in a Netscape browser Pinmemberwajman0:33 15 Apr '03  
GeneralRe: Positioning in a Netscape browser PinmemberShaun Wilde2:14 15 Apr '03  
GeneralRe: Positioning in a Netscape browser Pinmemberwajman21:53 15 Apr '03  
GeneralRe: Positioning in a Netscape browser Pinmemberwajman1:31 16 Apr '03  
Generalfor Shaun... Pinmemberbyblos0:58 24 Aug '02  
GeneralRe: for Shaun... PinmemberShaun Wilde5:48 24 Aug '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 24 Aug 2002
Article Copyright 2002 by Shaun Wilde
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid