ASP.NET Remove ID From Control

ID attribute

You can remove the ASP.NET ID attributes that are generated by ASP.NET when the page is rendered, which waste bandwidth. On many sites, there are tags that have an ID on them. Here we see how you can remove long ASP.NET ID attributes from the output.

Remove IDs

Set controls' ID values to NULL before sending the page to the browser. This will decrease the size of all ASP.NET pages that have ID values. First, note that the ID is useful in some PostBack scenarios, but it is not useful for Microsoft.com.

Example that removes ID properties [C#]

public partial class SiteHomePage : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
	// After you render the page in C#, remove the IDs.
	RemoveIDs();
    }

    /// <summary>
    /// Reduce page size by 200 bytes by removing the ASP.NET ids.
    /// </summary>
    public void RemoveIDs()
    {
	Body1.ID = null;
	CategoryCountSpan1.ID = null;
	CategoryText1.ID = null;
	CategoryListItem1.ID = null;
	RssListItem1.ID = null;
	Prefix1.ID = null; // Shown in below example.
	Header1.ID = null; // Shown in below example.
	Author1.ID = null; // Shown in below example.
	BottomDivHome1.ID = null;
	BottomDivHome2.ID = null;
	JavaBlock1.ID = null;
	Advertisement1.ID = null;
	LogoLink1.ID = null;
    }
}

Null assignment

Many times in ASP.NET, pages have certain elements (such as in a master page) that have runat="server" and an ID specified. (Note that an HTML ID is different from a server-side ASP.NET ID. HTML IDs are used for CSS and JavaScript.) After you process and build (render) your ASP.NET page in code-behind, simply set the ID to null.

In the above method, we set the ID of each server control (runat=server) element to null in the ASPX page. Here I will show a couple examples of that markup in the ASPX page, just for clarity. Then I will show what the rendered HTML will look like. If you didn't set the ID to null, each of the above elements would have ID="..." sections like the example I show from Microsoft in the problem statement.

ASPX markup

<h1 class="BigPrefix" id="Prefix1" runat="server" />
<h1 class="BigText" id="Header1" runat="server" />

<div class="AuthorDiv" id="Author1" runat="server"></div>

HTML output

<h1 class="BigPrefix">C#</h1>
<h1 class="BigText">Problems and Solutions</h1>

<div class="AuthorDiv"></div>

Is this worthwhile?

Question and answer

Yes—I do not have access to the code-behind of Microsoft.com (take a look at its HTML), but they could probably add a simple loop when rendering the output cache (they are probably output caching). On my very small informational site, I removed about 200 bytes from each page.

Note: This article is outdated.

Summary

We saw how you can eliminate the ID attributes from ASP.NET website pages. IDs in your ASP.NET pages bloat the pages. On my site, 200 bytes were saved for every request, which amounts to more than 30 MB a month. You can use a recursive method on Controls to null the IDs, but it is likely much more efficient to null them individually.

ASP.NET Tutorials
.NET