Thursday, August 5, 2010

ClientIDMode Property for generated ClientID in ASP.NET 4.0

ClientIDMode Property for generated ClientID

Every asp.net control is rendered as an HTML element and a corresponding ClientId is generated for it.If we want to reference the generated HTML element in javascript we must know the Id attribute of the generated element. The ID attribute in HTML that is rendered for Web server controls is generated based on the ClientID property of the control.

Until ASP.NET 4, we do not have much control over the generated Id of the Control. The new ClientIDMode property lets you specify more precisely how the client ID is generated for controls.

Possible settings for ClientIDMode are following:

* AutoID - Generated ClientID is similar to earlier versions of ASP.NET.Difficult to predict generated Id values.
* Static - This specifies that the ClientID value will be the same as the ID. For example if our label control has id HeaderLabel then the ClientId will also have value of HeaderLabel
* Predictable - This setting is used mostly in controls that are inside Data Bound Controls. The ClinetId value is generated by concatenating the ClinetId value of the parent naming container with the ID value of the control. This setting works in conjunction with the ClientIDRowSuffix property

If specified the value of the data field specified in the ClientIDRowSuffix property is added at the end.

For instance if we have StudentName Label inside ListView1 Student Name labels ClinetIDMode is set to Predictable and ClientIDRowSuffix is set to "ID" then StudentName labels' generated client id's will be as :

ListView1_ StudentName _1
ListView1_ StudentName _2
ListView1_ StudentName _3
ListView1_ StudentName _4

* Inherit - The control inherits the ClientIDMode setting of its parent control. This is the default value for control

Permanent Redirect in Asp.net 4.0: Response.RedirectPermanent

Response.RedirectParmanent is an extension function introduced in .NET 4.0.
The main motive of it is to indicate the Response Code to the Search Engine that the page is moved permanently. The Response.Redirect generates Response code as 302 whereas RedirectParmanent returns 301.

Thus say you have a page, and which is included to search engine for a long time, if you use Response.Redirect() it will not change this effect to the search engine(taking this a temporary change), while if you use Response.RedirectParmanent() it will take it as permanent.

In case of Server.Transfer() the actual response is actually been updated. There is no effect to the search engine, and search engine will think the output is coming from the same page that is called upon. Let us give an example :

Say you have 2 pages (Page 1 and Page 2) where Page1 redirects to Page2
In case of

1. Response.Redirect() : Search Engine will take this redirection as Temporary(Status 301) and always keep Page1 in its cache.
2. Response.RedirectParmanent() : Search Engine will take this a permanent redirection(Status 302) and will remove Page1 from its database and include Page2 for better performance on search.
3. Server.Transfer() : Search Engine will be unaware of any redirection been took place (Status 200) and will keep Page1 to its database. It will think Page1 is producing the output response of Page2.

When to use:
Response.Redirect is perfect when your page is temporarily changed and will be changed to original within a short span of time.
Response.RedirectParmanent() when you are thinking of deleting the Page1 totally after the search engines changes its cache.
Server.Transfer() when you are thinking of keeping the page for ever, and to let search engine unaware of this redirection.

Response.RedirectPermanent do the same thing as following code does.

Response.Status = "301 moved permanently";
Response.AddHeader("location", newPath);
Response.End();