Getting rid of the Naming Container in asp.net 2.0

September 12th, 2007

Lately I’ve been playing around with asp.net 2.0.
One of the new "features" of asp.net 2 is the introduction of Naming containers.
While using naming containers can be really useful when using databound controls and standard asp.net coding, they can be a real pain to work with when making extensive use of javascript.
This is because a control declared as

<asp:TextBox ID=”TextBox1” runat=”server” />

will be rendered as
<input name=”ctl00$ContentPlaceHolder1$TextBox1” type=”text”
    id=”ctl00_ContentPlaceHolder1_TextBox1” />

on the client side, thus breaking any script referencing TextBox1 with, for example, getElementById(’TextBox1′).
This behavior is built inside asp.net and is triggered not only when using databoud controls (where it would be necessary to prevent having multiple controls with the same name, for example inside a datagrid), but it is also triggered when using Master Pages, where having multiple controls with the same name is almost impossible.
So, how do we code around it?

The client-side hack

Luke Foust, in his article Using JQuery to Make Asp.Net Play Nice with Asp.Net, explains a couple of client-side methods to let your javascripts select the right element in this situation. I recommend you to read it, because it contains some nice ideas and it also shows off some of the power of JQuery.
While these methods will work for most projects, they still present a couple of issues:

  • Generally quite hard to mantain, especially the first hack
  • Code bloat. While bandwidth is costantly becoming less of an issue, the generated page will have elements with horribly long declarations
  • It breaks existing scripts. You’ll have to re-check and re-code most of the scripts that should already work
  • Extra workload on the client. Todays PCs are fast, so this is less of an issue, however it is something you should consider if you have a lot of asp.net controls in your page.

The Solution

Let’s face it: the problem is really on the server, not on the client. Asp.net should not generate those horribly long IDs in the first place, unless we want to. So the best solution is a server-side hack.
How does asp.net know whether or not it should generate a unique id for our controls?
Every WebControl in asp.net exposes a property called (I’m sure you guessed it) NamingContainer, which tells asp.net what’s the parent control for our textboxes, labels and so on. All we have to do in our code is create a new class that will inherit the control we want to "sanitize" and hide that property from asp.net. Better said in code than in words:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace wtd
{

    /// <summary>
    /// A TextBox control without ugly IDs
    /// </summary>
    public class TextBox : System.Web.UI.WebControls.TextBox
    {
        public override Control NamingContainer
        {
            get
            {
                return null; 
            }
        }
    }

}

Then, in our pages, all we have to do is register our new set of controls, like this:
<%@ Register TagPrefix="wtd" Assembly="app_code"
     Namespace="wtd" %>

and replace every instance of the default asp controls with our own set of controls (a simple find&replace will do the trick), so
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

becomes:
<wtd:TextBox ID="TextBox1" runat="server"></wtd:TextBox>

And that’s it! Our textbox will now be rendered like this:
<input name=”TextBox1” type=”text” id=”TextBox1” />

Not only this method won’t break your existing javascript, it also works nicely with your existing server-side code :)

Update

As pointed out by some of you in the comments, this method does not work in a traditional post-back scenario. See my new article for a possible solution to this problem.

gljakal.com gets a facelift

August 29th, 2007

As you may have noticed, the home page for this website just changed its dress!
gljakal.com: the new design

But the news don’t just stop here.
Along with the new website, a new program is now available for download: it’s Irrlicht Font Maker, a program that will surely save hours to the many Irrlicht developers around the world.
The purpose of Irrlicht Font Maker is to generate 32bit, alpha mapped font textures ready to be used inside the Irrlicht Engine, a lighting fast open source 3d engine.

So, what do you think about the new design and new program? Let me know in the comments!

PS. For more information about the Irrlicht Engine, visit the source forge page and the author’s blog.

How to start any program with Windows

June 22nd, 2007

A lot of people asked me what’s the best way to launch Clock! as soon as they log into their computers.
In Windows, there is a very simple way to start any program right after you type your username and your password in the login screen. It’s called the “Startup Menu”.

The startup menu

The startup menu is simply a folder located under “All programs” in your Start menu. Every program located in this folder is automatically launched when you log into windows.
How do you add an item to the startup menu? The first thing to do is right-click it and choose Open. Now, probably you have a link to the program you would like to launch on your desktop. Just drag and drop your desktop icon inside the startup menu folder and there you have it: your favorite program will now start with windows!

Clock3D screensaver progress

April 24th, 2007

Progress on Clock3D screensaver is slow, but ongoing!
Here you can see the new “Fashion watch” skin:
Clock 3d screensaver
More pictures to come!

Clock reaches version 2.3

April 19th, 2007

Finally, after way too much time, Clock has reached the stable version 2.3!

Clock 2.3 - 10 of the 50 included skins
New features over the beta version:

  • clock's style manager
    Improved style manager: now you can create and save your custom skins, preview all the skins, customize all the details, hide the second hand.
  • Quick calendar: a small calendar window, useful when you are planning a trip, or a visit to the doctor! The calendar window can be resized to display multiple months.
  • Even more skins because they are never enough :)
  • Improved stability: no more need for the library files setup, everything just works at the first install!

Of course, all the cool features that were in the beta versions also made it to the new stable:

Get it now!

gljakal’s MP3Streamer

April 17th, 2007

gljakal's MP3Streamer screenshot
Have you ever visited the website of an independent musician? Often you will find that they are kind enough to let you download some of their tracks in mp3 format. But then you have to download the mp3 somewhere on your computer, wait for the transfer to complete and (if you still remember where you actually saved it!) open the mp3 file with your audio player of choice. Isn’t there a quicker way to listen to a song or a podcast? Sure there is!
Gljakal’s MP3Streamer does just that, in the simplest way possible: just copy the address of the song you want to listen in the “mp3 file address” field and hit “Stream” to start listening: no downloads, no waiting, it’s all set!
If you are a musician or a podcaster, you can also use the HTML code provided to embed the mp3 player directly into your site!
Also, if you are really really lazy like me, you can get a bookmarklet that will convert all the links to mp3 files in the page you are currently visiting in mp3 players :) .
Have fun!

File Partitioner version 2.0 released!

December 11th, 2006

A newer version of File Partitioner is out today.
It now features its own setup program, improved performance and a much better interface.

File Partitioner version 2.0

Go to the program page to learn more and download it.

Coming soon: 3d clock screensaver!

November 28th, 2006

Almost ready (the first 90% is done, I’m working on the next 90%), this is the screensaver you will be impatient to show your friends!
The screensaver is completely 3d rendered and will require a 3d video card to run (early geforce models or better, the kind of video card any computer bought in the latest four years should have).
It will feature multiple clock models, so you can change your virtual clock every day, depending on your mood :)
Right now it’s still in production phase: it needs a little more polish, a decent installer and some kind of documentation. I am also modeling some new clock model, just to be sure you will have enough styles to choose from ;)
gljakal's 3d clock screensaver
By the way, this screensaver will also need the Microsoft .net framework version 2.0, so you might as well start downloading it.

FontViewer version 1.3

November 23rd, 2006

A new version of FontViewer has been released today.

Features of the new version

  • Character Map The new Character map window allows you to view all the 256 ASCII characters for the selected font. This is especially useful when you are searching for a specific character in graphical fonts like Wingdings and Webdings. You can of course copy any element on the grid.
  • Installed font browser This new window allows you to browse through your installed fonts, so you can finally use only one program for your font hunts.
  • Improved stability The new setup should fix all those “comctl32.ocx” errors many of you are having.

Font Viewer version 1.3

Get it now!

As always, if you have any trouble or suggestion, feel free to leave a comment to this post.

gljakal’s To Do 0.9.2 Preview Release

October 9th, 2006

This time I am giving you a peek of what’s coming in the next version of gljakal’s To Do. Here is a list of the features you will find in the new version:

  • Multiple font support will let you customize the look of the task list. The same font is also used for printing, so you can customize your prints too.
  • TXT and HTML export will enable you to share your to do list even with those unlucky ones that don’t have gljakal’s To Do installed :)
  • Plugin Support, which will give you the ability to bend the laws of the To Do list to your own will! The plugin system, while working, is still missing a lot of functionalities in this beta, so no documentation is available at the moment. Drop me a line if you would like to know more.
  • A brand new Error reporting system, similar to that of Windows XP. I hope you will never see it in action!
  • Other minor addons and fixes. For a complete list, see the included readme file.
  • gljakal's To Do version 0.9.2 Preview Release

    Get it now!

    As always, this is still a beta release, so please report anything that’s not working properly or that you wish to have in the next version, either by comment to this post or by mail.