Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2009, 05:39 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to higgsy
Default Setting clientID

Hello,

As part of a website im building, there are hyperlinks which form the top navigation. To style these hyperlinks i have created styles within CSS using an ID rather than a class, basically because CSS best practices would dictate using an ID where there is only one existence of that element.

The problem is I am using .NET master pages which means that my hyperlinks are rendered with id's like ctl00_navItem. I only know of 3 ways to get round this, none of them seem like a good solutions.

1) Override the UniqueID and ClientID properties so that the ctl00 ID's are not generated - this obviously poses risks that Microsoft intended to negate by using such unique ID's.

2) Create classes in my CSS and use the .NET CssClass attribute - again, this is changing the way css development is intended to be implemented just to accomodate .NET.

3) Name my ID's within my CSS ctl00_ElementName. As i havent actually set the IDs of my elements with these unique ID's, i presume there is a risk that these IDs could programmatically change as the application grows - this would render my css useless!

Any ideas anyone?

Thanks in advance
higgsy
 
Old April 16th, 2009, 12:44 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

This is actually a pretty good question. I am asked this a lot by other colleagues and such.

First of all, let me say that I congratulate you for favoring ID selectors over classes where you can. That is the way CS is supposed to work, yet many developers (especially ASP.NET guys it seems) never seem to grasp this, or understand why they should even care. So you are on the right track there.

Quote:
Override the UniqueID and ClientID properties
Really bad idea. You already know why.

Quote:
Create classes in my CSS and use the .NET CssClass attribute
Valid, yet far less than ideal. This is what I call the "Class-itis Approach", and unfortunately it is the one far too many ASP.NET devs seem to opt for. Seriously, if you're just going to stick class attributes on every tag, why not just use inline styles and forget the stylesheets altogether, right?

Quote:
Name my ID's within my CSS ctl00_ElementName
While not totally ideal either, this is by far the best approach of the three, as it comes the closest to the spirit of true CSS design principles. I've done it this way many many times, and I can tell you that in actual practice it is far less problematic than you might otherwise think.

The key is to try to get as much of the UI in place as you can up front. Then, during the development phase, as long as the UI remains relatively stable, the chances that you will introduce a "breaking" change that messes up an ID is relatively low. If you happen to do that, just change the stylesheet to target the correct ID -- no big deal.

Once the UI is set, there is no chance at all that the IDs will just "spontaneously" change without intervention from you. In other words, if you don't mess with it, it won't mess with you.

Hope this helps.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}

Last edited by Lee Dumond; April 16th, 2009 at 04:19 PM..
 
Old April 16th, 2009, 01:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not very useful at this moment, I know, but you could delay and delay and delay the project, wait for .NET 4.0 to arrive and then manually set the control IDs:

http://weblogs.asp.net/asptest/archi...-overview.aspx
http://www.mostlylucid.net/archive/2...p.net-4.0.aspx

On top of the useful things that Lee said, you could also try to favor inheritance. While of course not always possible you may not always need an id but instead you could use a parent element as a reference to your iems. For example:

Code:
 
<div id="MainMenu">
  <asp:Repeater....>
    <a href=....... />
  </asp:Repeater>
</div>
Instead of referring to each <a> menu item using an ID, you could of course refer to its parent:

#MainMenu a
{
....
}

You probably knew that already, but I wanted to mention it anyway...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 16th, 2009, 01:39 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Great point from Imar about the upcoming 4.0, and the ability to set the ClientID on child controls.

I meant to mention that. Really, I did.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old April 16th, 2009, 03:53 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to higgsy
Smile

Thanks so much for your feedback, its really useful info, and great to know that this will be addressed in .NET 4.0.

Regards,
Al
 
Old April 16th, 2009, 04:18 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by higgsy View Post
Thanks so much for your feedback, its really useful info, and great to know that this will be addressed in .NET 4.0.

Regards,
Al
You're welcome.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
setting the value of FK csharpa SQL Server 2005 1 March 8th, 2007 01:50 PM
Browser Compatability -getting ClientID in FireFox sailu BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 5th, 2006 10:17 AM
Need help setting up a Biztalk example cxs00u Biztalk 0 November 19th, 2003 08:15 AM
ClientID psingh ASP.NET 1.x and 2.0 Application Design 0 July 10th, 2003 08:42 AM
javascript clientID reference in composite control arjeno ASP.NET 1.0 and 1.1 Professional 1 June 4th, 2003 02:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.