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

You are currently viewing the ASP.NET 2.0 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 June 16th, 2006, 05:31 PM
Friend of Wrox
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default JavaScript and User controls


I'm confused about how ASP.NET assigns DOM level IDs for items inside user controls.

For example if I have a label declared as follows:

<asp:Label ID="lblAddress" runat="server">Address:</asp:Label>

I would expect that my ID would come up in the browser as lblAddress. That way I could execute Javascript against it. But it turns out that ASP.NET prefixes different stuff to the ID, depending on where the control is, i.e. is it in a page, a panel within a page, on a master, in a user control etc.

What I've been doing is just finding it in the control in the source and then copying the ID and pasting it into my JavaScript. Very clunky. It would be much better to either:

1) be able to find it in the javascript, using some method similar to getElementById

2) Know exactly how ASP.NET is going to Prefix my IDs, based on where they are.

The former method would be more desirable, but the latter is probably more realistic.

Thanks for your help.


Old June 16th, 2006, 05:43 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Aaron,

You can use ClientID and UniqueID for that.

So, myTextBox.ClientID will return the ID you'll find in the final HTML...

Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004

Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
User Controls..... Help please cowboy2066 General .NET 2 July 28th, 2004 09:22 AM
User Controls Duncan Pro VB.NET 2002/2003 2 December 1st, 2003 05:41 AM
User Controls Duncan Pro VB.NET 2002/2003 1 October 27th, 2003 12:51 PM
User Controls raZZa VS.NET 2002/2003 3 July 30th, 2003 07:36 AM

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