Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ASP.NET server controls get the shortest IDs?

I am using ASP.NET 4.0 WebForms. I am also using master pages which inherit from other master pages. I have controls inside panels. Basically there are multilevel containers. This is causing elements to have HUGE ID's. I am seeing ID's about 300 bytes long!

When there are hundreds of elements in a page, these ID's increase the page's size dramatically. I have a GridView with binding controls.

What is the best way to have each server control have the shortest ID possible? Is there a way to have each element not be dependent on its container (other than ClientIDMode="static") ID even if but still be unique in the page? Last, does ASP.NET MVC alleviate this issue?

like image 515
Tony_Henrich Avatar asked Jun 20 '12 20:06

Tony_Henrich


2 Answers

I would suggest changing the ClientIdMode to either Predictable or Static in order to see if that produces shorter ID's.

Further to that this CodeProject article appears to achieve what you need.

like image 156
m.edmondson Avatar answered Nov 14 '22 00:11

m.edmondson


MVC absolutely alleviate this issue because there is no server side rendering of html code in the same manner. All of your html is directly in your views so you have full control over every item. You also run the risk (in the case of a page that has hundreds of inputs) of colliding inputs.

One way you can help shorten all the html produced in WebForms is to remove anything that is not absolutely necessary to be a webusercontrol. For instance, most labels are static. They can replaced normally with standard text or items that don't include the runat="server" attribute. This will prevent ids from being generated in the first place. Another way to reduce the amount of junk that gets generated is to remove as many controls as you can from the ViewState. This will prevent them from loading their state data and keep the ViewState shorter.

ClientIDMode is an inheritable property so you can set it in the web.config (global), web.config (local) or page level. You could also use it individually. Your question specifically eliminates this, but it would probably be the best option with the most flexibility without rewriting what you already have. If rewriting is not an issue, I'd recommend using MVC.

like image 39
Joel Etherton Avatar answered Nov 14 '22 01:11

Joel Etherton