How to force ASP.NET server elements to get the shortest identifiers?

I am using WebForms ASP.NET 4.0. I also use master pages that inherit from other main pages. I have control panels inside. Basically, there are layered containers. This causes items to have a HUGE ID. I see an ID about 300 bytes long!

When there are hundreds of elements on a page, these identifiers greatly increase the page size. I have a GridView with snap controls.

What is the best way for each server control to have the shortest identifier? Is there a way so that each element does not depend on its container (except for ClientIDMode="static" ) ID, even if it was still unique on the page? Finally, does ASP.NET MVC alleviate this problem?

+7
source share
4 answers

I would suggest changing ClientIdMode to Predictable or Static to see if shorter identifiers create.

In addition, this CodeProject article allows you to achieve what you need.

+3
source

MVC completely fixes this problem, since there is no third-party server on the html-code side. All of your html is right in your views, so you have full control over every element. You also risk (in the case of a page of hundreds of entries) oncoming entries.

One way to help reduce all the html created in WebForms is to remove everything that is not absolutely necessary for working with webusercontrol. For example, most shortcuts are static. They can normally be replaced with standard text or elements that do not include the runat="server" attribute. This will prevent the creation of identifiers in the first place. Another way to reduce the amount of junk mail that is generated is to remove as many controls as possible from ViewState. This will prevent them from loading status data and reduce the viewing time of the ViewState.

ClientIDMode is an inherited property, so you can set it in web.config (global), web.config (local) or page level. You can also use it individually. Your question specifically addresses this, but it is probably the best option with maximum flexibility without overwriting what you already have. If rewriting is not a problem, I would recommend using MVC.

+2
source

I think ClientIDMode=static is in one direction. You can also install this in your web.conf. And in Asp.Net mvc you have full control over html (especially in Razor). There are no ideas like webforms. You have control over the assignment of identifiers to html tags. Infact Asp.Net mvc meets all other frameworks.

+1
source

Finally, does ASP.NET MVC do this problem?

Yes.

There is no ViewState in MVC, and you are not using any controls on the server.

You do not use any server controls that generate a lot of hard-to-read HTML.

You have full control over ClientID in MVC.

 <a href="#" id="myID">Click</a> 

The above markup will always have the identifier myID , unless I change it myself.

+1
source

All Articles