0

I'mm getting really long client ids one such example is below. Some of them are about 100 characters long which is ridiculous. Is there anything or any tool i can use that will shorten the pages ids?

id="ctl00_ctl00_MainContent_MemberMain_Abc_asdsad...._DlSAdasdasdasd_ctl00_PnsadasdasdnsHF"

EDIT: I am nesting a lot which is the cause of the problem. I am using .net 3.5. and collisions are not ok as some of the ids are being used by javascript funcitons

Letseatlunch
  • 2,432
  • 7
  • 28
  • 33
  • 3
    Do you need your resulting ids to be unique? Or are collisions ok? – robert Dec 20 '10 at 20:32
  • Easiest fix: don't nest so much. Client IDs by default are based on the control's name in code and its parent control. And you'll be generating better HTML besides. :P – cHao Dec 20 '10 at 20:33

6 Answers6

2

If using ASP .NET 4, you can set ClientIDMode to Static. Then you just have to make sure yourself that control ids in the hierarchy is unique. See this.

driis
  • 161,458
  • 45
  • 265
  • 341
2

I encountered this issue as well, but my solution was on the side of jQuery. I simply changed the "selector". Normally the ID that you are looking for is the last part of the ASP.Net given ID. By default the calls for the ID are generated with this selector: #theid so I changed it to [id$=theid] which is a CSS3 selector to find the element which id ends in "theid". Example:

From:

if (jQuery("#txtEmailFinish").val() != "") 

Change to this:

if (jQuery("[id$=txtEmailFinish]").val() != "")

Could be helpful if you already got your javascripts working and with a simple find and replace, change the call to the ids.

Note: The reason why I had to do this was because I needed to make an app that was developed in ASP.Net 4.0 back into ASP.Net

Nelson Rodriguez
  • 492
  • 3
  • 12
1

Maybe. If you're using .NET 4.0. If not, I don't think there's a way to shorten the name. In ASP.net <=3.5, each ContentPlaceHolder or UserControl will add a its name to the If you're trying to access these with javascript, you can use a few different methods.

For .NET 4.0 Only: Put ClientIDMode="Predictable" or ClientIDMode="Static" in your MasterPage or UserControl Declaration (and maybe your Page Declarations).
If you're on .NET 3.5 or less, you can't shorten, but you can use one of these:

  1. JQuery has a function which allows you to find by end text:
    var ElemIWant=$("[id$=PnsadasdasdnsHF]");

  2. Use an inline ASP call:
    var ElemIWant=document.getElementById('<% =TextBox1.ClientID %>);

There's also another Article you can look at: Need workaround for .Net Master Page Name Mangling

Community
  • 1
  • 1
ChrisG
  • 1,403
  • 13
  • 22
1

If your intention is to compress the amount of output of the page, consider a HTML compressor such as HTMLCompressor:

http://www.textfixer.com/html/compress-html-compression.php

I found that by removing all the white space from the ASP.NET markup I made a saving of 45%on AJAX bandwidth. Use Fiddler (http://www.fiddler2.com/fiddler2/) to see the difference.

Zymotik
  • 6,412
  • 3
  • 39
  • 48
0

What version of VS.NET/.NET are you using? The new ClientIDMode in ASP.NET 4 allows you to control how the client IDs are generated:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=492

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
0

The INamingContainer interface is probably what you are looking for. Implement it on surrounding controls.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90