ASP.NET Remove ID From Control

This C# tutorial shows how to remove ID attributes in ASP.NET.

ID attributes are generated by ASP.NET.

These waste bandwidth. On many sites, there are tags that have an ID on them. Here we remove these long ASP.NET ID attributes from the output.



Example. 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. The ID is useful in some PostBack scenarios, but it is not useful for


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.

    /// <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. 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.)


And: 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 show the rendered HTML content. 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>


Discussion. I do not have access to the code-behind of, but they could probably add a simple loop when rendering the output cache (they are probably output caching). On my site, I removed about 200 bytes from each page.


Note: This article is outdated. Web sites constantly change. It was once accurate.


Summary. We eliminated 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.


Also: You can use a recursive method on Controls to null the IDs, but it is likely much more efficient to individually null them.