0

Question: How Come the Button Click on the aspx page that was transferred to does not fire correctly, after the first aspx page transferred focus to it using the Server.Transfer() method in C#.

The Answer: When the button is clicked in the transferred page after the Server.Transfer, it runs the page_load() in the transferred page instead of the button_click(). If the button is clicked a second time in the transferred page, then the button_click() fires.

Sniff
  • 11
  • 3

1 Answers1

0
---- Main.aspx ----
<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="Main.aspx.cs" 
Inherits="Server_Transfer_App.Main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" 
  Text="Enter Your First Name: "></asp:Label>
            <asp:TextBox ID="TextBox1" 
 runat="server"></asp:TextBox>
        </div>
        <p>
            <asp:Button runat="server" Text="Button" 
    OnClick="Button1_Click" />
        </p>
    </form>
</body>
</html>
-----------Main.aspx.cs-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Server_Transfer_App
{
    public partial class Main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, 
    EventArgs e)
        {

        }

        protected void Button1_Click(object sender, 
        EventArgs e)
        {
            Server.Transfer("~/WebForm1.aspx", 
        true);
            //Response.Redirect("~/WebForm1.aspx");
        }
    }
}
----------- WebForm1.aspx -------
<%@ Page Language="C#" AutoEventWireup="true" 
  CodeBehind="WebForm1.aspx.cs" 
 Inherits="Server_Transfer_App.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" 
  Text="Label"></asp:Label>
            </div>
        <div>
            &nbsp;</div>
        <div>
            <%--<input id="Button1" type="button" value="button 1" runat="server" onserverclick="Button1_Click"/></div>--%>
            <asp:Button ID="Button1" runat="server" Text="Button 1" OnClick="Button1_Click1" />
            <div>
            -----------</div>
        <div>
            <asp:Button ID="Button2" runat="server" Text="Button 2" OnClick="Button2_Click" />
        </div>
    </form>
</body>
</html>
-------- WebForm1.aspx.cs ------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Server_Transfer_App
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Use when transfer to this page using Server.Transfer("~/WebForm1.aspx") from Main.aspx
            Page pPage = Page.PreviousPage;
            if (pPage != null)
            {
                Label1.Text = ((TextBox)pPage.FindControl("TextBox1")).Text;
            }
            else
            {
                //Label1.Text = "Got to here, now click Button 2 to go back.";
                Label1.Text = "Did Page Load () in WebForm1, and pPage is NULL.";
            }

            //Response.Redirect("~/WebForm1.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Main.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Click on Button2";
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Label1.Text = "Performed Button1_Click1().";
        }
    }
}
Sniff
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 18 '22 at 07:52