Click here to Skip to main content
Click here to Skip to main content

Syntax Change When Building Controls Dynamically in ASP.NET 2.0

By , 19 Apr 2006
 

Introduction

This tiny snippet will attempt to save developers that are porting ASP.NET 1.1 code to ASP.NET 2.0 code. Especially if controls are being built dynamically.

Using the code

The code posted here should replace any legacy ASP.NET code when converting to ASP.NET 2.0 if you are building server-side controls dynamically. It can even be a find and replace but not doing it will cause those controls to not pick up the correct values out of them.

Below is how I retrieved a value from an ASP.NET server control that was built on the fly or dynamically using VB.NET 2003.

Request.Form(me.ClientID & ":controlname")

The above code snippet will not work in Visual Studio 2005. There is no error thrown but the value property of the control returns nothing when there is a valid value available. This makes this little issue hard to catch without unit tests. Below I have included the same line of code as before but with one fundamental difference. The ":" gets replaced by "$".

Request.Form(me.ClientID & "$controlname")

This fixes the issue for ASP.NET 2.0. I did not see any documentation on this so I hope this is useful to someone.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

bholliman
Web Developer
United States United States
Member
I am the lead software architect for Benefit Communications Inc. based in Nashville, TN. I specialize in enterprise development for windows and the web using .NET and IVR (Interactive Voice Response) systems. I hold the MCSD and MCDBA. I have a passion for video production. I love everything from shooting the video to editing it and everything in between. I love new technology and spend a lot of time on Microsoft's Research website. I am also very interested and amazed at the potential power of nanotechnology.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDid you already try to use me.UniqueID instead?memberptmcomp19 Apr '06 - 9:31 
I don't have a ASP.NET environment available at the moment. But I believe that me.UniqueID would work on both versions of .NET.
 
-- modified at 15:32 Wednesday 19th April, 2006
AnswerRe: Did you already try to use me.UniqueID instead?memberbholliman19 Apr '06 - 9:44 
me.UniqueID does work on both versions. The idea of the article was to warn those who have built controls dynamically to note that the character in between the uniqueid and the name of the control has changed.
 
Example:
 
Let's say that I am building a control on the fly called MedOptions and I want to grab the value of it when i leave the page. So in order to do this I would want to qualify the call by saying:
 
Request.Form(me.UniqueID & "$MedOptions")
 
This allows me to make sure that i have the correct control.
 
Well in previous versions of .net the code was called like this:
 
Request.Form(me.UniqueID & ":MedOptions")
 
Notice the difference in the colon and the dollar sign? Makes a world of difference in whether or not you get the value out of the control or not. I was just trying to save some people a little headache.
 
:: Another Dopeless Hopefiend ::
GeneralRe: Did you already try to use me.UniqueID instead?memberptmcomp19 Apr '06 - 9:58 
What's the output of Request.Form(me.UniqueID & "$MedOptions")?
Note that UniqueID doesn't return the same String as ClientID.
GeneralRe: Did you already try to use me.UniqueID instead?memberbholliman19 Apr '06 - 10:38 
I sincerely apologize about that! I meant ClientID just as it says in the article. I'm sure that has confused everyone.
 
So to answer your initial question, no i have not used UniqueID. It may produce the same results as ClientID.
 
I realize it doesn't produce the same string but to request the value of a specific item in a control built dynamically I have always used clientid and that requires a concatenation of clientid + [colon|dollar sign] + controlName to get the value.
 
Sorry for the confusion... Blush | :O
 
:: Another Dopeless Hopefiend ::
GeneralRe: Did you already try to use me.UniqueID instead? [modified]memberbreischl13 Jul '06 - 5:53 
Actually ClientID is slightly different, it uses yet another delimiter that's more friendly for Javascript.
 
So under 1.1, a control might have these IDs:
UniqueID: logindialog:chkPersist
ClientID: logindialog_chkPersist
 
And under 2.0, it would be like this:
UniqueID: logindialog$chkPersist
ClientID: logindialog_chkPersist
 
The UniqueID gets used for the HTML name attribute, so that's what the form value will be available as. The ClientID is only useful for clientside scripting code (ie, Javascript)
 
-- modified at 12:08 Thursday 13th July, 2006
I forgot to mention, we've noticed that the delimiter used in UniqueID seems to change based on the environment. On all our dev machines it still uses a colon, but on the Win2k3 test servers it uses a dollar sign. We really don't know why that is. My guess is that it's something to do with IIS 5.1 vs IIS 6.0, but that's just a guess....
 
Best of luck,
BKR

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 19 Apr 2006
Article Copyright 2006 by bholliman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid