Thursday, September 15, 2011

How to disable Client Side and Proxy Caching of ASP.NET page?

In page Load
   Response.Cache.SetCacheability(HttpCacheability.NoCache);











How to prevent a Button from Validating Form on ASP.NET page?

ASP.NET Button controls have property CausesValidation.



When its set to "False" and button is clicked on ASP.NET page, validation is not triggered.



How to add Client-Side confirmation when deleting items in GridView?

In asp:TemplateField......itemtemplate
Add link button with commandname="Delete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this data Row?')"

How To Prevent Form Submission When User Presses the ENTER Key in TextBox?

protected void Page_Init(object sender, EventArgs e)
{
    TextBox1.Attributes.Add("onkeydown","if(event.which event.keyCode){if (event.keyCode == 13) return false;}");
}





How to Capitalize the First Letter of All Words in a string in C#

using System.Globalization;


TextInfo UsaTextInfo = new CultureInfo("en-US", false).TextInfo;

string capitalized = UsaTextInfo.ToTitleCase("asp.net simply rocks!!!");

What is the difference between Server.Transfer and Response.Redirect methods?

You can transfer current users page request to another page with two methods:

* Server.Transfer (HttpServerUtility.Transfer Method)
* Response.Redirect (HttpResponse.Redirect Method)

Its not always clear how these two approaches differ so let us try to clarify things a little:

Response.Redirect sends HTTP code 302 down to the users browser along with the new URL location of the wanted page.
HTTP Code 302 actually means ' The requested resource resides temporarily under a different URI'.
After browser receives this code it tries to open the new location of the resource that was suggested by the server.
This actually causes two requests to the server, first one to the original URL, and second to the new URL that is suggested via 302 response.
All the Query Strings and Form variables are lost during the redirect and they are not available to the redirected URL.

Also its important to say that the new URL can reside on the same server but also it can be on some other server and the redirected URL does not need to be .aspx page it can be regular HTML page also).

So we can us the Redirect method to redirect users request to another page on our server like this:

Response.Redirect("newPage.html");


or to redirect our it to some other server like this:

Response.Redirect("http://www.someotherserver.com/newPage.aspx");



In contrast to all this when we call Server.Transfer we do not initiate another request to the server, but the original request is simply rewritten and transfered to some other page on the same server.
(This off course means that we can use it only to transfer requests to the pages on the same server, not to some other servers and we can only transfer to .aspx pages and not other page types like HTML, php etc).

All posted Form variables and query strings can optionally remain available to the second Page where we transfered request (if we use second overload Server.Transfer(string path, bool preserveForm) and supply true for the second parameter).
Otherwise the Form Variables and Query String are cleared just like when we use Redirect.
WARNING: When You Use Server.Transfer and use this method
to preserve Query String and Form variables and receive error: "View State Is Invalid" its because your EnableViewStateMac attribute of the element is set to true.
Its also important to note that because of the way Server.Transfer works, after the transfer, the URL shown in the users Web Browser remains the original one that was requested, because browser has no knowledge that its request was transfered (transfer occurs on the server side).

TIP: One thing to be careful about when using the Server.Transfer is to clear the the HttpResponse object with Response.Clear method on the transfered page to avoid any output from the first page to be shown on the second page.

So now that we know what are the similarities and differences between these two approaches we can try to use them wisely.

Here is the summary:

Response.Redirect should be used when:
•we want to redirect the request to some plain HTML pages on our server or to some other web server
•we don't care about causing additional roundtrips to the server on each request
•we do not need to preserve Query String and Form Variables from the original request
•we want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to bookmark it if its necessary)
Server.Transfer should be used when:

•we want to transfer current page request to another .aspx page on the same server
•we want to preserve server resources and avoid the unnecessary roundtrips to the server
•we want to preserve Query String and Form Variables (optionally)
•we don't need to show the real URL where we redirected the request in the users Web Browser
You can transfer current users page request to another page with two methods:

* Server.Transfer (HttpServerUtility.Transfer Method)
* Response.Redirect (HttpResponse.Redirect Method)

Its not always clear how these two approaches differ so let us try to clarify things a little:

Response.Redirect sends HTTP code 302 down to the users browser along with the new URL location of the wanted page.
HTTP Code 302 actually means ' The requested resource resides temporarily under a different URI'.
After browser receives this code it tries to open the new location of the resource that was suggested by the server.
This actually causes two requests to the server, first one to the original URL, and second to the new URL that is suggested via 302 response.
All the Query Strings and Form variables are lost during the redirect and they are not available to the redirected URL.

Also its important to say that the new URL can reside on the same server but also it can be on some other server and the redirected URL does not need to be .aspx page it can be regular HTML page also).

So we can us the Redirect method to redirect users request to another page on our server like this:

Response.Redirect("newPage.html");


or to redirect our it to some other server like this:

Response.Redirect("http://www.someotherserver.com/newPage.aspx");



In contrast to all this when we call Server.Transfer we do not initiate another request to the server, but the original request is simply rewritten and transfered to some other page on the same server.
(This off course means that we can use it only to transfer requests to the pages on the same server, not to some other servers and we can only transfer to .aspx pages and not other page types like HTML, php etc).

All posted Form variables and query strings can optionally remain available to the second Page where we transfered request (if we use second overload Server.Transfer(string path, bool preserveForm) and supply true for the second parameter).
Otherwise the Form Variables and Query String are cleared just like when we use Redirect.
WARNING: If you use this method to preserve Query String and Form variables and receive error: "View State Is Invalid" its because your EnableViewStateMac attribute of the element is set to true.

Its also important to note that because of the way Server.Transfer works, after the transfer, the URL shown in the users Web Browser remains the original one that was requested, because browser has no knowledge that its request was transfered (transfer occurs on the server side).

TIP: One thing to be careful about when using the Server.Transfer is to clear the the HttpResponse object with Response.Clear method on the transfered page to avoid any output from the first page to be shown on the second page.

So now that we know what are the similarities and differences between these two approaches we can try to use them wisely.

Here is the summary:

Response.Redirect should be used when:
•we want to redirect the request to some plain HTML pages on our server or to some other web server
•we don't care about causing additional roundtrips to the server on each request
•we do not need to preserve Query String and Form Variables from the original request
•we want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to bookmark it if its necessary)
Server.Transfer should be used when:

•we want to transfer current page request to another .aspx page on the same server
•we want to preserve server resources and avoid the unnecessary roundtrips to the server
•we want to preserve Query String and Form Variables (optionally)
•we don't need to show the real URL where we redirected the request in the users Web Browser