Redirect to previous page on ASP.Net MasterPage Button Click using C# and VB.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

hello,

i want to apply this back button solution in master page.

Redirect to previous page on Button Click using C# and VB.Net in ASP.Net

i notice on master page it does not have previouspage property? please advice i want to apply back button on master page so it appearts on all pages

Posted 2 months ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

Master Page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="LinkButton2" runat="server" Enabled="false">LinkButton</asp:LinkButton>
        <hr />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

CS Page

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Button ID="btnResponseRedirect" Text="Response Redirect" runat="server" />
    <asp:Button ID="btnServerTransfer" Text="Server Transfer" runat="server" OnClick="ServerTransfer" />
</asp:Content>

Default Page

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    Default Page
</asp:Content>

Code

C#

Master Page

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Page.PreviousPage != null)
    {
        LinkButton2.Attributes.Add("onClick", "javascript:history.back(); return false;");
        LinkButton2.Enabled = true;
    }
}

CS Page

protected void Page_Load(object sender, EventArgs e)
{
    btnResponseRedirect.PostBackUrl = "Default.aspx";
}

protected void ServerTransfer(object sender, EventArgs e)
{
    Server.Transfer("Default.aspx");
}

VB.Net

Master Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Me.Page.PreviousPage IsNot Nothing Then
        LinkButton2.Attributes.Add("onClick", "javascript:history.back(); return false;")
        LinkButton2.Enabled = True
    End If
End Sub

CS Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    btnResponseRedirect.PostBackUrl = "Default.aspx"
End Sub

Protected Sub ServerTransfer(ByVal sender As Object, ByVal e As EventArgs)
    Server.Transfer("Default.aspx")
End Sub

Screenshot