web 2.0

Get Previous Page Control Values after Cross-Page Posting

Hi,

A little trick in order to get Previous Page Control Values after a Cross-Page Posting.

Take a first Sample Page named Default.aspx containing a TextBox and a Button.

We set the PostBackUrl of the Button to our Second Page : Page2.aspx

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Page2.aspx" />
</div>
</form>

Now on the Page2.aspx OnLoad Event we do the following code. The Trick resides in the PreviousPage property of the Page property.
So we can do a FindControl<> with the name of the TextBox from the Default.aspx page.

protected void Page_Load(object sender, EventArgs e)
{
    String s = String.Empty;
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
        TextBox txt = PreviousPage.FindControl<TextBox>("TextBox1");
        Response.Write("CrossPosting : " + txt.Text);
    }
}

Here is the Result :

image

 

image

Hope this help's!

Stay Tuned :)



Views(9579)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET

Comments

Technorati Profile