Search code examples
asp.netwebformscross-page-postingcross-page-postback

One aspx page, two cross-page postbacks


Scenario

I have a site where users can register for a course, I'm developing a new feature allowing existing students to login with an email & password and register using their previous details.

Architecture

Register.aspx contains an email and password textbox, a login button and a forgot password button. Completing the email and password and clicking the login button results in a cross-page postback to the login.aspx page. Clicking the forgot password button also results in a cross-page postback to the login.aspx page.

Knowing that PreviousPage will be an instance of the code behind class for the Register.aspx page how can I distinguish between the two events (Login/Forgot Password) in the Page_Load for the Login.aspx page?


Solution

  • Why not handle the button logic in the code behind then use transfer? That would be easiest as you would have two methods:

    protected Login_Click(...)
    

    and

    protected ForgotPass_Click(...)
    

    When you're using the cross-page posting, you lose some of the context. You could add that back, however, in the control definition e.g.

    <asp:Button runat="server" Text="Login" PostBackUrl="~/login.aspx?cmd=login" />
    <asp:Button runat="server" Text="Forgot Password" PostBackUrl="~/login.aspx?cmd=forgot" />
    

    Which you can then read like this:

    if(Request.Querystring["cmd"]=="forgot")
        ...