Friday, 18 October 2013

ASP.NET - Disabling Back Button of Browser in C#/VB

Hi friends, in this article I will explain about ASP.NET - Disabling Back Button of Browser in C#/VB.NET
I already explained in the previous articles about ASP.NET-Disabling Back Button of Browser in JavascriptMVC4 Razor :Routing or Set StartUp page in MVC4 with example and MVC4 Razor: How to use sessions using C#

In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >ASP.NET - Disabling Back Button of Browser in C#/VB</title>
</head>
 <body>
<form  runat ="server">
   User Name: <asp:TextBox runat ="server" ID="txtUser"></asp:TextBox> 
 <asp:Button runat ="server" ID="Button1" Text="Go" onclick="Button1_Click" />
    </form>
</body>
</html>



In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Sample : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        string pURL = "NoBack.aspx";
        sb.Append("<FORM action='" + pURL + "' method=post name=frmResult>");
        sb.Append("<INPUT name='Username' type=hidden value='" + txtUser.Text + "'>");
        sb.Append("<script>frmResult.submit();</script>");
        Response.Write(sb.ToString());
    }   
}


In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Public Class Sample
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim sb As New System.Text.StringBuilder()
        Dim pURL As String = "NoBack.aspx"
        sb.Append("<FORM action='" & pURL & "' method=post name=frmResult>")
        sb.Append("<INPUT name='Username' type=hidden value='" + txtUser.Text & "'>")
        sb.Append("<script>frmResult.submit();</script>")
        Response.Write(sb.ToString())
    End Sub
End Class


NoBack.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >ASP.NET - Disabling Back Button of Browser in C#/VB</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>Browser Back button Disabled</p>
    </div>
    </form>
</body>
</html>

Noback.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NoBack : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string str=Request .Form ["Username"].ToString ();
        Response.Write (str);
    }
}


Noback.aspx.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class NoBack
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim str As String = Request.Form("Username").ToString()
        Response.Write(str)
    End Sub
End Class


The output of the above code as shown in the below figure.
When we click on Back button in the browser then it shows as shown in the below figure.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.