Overriding ClientID and UniqueID on ASP.NET controls
So I posted the updated wwHoverPanel control yesterday and while it addressed some of the core issues, it still isn't working in all situations due to the naming issues of the ASP.NET containership naming schemes. Well, ASP.NET is doing nothing unpredictable really, but my goal for the control was that it should be easy to reference and access from within script code.
So when the control is hosted in a MasterPage and you're doing a JSON page callback I don't want the user to have to use a cryptic name like:
ctl100_Content_LookupPanel.Helloworld('Rick',Helloworld_Callback);
or
document.getElementById('ctl100_Content_LookupPanel');
To do this the user either needs to type the ugly name above or:
<%= LookupPanel.ClientID %>.Helloworld('Rick',Helloworld_Callback);
So anywhere the panel is referenced then would require code like this which is – well, ugly as shit. It also makes the control handling a lot more difficult, because in some cases the control would have to generate the base ID (LookupPanel) or in other cases generate the ClientID into the code.
To end user the experience is confusing.
Given that this is a 'worker' control, not something that will be stuck into containers and iterated over it seems much more reasonable to use simple names directly:
LookupPanel.Helloworld('Rick',Helloworld_Callback);
and
document.getElementById('LookupPanel');
So, today I realized an obvious solution which is to simply override ClientID and UniqueID in the control class:
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string UniqueID
{
get
{
return this.ID;
}
}
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string ClientID
{
get
{
return this.ID;
}
}
This complete overrides the extended naming functionality that ASP.NET provides and results in the control names being rendered into the output as a simple ID names that don’t reflect the control hierarchy.
So now the code automatically generates the simple ID all the time both for client and server side code and users can talk to this control with its simple name in script code.
For this particular control which essentially isn't bound to the control tree hierarchy this is just fine. The control either fills the panel with data from an
Now, there's a caveat of course: Due to this overriding the page author now has to ensure that the name of the control is unique on the page. Since this control is likely to be only dropped once maybe twice (once for panel operation and once for callback operation) this is not really problem, but it is unconventional. I can live with that.
Can anybody see where this would cause problems beyond possible naming conflicts?
The Voices of Reason
# re: Overriding ClientID and UniqueID on ASP.NET controls
Like that idea of the override control - slick idea. That might come in handy in other places. In this case though the control has to manage the id on its own.
# re: Overriding ClientID and UniqueID on ASP.NET controls
Stig
# re: Overriding ClientID and UniqueID on ASP.NET controls
Thank you guys.
# re: Overriding ClientID and UniqueID on ASP.NET controls
Thank you Rick
Halit Develioglu
Turkey
Keywords used : asp.net custom control unable to find uniqueid
# re: Overriding ClientID and UniqueID on ASP.NET controls
I have been battling with the id issue for quite some time. Your idea of overriding the control class' uniqueID and clientID is great. But where do I override them?
thanks!
yugu
# re: Overriding ClientID and UniqueID on ASP.NET controls
I've been fighting for 5 hours trying to figure out a way to preserve the ID of my controls during an Ajax callback in which some of the controls may have been deleted.
Thanks!!!
Greetings from Spain
# re: Overriding ClientID and UniqueID on ASP.NET controls
I've created a class that inherits from HtmlGenericControl. It overrides UniqueID ans ClientID so that they both return this.ID. It also has contructors in the class that call the base class' constructors... and that's all that I have in the class.
But now I'm getting an error when I try to view the Trace page of a page that uses objects of this type. The error is a Null Reference Exception that occurs in System.Web.Handlers.TraceHandler.CreateControlTable(DataTable datatable) +2442. I'm not 100% sure that using my custom class is causing the problem, but that's my suspicion.
Can someone confirm (or refute) this for me? I'm thinking there's something else I have to have in my class, but I'm not sure what that is. Any help is appreciated!
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
While on other server it is like AAAA_myTab$.
Can anyone have any idea ?
# re: Overriding ClientID and UniqueID on ASP.NET controls
woot woot!
mcm
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
<input id="ProductID" type="text" runat="server" name="ProductID" />
Are custom controls the only solution to this problem?
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
I am in total agreement that Microsoft could have improved the current model before releasing it, but I have long adjusted to the fact if you want to do something (especially using Microsoft), the only way to get it to work and production worthy is to live by the standards bodies such as Microsoft set out. If you try and bend the system to satisfy your own wishes, it breaks. Keeping the UniqueID and ClientID's is certainly the only way to sleep at night, unless of course, your willing to write your own framework, which is something that is really not that difficult!
Good luck to all who choose otherwise! I have been their, had solutions fall over in places are dread to recall.
S
# re: Overriding ClientID and UniqueID on ASP.NET controls
I built this originally for a few AJAX controls which are sort of global managers on a page. In that scenario you're not going to have name-duplication unless a custom control uses the same component AND chooses the same name (possible but unlikely - custom controls at the very least might obfuscate names internally or even be Ok with full client ID names).
It's a special case scenario and I agree - you'll want to be super careful with this sort of 'swimming against the current'.
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
in my website the ID looks like this:
..."TextBox Id="contentplaceholder_visHome"...this is inside a content page...
using your method on master works fine... but what can i do to override the IDs inside de contentplaceholder???
tks!!!
# re: Overriding ClientID and UniqueID on ASP.NET controls
kazim
# re: Overriding ClientID and UniqueID on ASP.NET controls
kazim
# re: Overriding ClientID and UniqueID on ASP.NET controls
I've used your idea to eliminate the unused IDs of Label controls inside a GridView.
I' ve a lot of controls nested inside other controls and the asp.net framework generates very long clientId for every server control.
I've created a SimpleLabel custom control derived from Label and replaced it inside my GridView...
Then I've overriden the ClientID returning null.
The result is that every label is rendered as a simple SPAN element without the ID.
With 20 lines of code I've saved a lot of space in my page.
bye,
Matteo
# re: WOOOOOTTTT
I have a nested dynamic controls which are generated depend on user's action in one page. (no another choice)
And sometimes the button inside the control is not fired after the 1st post back. Why? Because the stupid Client ID keeps changing and Click event is not registered properly no matter how much you have re-initiated them.
It is not advisable to use everywhere, but this can be a workaround for a lot of CONTROL like in mycase.
THANKSSSSSS
JINN
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
http://www.gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/
# re: Overriding ClientID and UniqueID on ASP.NET controls
I came up with another workaround. We can give to each control we want to access with JavaScript a unique CSS class name by setting a value in the CssClass attribute and then we can access an appropriate control with the jQuery selection:
$("tagName.className")
I explained this method in blog post: http://www.mikeborozdin.com/post/CSS-Class-Names-Instead-of-ASPNET-Client-IDs.aspx#comment
# re: Overriding ClientID and UniqueID on ASP.NET controls
This explains the .NET support approach for controlling the generated ClientID. Though - I need to dig deeper as I know there is a hook into page generation where more advanced logic could be injected when needed.
Back to the point, the article covers a few concepts. Specifically, that there are options that can be set at the application, page level, or "context" level.
Best to just read the article versus me cut-and-pasting too much here :)
# re: Overriding ClientID and UniqueID on ASP.NET controls
I eventually got all that working by deriving my own MasterPage and ContentPlaceHolder controls and by juggling the control hierarchy during the page PreInit. I ended up writing about it and providing the source code necessary (C#).
Hope this helps. Feel free to copy the code:
http://www.netquarry.com/index.php/2009/03/master-pages-ajax-and-javascript-10292/
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
I just want to force that my elements have the name I give them, asp doesnt like customized scripts, crappy framework.
Thnx man
# re: Overriding ClientID and UniqueID on ASP.NET controls
# re: Overriding ClientID and UniqueID on ASP.NET controls
Just forget all this and go straight to ASP.Net MVC :) Web forms=FAIL.
# re: Overriding ClientID and UniqueID on ASP.NET controls
document.getElementById('<%= ControlID.ClientID %>')
Where ControlId is the id that you gave to your control at design time
and ClientID is the syntax (use it as it is)
So for example if you have a text box like this:
<asp:TextBox runat="server" id="inpErr" ></asp:TextBox>
then you will write something like this:
document.getElementById('<%= inpErr.ClientID %>')
to find out the actual id of your control.
Good luck,
Sky
# re: Overriding ClientID and UniqueID on ASP.NET controls
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
# re: Overriding ClientID and UniqueID on ASP.NET controls
server side and also java script side which both needed to use the same
name otherwise the radio buttons will not see each other.
but I created a server control based on this article and it worked.
I didnt want to use JQuery to rename because thats not efficient
hence your solution is very good for the long run
and the page does not use post back so your solution works best.
Thanks
# re: Overriding ClientID and UniqueID on ASP.NET controls
In your case, the ID does no longer contain information about the control hierarchy. That's why the Page will be unable to call the control's LoadPostData/RaiseChangedEvent methods. As a result, your control may no longer work if it implements IPostBackDataHandler.
I've implemented a generic control, IDOverride (http://www.wilcob.com/Wilco/News/AspNetIDOverride.aspx), which should both let you do what you want to do, and take care of the problem I described.