How to decide: user controls vs. composite controls vs. rendered custom controls

There are several ways to make your own server controls in
ASP.NET.  You may have wondered which type you should make. 
There are two basic types:

  • user controls
  • custom controls

Within custom controls, you have rendered controls, and composite
controls.  A composite control could merely inherit from another
control and change or add something.  More often, though, it
creates many child controls to render inside itself.

A User Control is the easiest to make, and it was originally going to
be called “pagelets” because the creation experience is pretty close to
that of a page.  You have the html view and a code-behind. 
It’s just a snippet of what would go on a page.  You can lay out
your html and add other server controls inside, and then control them
with your code-behind.  This type of control should be considered first.  If you need a control that is specific to a particular application, this type of control is for you.

A custom control is all code and is compiled into an assembly. 
The main benefit is that the assembly can be distributed and shared
among many applications.  If you don’t need to share controls
between applications, then it may be hard to justify using this type of
control.  This control is more time-consuming to create.  One valid reason may be that you need a control all over in
your application.

Within the custom control umbrella, you have the rendered controls and
composite or inherited controls.  I prefer the inherited controls
myself.  Often, I’ll inherit from a Panel (a <div/>) and add
other controls as children.  Another method some prefer is
rendered controls.  This is when you inherit from Control and
override the Render method and output all html yourself.  This can
quickly become cumbersome and if very error-prone.  I would
consider this a last resort.  In some cases, a performance
argument is made, and your decision will have to be based on the
application environment.

In summary, if you need to distrubute controls in an assembly, you will
have to use custom controls and decide between rendered or
composite.  If all your controls will stay with the application,
user controls are the most compelling answer.

A must read for every ASP.NET developer is Developing ASP.NET Server Controls and Components.


In the current events space, contratulations to .Net Junkies for upgrading their blogging engine to Community Server.

Update:  Thank you Fregas for the correction.  I’m very
excited about declaratively setting properties on user controls.

ASP.NET trust levels demystified – level 300

The good news is that most of the web applications I write work just
fine in medium trust, so it’s very easy to run your ASP.NET app in
partial trust.  All it takes is a change to your web.config.
    <system.web>
        <trust level=”Full”/>
       . . ..
    </system.web>

ASP.NET runs at Full trust by default (trust levels didn’t exist in
v1.0).  Change the level attribute above to a different setting to
change the permissions of your ASP.NET code.  You can change your
trust level, and here’s the rundown of what each one means:

  • Full trust – your code can do anything that the account running it can do.
  • High trust – same as above except your code cannot call into unmanaged code. i.e. Win32 APIs, COM interop.
  • Medium trust – same as above except your code cannot see any part of the file system except its application directory.
  • Low trust – same as above except your code cannot make any out-of-process calls. i.e. calls to a database, network, etc.
  • Minimal trust – code is restricted from anything but the most trival processing (calculating algorithms).

The above lines are the most significant differences that would lead
you to choose a particular trust level.  Read more about trust leves and code-access security from MSDN.

Switch Internet Explorer 6.0 to standard-compliant mode – level 200

It’s very easy to have IE render your web pages using current web
standards, but IE operates in “quirks” mode quite a bit, but it depends
on the web author.  If you are using ASP.NET 1.1 with VS 2003,
then this doctype is added for you automatically:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
If you leave this, IE will not render your page using web
standards.  The problem is that the full Url to the DTD is not
included.

To switch IE 6 to standards-mode, change the Doctype to this:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;

This is the minimum you should do to every page you create with
ASP.NET.  There are other doctypes you should use for other
reasons, but at least change the Doctype to make IE render in
standards-mode.

Here’s a page from Microsoft that explains this more.

Consistent look in multiple browsers: Use a DOCTYPE – level 200

The <!DOCTYPE /> tag in an Html document is very fundamental,
but a good number of people don’t know what it is, and they don’t use
it.  If you are one of them, read on. 

The W3C makes standards recommendations for the web and DOCTYPEs is one of them. 
The Doctype tells the browser what content to expect and what set of
rules to apply.  Look at a web page you created with Visual Studio
2003.  You will notice that it defaults the Doctype to :

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

If you only test your web applications in Internet Explorer, you
have probably, through trial and error, found the workarounds to make
your web pages look good.  Switch this top line in any page to:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“>

See what parts of your web page look messed up.  You will
see that CSS is rendered differently in some cases.  There are
plenty of resources on the web for learning the rules of a particular
Html version.  What you want to do is choose a Doctype that will
allow you to write a web page once and have it look good in multiple
browsers.  With that goal in mind, don’t use the Html 4.0
Doctype.  Use at least the Html 4.01 doctype.  There are some
specific CSS rendering *bugs* with the 4.0 Doctype, but you can study
those on your own if you wish.  Here’s a simple web page you can
use to see the difference each Doctype makes (with different browsers
as well). 

<html>
 <head>
  <meta http-equiv=”content-type” content=”text/html;charset=utf-8″ />
  <meta http-equiv=”Content-Style-Type” content=”text/css” />
  
  <style type=”text/css”>
    #myDiv{
     background-color: yellow;
     padding: 5px;
     margin: 5px;
     border-style: double;
     height: 100px;
    }
  </style>
 </head>
 <body style=”margin:0px; background-color:AliceBlue”>
  

Hello, this is a div.

 </body>
</html>

This page is incomplete because it doesn’t have a Doctype at the top.  Choose a doctype from the W3C and
see how this page is rendered.  You won’t see any difference
between Html 4.01 and the Xhtml varieties.  This page doesn’t have
any mal-formed tags, so there’s no observable differences.  Note
that ASP.NET 1.1 server controls don’t render Xhtml, so it may be a
good idea to use the Xhtml 1.0 Transitional Doctype.  ASP.NET 2.0
server controls will render proper Xhtml.

With Internet Explorer, you can control “standards-compliant”
mode.  If your Doctype includes the full Url where the DTD can be
found, it will render with standards in mind.  If the full Url is
left out, it will render in old-style “Quirks mode”  Check out this page by Microsoft that explains how to ensure IE 6 renders in standards mode.

There are valid reasons to choose one Doctype over the other, and
currently I use Html 4.0, Html 4.01 as well as Xhtml 1.0
Transitional.  A gotcha is that VS 2003 puts the Html 4.0 Doctype
at the top of the page without the Url to the DTD.  Excluding the
DTD Url leaves IE in quirks mode, and will render pages differently
that other standards-compliant browsers.  To tell IE to use web
standards, add the DTD Url to the Doctype, and IE will switch its
rendering mode.

I would also recommend reading up on web standards. 
Doing that will take the mystery out of afternoons when you can’t
figure out why your pages don’t render as you expect them too.  If
you know the rules that are being applied, you can create your web
pages faster.

Fix the network settings for the VSTS Beta 2 VPC image – level 100

At Tech Ed, Microsoft handed out DVDs with a VPC image ready to go with:

  • Windows Server 2003 Standard
  • VS Team Suite (included all of Team System and Whidbey).
  • Sql Server 2005 April CTP
  • Microsoft Office 2003
  • Visual Studio Tools for Office.
  • All of it expires in Sept 2006.

This is the one of the best ideas that Microsoft has had!  Many
were feeling the pain of beta installs, especially with Team System, so
now they release VPC images  that are ready to be evaluated. 
I’m loving it.  I’m using this image for all my .Net 2.0 Beta 2
goodness, and I didn’t have to spend hours setting it up.  Hop
over to Microsoft or talk to your local Developer Evangelist (or User
Group) to get your copy of the DVD.

I was frustrated, however, that my image couldn’t get on the
Internet.  After some investigating, I discovered that the network
connection had an explicit IP and a gateway of 198.168.1.1.  I’ve
made that typo before (198 instead of 192), but I just cleared it out,
use DHCP, and now my image can get to the Internet.  A small
oversite on the image-creators, but an easy fix.  I love VPC for
beta software!  I’m diving into all the ASP.NET security
features. 

A _great_ idea with 2.0 is a machine-wide web.config file located at:
%WinDir%Microsoft.NETFrameworkv1.1.4322CONFIG.  If you have a
team of developers that are in charge of an app, you can set the
machine to High trust instead of Full, turn override off, and they
won’t be able to set their app to Full trust (this assumes that you
don’t want them running arbitrary Win32 API calls from their web pages,
or COM interop).

How to render up-level html to FireFox from ASP.NET 1.1 – level 200

Out of the box, ASP.NET 1.1 doesn’t recognize FireFox, but FireFox is
now probably the second most popular browser behind IE.  IE still
has the market, and if you are just checking IE, you probably won’t
feel any pain, but when developing an app for the Internet, you should
make sure that your site works and looks good in the popular
browsers. 

Since ASP.NET 1.1 doesn’t recognize FireFox, the built-in WebControls
will render down-level html.  What that means is that every
<div/> tag will be replaced by a single-cell table, and a host of
other changes.  This isn’t good because it will distort the layout
of your page.

The good news is that ASP.NET 1.1 can easily be told of the
capabilities of other browsers through the web.config file.  You
can customize the regular expression used to evaluate the User Agent
string, but if you quickly need to support FireFox as well as IE, paste
the following into your web.config within the <system.web/> node:

    <browserCaps>
            <case
match=”^Mozilla/5.0 ([^)]*) (Gecko/[-d]+)(?’VendorProductToken’
(?’type'[^/d]*)([d]*)/(?’version'(?’major’d+)(?’minor’.d+)(?’letters’w*)))?”>
                browser=Gecko
                <filter>
           
        <case
match=”(Gecko/[-d]+)(?’VendorProductToken’
(?’type'[^/d]*)([d]*)/(?’version'(?’major’d+)(?’minor’.d+)(?’letters’w*)))”>
                        type=${type}
                    </case>
           
        <case> <!– plain
Mozilla if no VendorProductToken found –>
                        type=Mozilla
                    </case>
                </filter>
                frames=true
                tables=true
                cookies=true
                javascript=true
                javaapplets=true
                ecmascriptversion=1.5
                w3cdomversion=1.0
                css1=true
                css2=true
                xml=true
                tagwriter=System.Web.UI.HtmlTextWriter
           
    <case
match=”rv:(?’version'(?’major’d+)(?’minor’.d+)(?’letters’w*))”>
                    version=${version}
                    majorversion=0${major}
                    minorversion=0${minor}
           
        <case match=”^b”
with=”${letters}”>
                        beta=true
                    </case>
                </case>
            </case>
        </browserCaps>

ASP.NET apps – things of utmost importance – level 200

I’ve inherited an ASP.NET application that evolved as it went, and my task is jump in and continue to enhance it. In order to do that, I’ve found that I need to refactor it in order to sustain its development. As it is now, it will continue to be a large, unmaintainable beast.

The most important thing I’m doing is taking parts that are repeated and refactoring them into controls. This app was made with aspx pages as the focal point, and I’m refactoring to focus on controls. My goal is to enable a new page to be constructed by simply adding the appropriate controls. I don’t want any code in the code behind of pages except for a property setting or method call on child controls. It’s a lot easier to test custom controls than it is pages. Plus, it leads to a more maintainable site. For those reading: if you aren’t comfortable with custom controls, then put everything inside user controls. You will still get the benefits of reuse and encapsulation.

My rule of thumb for pages is if I have to scroll to see all the markup, then it’s too much, and I’m going to refactor to controls. I’m a little more relaxed with this rule for user controls because they can be reused, but I’m firm about pages. Less is more.

One quick note: I’m not doing RAD (disposable) software, and I rarely use the designer, so my rules apply to a more sustainable software paradigm, but they make no sense in a RAD environment [not many things do :)].

ASP.NET with FireFox – level 200

I’m a little slow on testing my ASP.NET apps with non-IE browsers, and it’s because I used to be in an environment where I was writing internal web applications, and IE 5+ was mandated.  That took away headaches, but now I need to have ASP.NET apps that look good and function well in all the major browsers, so right now, that includes FireFox.  Although IE still has an overwhelming market share, in my opinion, the Internet savvy are split more evenly in browser usage.  There are tons of computers that don’t get on the Net very often, and those all have IE, so I think it skews the numbers.

When testing my apps with FireFox, I noticed that ASP.NET was rendering for a down-level browser.  This is most likely because when .Net shipped, FireFox wasn’t a major player, but now it is, and it supports all the high-level features, so it’s necessary to add an entry to the <browserCaps/> section of the web.config.  Here is a page that can provide an applicable entry to make .Net render up-level to FireFox.

Some of the issues you will run into is that <div/> tags will be rendered as tables, and style percents will not be rendered at all.  It will dumb down the page to HTML 2.0.  Adding the <browserCaps/> section will cause the HTML rendered to be the same as that rendered to IE.  In IE, I use the style: TEXT-ALIGN:cetner to center contents of a div, but in FireFox, that doesn’t work (it may be a standard thing, I’m not too sure), but doing <div align=“center“/> fixed the problem in both browsers.

I’m also using the XHTML doctype.  If you search newgroups, you’ll find a lot of people poo-pooing xhtml as just a worthless fad, but if there is one and only one benefit (I’m sure there are more), it’s that my CSS is truly cascading.  In HTML 4.0, I have to redeclare classes because the settings don’t flow from the parent container to all children.  <td/> tags are notorious for this.

Work with me on cool software – now hiring! – level 000

I’ve joined another company (www.pluck.com), and my team needs more developers.  The company is a small start-up (25 people) that’s been in operation for 2 years.  The main applications are ASP.NET and services.  It’s an administaff company with really good benefits and good salaries.  We need more people who can start within the month and immediately work on an application.  We need senior people (no coaching required).  The company is at 7th and Brazos in downtown Austin.
 
When I say senior, I mean senior (and will be compensated accordingly).  If hired, you’d be working on the same projects I’m working on.
 
Knowledge of TDD is a huge plus.
 
If interested, please email me through my blog at www.jeffreypalermo.com.  We need several people to start within a month, so expect a reply immediately.  That’s just enough time for a successful interview and to give your current company professional notice.