To redirect to a different page in an ASP.NET web application, you can use the `Response.Redirect` method. Here’s how you can do it:
Code-Behind (C#)
In your code-behind file (e.g., `.aspx.cs`), you can write the following code to redirect to `ManageUser.aspx`:
```csharp
string pageName = "ManageUser.aspx";
Response.Redirect(pageName);
```
Example Scenario
Let's say you have a button on your ASPX page, and when this button is clicked, you want to redirect to `ManageUser.aspx`. Here’s how you can achieve this:
ASPX Page
```html
<%@ 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>Redirect Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnRedirect" runat="server" Text="Go to Manage User" OnClick="btnRedirect_Click" />
</div>
</form>
</body>
</html>
```
Code-Behind (ASPX.CS)
```csharp
using System;
using System.Web.UI;
namespace YourNamespace
{
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
string pageName = "ManageUser.aspx";
Response.Redirect(pageName);
}
}
}
```
Explanation
1. ASPX Page:
- Contains a button (`btnRedirect`) with an `OnClick` event handler (`btnRedirect_Click`).
2. Code-Behind:
- In the `btnRedirect_Click` method, `Response.Redirect` is used to navigate to `ManageUser.aspx`.
Additional Tips
- Passing Query Parameters: If you need to pass query parameters to the target page, you can modify the `pageName` string:
```csharp
string pageName = "ManageUser.aspx?userId=123";
Response.Redirect(pageName);
```
- Avoiding Browser Cache: If you want to ensure that the browser doesn't cache the response, you can use the following overload:
```csharp
Response.Redirect(pageName, false);
```
This will allow the current page to complete processing before the redirect occurs.
By following these steps, you can easily redirect to another page in your ASP.NET web application. If you like comment and share. 🚀
0 Comments