Wednesday, 31 October 2012

How to handle Browser back button using c#

How to handle Browser back button using Asp.net c# on

Step 1 : Put this code on page ,where you don't back to come back by using back button...


 protected override void OnPreRender(EventArgs e)
    {
        ViewState["PreviousPage"] = Request.UrlReferrer;
        base.OnPreRender(e);
        string strDisAbleBackButton;
        strDisAbleBackButton = "<script language='javascript'>\n";
        strDisAbleBackButton += "window.history.forward(-1);\n";
        //   strDisAbleBackButton += "alert('" + ViewState["PreviousPage"].ToString()+ "');\n";
        strDisAbleBackButton += "\n</script>";
        ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
        // Response.Redirect("~/Login.aspx");
    }


Example :-

Your first page is FirstPage.aspx   -----Note: - Put code in this page
Your Second Page is : - SecondPage.aspx

and when you redirect from firstpage.aspx to secondpage.aspx page and press back button that time the Page FirstPage.aspx will not allow the redirect ....

No comments:

Post a Comment

Thank you for your interest .