Hi there! Seems you are visiting my site for the first time. Hmm, Why dont you subscribe to RSS or use email subscription service to get all posts at your inbox (No spam). You can also follow me as google friend.
Please carry on reading my blogs, if you like, please give your feedback too. Have a wonderful day! (Dismiss)

Monday, September 7, 2009

Comparision between Response.Redirect, Response.RedirectParmanent and Server.Transfer

It is to be noted, .NET has lately introduced Response.RedirectParmanent() after a long time. The main motive is to have permanent response redirection to the Search Engines.

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.

If you try to write Response.RedirectParmanent() the code will look like :

public static class Extensions
{
public static void RedirectPermanent(this HttpResponse Response, string absoluteUri)
{
Response.Clear();
Response.Status = "301";
Response.RedirectLocation = absoluteUri;
Response.End();
}
}

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.

Thanks for reading and hope you like the discussion.

Monday, August 31, 2009

New Features of C# 3.0

1. Extension Methods : You can define extension methods to your namespace, so that if this is added, the class in which it is applied will act as instance variable. Example :


pubic void isDateTime(this string x)
{
return DateTime.TryParse(x);
}

The function will be added to the string object, and you can use it normally as with instance methods of String class.

2. Annonymous Types : You can create unnamed objects that is not derived from a class, rather it is created on a fly. Annonymous types comes very handy when working with LINQ.
Example :

var x = new {x=20,y=40};


thus x will have 2 properties x and y.

3. Initialisers : While you create object of a class, ,we now dont need to call its constructors as all the public properties can be initialised easily.
MyClass x = new MyClass {
MyProperty1 = 20,
MyProperty2 ="This is new Property "};

thus we can set properties directly without calling the respective constructors. This feature also comes very handy when we dont want to have lots of constructors overloads or just say we have to create 2 constructors with same set of parameters but which will be assigned to 2 different properties . Accessing properties directly is very handy feature lately.

Also there is collection initialisers like
List mycol = {"ss","gg"};
You can do this without calling add method.


4. Implicitely Typed Variables : C# introduces "var" which means when the object is assigned the type will be determined automatically. if you assing a string to x it will be of string type, or whatever you do.

5. Partial Methods : .NET 3.5 introduces partial methods, where the same methods can be declared more than once so that when called it will automatically add features to the existing method that is already defined.
Note : Partial methods must not return anything, it should be defined as Void.

6. Property implementation is not required : Sometimes we just need to define properties to expose objects. In such case we dont need to implement a property, rather we can go with auto implementation feature introduced.

public string myproperty {get;set;};

The property will implicitly create a private variable and assign the values properly.

7. LINQ and Lambda Expression : You can query .net objects now using Linq. Lambda expressions are short - hand representation of LINQ queries. :)

This is just basics of what introduced in C# 3.0. I will discuss more on each of them when I find time.

Hope you like reading this.

Sunday, May 10, 2009

Javascript pure HTML Dialog Box

It is a very common scenario to make a dimmed effect in web when a javascript popup is opened. Lots of people wonder how those being made. Some people tile with a transparent png to the background while others use div with certain opacity to fill up the entire space. I choice is with divs as they are purely htmls, and you can easily create div dynamically and also remove them from dom.


To do this, First we need a semi – transparent div to cover the entire work area :

var width = document.documentElement.offsetWidth + document.documentElement.scrollLeft;

var height = document.documentElement.offsetHeight + document.documentElement.scrollHeight;

var centerX,centerY;

if(document.documentElement && document.documentElement.clientHeight) // NON IE

{

centerX = document.documentElement.offsetWidth / 2;

centerY = document.documentElement.offsetHeight / 2;

}

else if( document.body ) //IE

{

centerX = document.body.clientWidth / 2;

centerY = document.body.clientHeight / 2;

}

var layer = document.createElement('div');

layer.style.zIndex = 2;

layer.id = 'backgroundlayer';

layer.style.position = 'absolute'; // Required to place layer in background

layer.style.top = '0px';

layer.style.left = '0px';

layer.style.height = document.documentElement.scrollHeight + 'px';

layer.style.width = width + 'px';

layer.style.backgroundColor = 'black'; // Ideal for dim effect

layer.style.opacity = '.6'; // Works only in Non IE browsers

layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=60)"); // IE little fix

document.body.appendChild(layer); // Append to DOM

document.body.style.overflow="hidden"; //To Hide extra scrollbar when dialog is visible.

Explanation :

In the above code we have created a div, set certain properties and append that to body. We need to have DXImageTransform to make opacity of a div in IE as style.opacity cannot work in IE. DX is directX transform, so if you don’t need your application to run over IE, you can remove this line, Otherwise this line will be ignored in Non-IE browsers. documentElement.scrollHeight gives you the actually height of the browser working area adding up the scrollPosition. Background of Div goes perfect with black, but you can change it to any other color according to your choice.

Another little fix for IE is use of document.body to find the center screen position which shows the popup.

Next we add a div in the middle of the page on top of the semi-transparent layer.

var div = document.createElement('div');

div.style.zIndex = 3; // It should be more than background DIV

div.id = 'popupbox';

div.style.position = (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';

div.style.top = '200px';

div.style.left = (width / 2) - 200+ 'px'; // Half its width

div.style.height = '50px';

div.style.width = '400px';

div.style.backgroundColor = 'white';

div.style.border = '2px solid silver';

div.style.padding = '20px';

document.body.appendChild(div);

Explanation :

This is the actual dialog box which is placed over the semi – transparent div. For IE we need to place its position to absolute, otherwise to fixed. (IE always bugs me with non-standard properties, Oh god). Left position is set to its width/2 – 200 as actual width is 200. We also created a solid silver border to the popup.

And finally we put some text and a link that closes the popup when clicked:

var p = document.createElement('p');

p.innerHTML = 'Some text';

div.appendChild(p);

// Create a close link of popup

var a = document.createElement('a');

a.innerHTML = 'Close window';

a.href = 'javascript:void(0)';

a.onclick = function()

{

document.body.removeChild(document.getElementById(backgroundlayer));

document.body.removeChild(document.getElementById('popupbox'));

document.body.style.overflow="auto";

};

So its done, you can try out the live demo if you need as well. Online Demo