Asp.Net Button example to redirect to another page

Learn how to make a button in ASP.NET that sends you to another page! Easy steps with examples included.

ASPX file (Default.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <!-- Button to trigger the redirection -->
            <asp:Button ID="RedirectButton" runat="server" Text="Redirect" OnClick="RedirectButton_Click" />
        </div>
    </form>
</body>
</html>

Code-behind file (Default.aspx.cs):

using System;
using System.Web.UI;

namespace YourNamespace
{
    public partial class Default : Page
    {
        protected void RedirectButton_Click(object sender, EventArgs e)
        {
            // Redirect to another page
            Response.Redirect("AnotherPage.aspx");
            //or
            Server.Transfer("AnotherPage.aspx");
        }
    }
}

In this example:

  • Default.aspx contains an ASP.NET button (RedirectButton) that will trigger a server-side event when clicked.
  • In the code-behind file (Default.aspx.cs), the RedirectButton_Click method handles the button click event.
  • Inside the RedirectButton_Click method, Response.Redirect is used to redirect the user to another page (in this case, "AnotherPage.aspx").

Make sure to replace "YourNamespace" with the appropriate namespace for your application, and "AnotherPage.aspx" with the actual URL of the page you want to redirect to.

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice