Sunday, July 27, 2008

Performance Tips and Tricks in .NET Applications

One of the toughtest and important thing involved in ASP.NET web application development is improving the performance. So here are few trips which will help you to improve the performance of your web application. Following points needs to be kept in mind for ASp.net web applications
  1. Throw Fewer Exceptions
  2. Design with ValueTypes
  3. Trim Your Working Set
  4. Use StringBuilder for Complex String Manipulation
  5. Keep IO Buffer Size Between 4KB and 8KB
  6. Cache Aggressively
  7. Use Session State Only If You Need To
  8. Use View State Only If You Need To
  9. Remove Unnecessary Http Modules
  10. Avoid the Autoeventwireup Feature
  11. Turn On Option Strict and Explicit
  12. Remove unwanted namespaces imported

Tuesday, July 22, 2008

Blocking backspace key using javascript

Backspace key can be blocked by trapping the keycode using javascript. Following javascript code can be used to disable back and forward navigation with backspace and arrow keys in website. This function will work for IE6, IE7, FireFox, Opera and Safari browsers.

//backEnabled variabled is used to enable backspace key for textbox,
//if set to 1 backspace key will work, if 0 backspace key will not work(o will be while navigation)var backEnabled = 0;
//Function to prevent navigating to previous page on click of backspace
function checkKeyPress(e)
{
var Firefox = false; //detects mozilla browser
if(navigator.userAgent.indexOf("Firefox")!=-1)
{
var versionindex=navigator.userAgent.indexOf("Firefox")+8
if (parseInt(navigator.userAgent.charAt(versionindex))>=1)
Firefox = true;} e = e window.event //alert(e.keyCode);
if(e.keyCode == 9)//9 represents tab key
backEnabled = 0; //disbale enter key for Check financing condition textbox value change
if((e.keyCode == 13) && backEnabled==1) //13 for entery key
{
if(!Firefox)
{ e.returnValue =false; }
else{
if(e.cancelable)
e.preventDefault();
}
} //disable back navigation using backspace key
if ((e.keyCode == 8) && backEnabled == 0) //for backspace key (8 keycode)
{
if(!Firefox)
{ e.returnValue =false; }
else{
if(e.cancelable)
e.preventDefault();
}
}
else
{
DisableBackForward(e, Firefox);
}}

var altDown=0;

function DisableBackForward(e, Firefox)
{
if(e.keyCode == 18)altDown=1; //18 is for alt key
if(navigator.userAgent.indexOf("Opera")!=-1) //check for opera browser
{
if(e.keyCode == 16)altDown=1; //16 is for shift key
if((e.keyCode == 37 e.keyCode == 39 ))
{ e.returnValue =false; }
else if(altDown==1)
{
if(e.keyCode == 90 e.keyCode == 88) //90 for z key to go back, 88 for x key to go forward
{
e.returnValue =false;
}
}
else if(e.keyCode == 122 e.keyCode == 120) //122 for z key to go back, 120 for x key to go forward
{ e.returnValue =false;
}
}
else
{ //for IE, Mozilla browser
if((e.keyCode == 37 e.keyCode == 39) && altDown==1) //37 gor back arrow key, 39 for forward arrow key
{
if(!Firefox)
{ e.returnValue =false; }
else{
if(e.cancelable)
{ e.preventDefault(); }
} }
}}

function EnableBack()
{ backEnabled = 1;}

function DisableBack()
{ backEnabled = 0;}

if(navigator.userAgent.indexOf("Opera")!=-1)
{
document.onkeypress = checkKeyPress;
}
else
{
document.onkeydown = checkKeyPress;
}