How To Remoe ID of controls to reduce HTML Size

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 as Label;

L1.ID = null;

}

}

The function can be called in the label like this.

Reactions

Post a Comment

0 Comments

Close Menu