Nov 21, 2010

Cross Page Posting in ASP.Net

In this post, we will discuss about Cross Page Posting in ASP.Net and how it works.

In ASP.Net Web Forms page, when you click a button or any event which result in post-back posts the webpage and it's control values to the same page by default. Under some circumstances you might want to post one page to another page, In that case you can configure control of a page to post on some other page. This is referred as Cross-Page Posting.

Cross-Page Posting is generally implemented when you need to access some information from the Source Page i.e Previous page in the Target Page i.e Next Page

Let's take a small example of how to implement Cross Page Posting. In the example, we will configure the button of Source Page to post to some other page and will access the value of Textbox of Source Page on Target Page.

PreviousPage.aspx code
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="_textBox" runat="server"></asp:TextBox>
        <asp:Button ID="btnPost" runat="server" Text="Post" PostBackUrl="~/NextPage.aspx"
            OnClick="btnPost_Click" />
    </div>
    </form>
</body>
</html>

In Previous Page, we have set PostBackUrl property of button to NextPage.aspx. This will result in post-back to NextPage.aspx instead of self.

NextPage.aspx code
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="_textBox" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

NextPage.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
    _textBox.Text = ((TextBox)PreviousPage.FindControl("_textBox")).Text;
}

In Next Page, we are accessing the value of Previous Page text-box using FindControl Method of PreviousPage Property which is under Page Class.

    Choose :
  • OR
  • To comment
No comments:
Write comments