Tuesday, 18 December 2012

Cross Page Posting

Cross Page Posting



Response.Write(
((TextBox)this.PreviousPage.FindControl("MyTextBox")).Text)

In order to provide the strongly typed access to previous page, you can specify the previous page in PreviousPageType directive; since you have strongly typed reference of previous page you can now easily access its public members without any typecasting. Lets see how (here login page postbacks to UserAuthenticate page).

Login.aspx
<asp:Textbox ID="TextBoxUserName" Runat="server" />
<asp:Textbox ID="TextBoxPassword" Runat="server" />
<asp:Button ID="ButtonLogin" Runat="server" Text="Login"
         PostBackUrl="UserAuthenticate.aspx" />

Expose the following properties in code behind file
public TextBox UserName
{
    get
    {
        return TextBoxUserName;
    }
}

public TextBox Password
{
    get
    {
        return TextBoxPassword;
    }
}

UserAuthenticate.aspx
<%@ PreviousPageType VirtualPath="~/Login.aspx" %>
<script runat="server">   
protected void Page_Load(object sender, System.EventArgs e)
{
    String username = PreviousPage.UserName.Text;
    String password = PreviousPage.Password.Text;
    // Authenticate username/password
}

No comments:

Post a Comment