Friday, October 17, 2008

Disable back button of Browser in asp.net

This function use for disabling back button of browser.
If you want prevent to go back from browser so use follwing method instead of
response.redirect();

public string Disable_Back_Button(string nextpage_url)
{
try
{

string strScript;
strScript="<script language="javascript">";
strScript=strScript+"{";
strScript=strScript+" var Backlen=history.length;";
strScript=strScript+" history.go(-Backlen);";
strScript=strScript+" window.location.href='" + nextpage_url + "'; ";
strScript=strScript+"}";
strScript=strScript+"</script>";
return strScript;
}
catch(Exception ex)
{
throw ex;
}
}


Example for use:
Response.Write(Disable_Back_Button("nextpage.aspx"));
instead of response.redirect("nextpage.aspx");