To implement jumping back to the previous page after logging in in C# WebForm, you can use Session or URL parameters to save the user's previous page information and restore it after logging in. The following two methods are available:
Table of contents
1. Use Session:
Before the user selects a page, save the selected page information to the Session. After logging in, obtain the selected page information from the Session and jump based on this information.
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { // Save the selected header information to Session Session["selected_header"] = "Header1"; // Assume "Header1" is selected here } }
protected void btnLogin_Click(object sender, EventArgs e)
{ // Login logic // ...
// Jump back to the previous page after successful login
string selectedHeader = Session["selected_header"] as string;
if (!string.IsNullOrEmpty(selectedHeader))
{ Response.Redirect(selectedHeader); } else { // If the selection is not saved header information, jump to the default page Response.Redirect("Default.aspx"); } }
2. Use URL parameters:
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { // Save the selected header information to the URL parameter string selectedHeader = "Header1"; // Assume that "Header1" is selected here Response.Redirect("Login. aspx?selectedHeader=" + selectedHeader); } }
protected void btnLogin_Click(object sender, EventArgs e)
{ // Login logic // ...
// After successful login, get the URL parameters and jump back to the previous page
string selectedHeader = Request.QueryString["selectedHeader"];
if (!string.IsNullOrEmpty(selectedHeader))
{ Response.Redirect(selectedHeader); } else { // If the selected header information is not saved, jump to the default page Response.Redirect("Default.aspx"); } }