Reducing the size of HTML by removing the ID of control when not required

Hi,

Many a time we have many server controls with long Id which are never required once there value has been set. We normally disable their viewstate to reduce the size of both viewstate and the page. Especially if these controls are inside nested repeaters (or grid view or likes) the view state size can be huge.

Making the viewstate false reduces the size of the viewstate but when these controls are rendered they render with a long ID. If we are using a master page than the ID of such a label (or other controls) would be something like Ctl100_ + Contentplaceholderid + Repeater Name + ct102 + the label of control. If the repeater is nested than the name of ID can be very very large.

A very simple solution to this can be to remove the Id of these controls. We can have a very simple method which runs on the pre render of these controls and remove the id value of the controls. All we need to do is set the Id value to null. So now when these controls are rendered they do not have and Long ID. For a big page this can save lots of bytes across the network and hence make the page much faster.

protected void DeleteTheIDofControl(object sender, System.EventArgs e)

{

    if (sender is Label) {

        Label L1 = sender;

        L1.ID = null;

    }

}

The function can be called in the label like this.

<asp:Label ID="MyLabel" runat="server" OnPreRender="DeleteTheIDofControl”></asp:Label>

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 12/7/2007 6:28:57 PM

Interesting one Vikram. Will give it a try and see how it works! Thanks.

Posted on 12/22/2007 11:25:38 AM

This seems to be good..I am trying to reduce size by removing View State at Page level..It helped me too..but will try out this solutions by you.

Can you tell me whats all about that WebResource.axd file? which keeps on adding some size on overall application?

Also I would like to know..how can we use foreach loop in pages which are under Master Pages. (e.g If someone need to clear 30 Textboxes in one attempt. like we can do on Windows apps)?

Thanks
Vikram.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia