CrossPage PostBack in series of web pages

Posted by Vishnu Reddy on Stack Overflow See other posts from Stack Overflow or by Vishnu Reddy
Published on 2010-03-18T05:30:44Z Indexed on 2010/03/23 21:13 UTC
Read the original article Hit count: 391

I had a requirement to pass data between pages in my application.

I have a Page A where user will input some data and on submit User will be redirected to Page B where again user will enter some more data and on submitting user will be show a confirmation in Page C after doing some calculations and data saving.

Following is the idea I was trying to use:

PageA.aspx:

<form id="frmPageA" runat="server">  
        <p>Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox></p>  
        <p>Age: <asp:TextBox ID="txtAge" runat="server"></asp:TextBox></p>  
        <p><asp:Button ID="btnPostToPageB" runat="server" Text="Post To PageB" PostBackUrl="~/PageB.aspx" /></p>  
    </form>

In Page A Codebehind file I am creating following public properties of the inputs to access in Page B:

  public string Name { get { return txtName.Text.ToString(); } }  
  public int Age { get { return Convert.ToInt32(txtAge.Text); } }

In PageB.aspx:

using previouspage directive to access page A public properties

<%@ PreviousPageType VirtualPath="~/PageA.aspx" %>

<form id="frmPageB" runat="server">  
        <asp:HiddenField ID="hfName" runat="server" />  
        <asp:HiddenField ID="hfAge" runat="server" />  
        <p><asp:RadioButtonList ID="rblGender" runat="server" TextAlign="Right" RepeatDirection="Horizontal">  
                <asp:ListItem Value="Female">Female</asp:ListItem>  
                <asp:ListItem Value="Male">Male</asp:ListItem>  
            </asp:RadioButtonList></p>  
            <p><asp:Button ID="btnPostToPageC" runat="server" Text="Post To PageC" PostBackUrl="~/PageC.aspx" /></p>  
        </form>

In Page B Codebehind file I am creating following public properties for the inputs to access in Page C:

public RadioButtonList  Gender   
{ get { return rblGender; } }  

public string Name { get { return _name; } }  

public int Age { get { return _age; } }  

//checking if data is posted from Page A otherwise redirecting User to Page A  

protected void Page_Load(object sender, EventArgs e)  
    {  
        if (PreviousPage != null && PreviousPage.IsCrossPagePostBack && PreviousPage.IsValid)  
        {  
            hfName.Value = PreviousPage.Name;  
            hfAge.Value = PreviousPage.Age.ToString();  
        }  
        else  
            Response.Redirect("PageA.aspx");  
    }`

in PageC.aspx:

using previouspage directive to access page A public properties

<%@ PreviousPageType VirtualPath="~/PageB.aspx" %>

<form id="frmPageC" runat="server">  
        <p>Name: <asp:Label ID="lblName" runat="server"></asp:Label></p>  
        <p>Age: <asp:Label ID="lblAge" runat="server"></asp:Label></p>  
        <p>Gender: <asp:Label ID="lblGender" runat="server"></asp:Label></p>  
         <p><asp:Button ID="btnBack" runat="server" Text="Back" PostBackUrl="~/PageA.aspx" /></p>  
</form>

Page C code behind file:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack && PreviousPage.IsValid)  
            {  
                lblName.Text = PreviousPage.Name.ToString();  
                lblAge.Text = PreviousPage.Age.ToString();  
                lblGender.Text = PreviousPage.Gender.SelectedValue.ToString();  
            }  
            else 
                Response.Redirect("PageA.aspx");

© Stack Overflow or respective owner

Related posts about cross-page-posting

Related posts about postback