Crosspage posting and master pages with simple code

Posted: December 10, 2005 in Uncategorized
One of the email i got after the crosspage posting is :-
I was asked  how can i use crosspage page posting with masterpages and get the values of the previous page.
So I thought let me give 2 simple scenario of getting the same back first with non master pages and with master pages.
Here you go:-
 
here is the code ith simple page without master:-

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<head runat=”server”>

<title>Untitled Page</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>

</div>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” PostBackUrl=”~/Default2.aspx” />

</form>

</body>

 

and here is the Default2 code in pageload:-

 

protected

void Page_Load(object sender, EventArgs e)

{

if (IsCrossPagePostBack)

{

TextBox1.Text = ((TextBox)PreviousPage.FindControl(“TextBox1”)).Text;

}

}

 

In MasterPage Scenario:-

<%@ Page Language=”C#” AutoEventWireup=”true” MasterPageFile=”~/MasterPage.master” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” PostBackUrl=”~/Default2.aspx” style=”left: -296px; position: relative; top: 81px” />

</asp:Content>

Here is the default2  load :-in default2.cs

 

protected

void Page_Load(object sender, EventArgs e)

{

if (IsCrossPagePostBack)

{

TextBox1.Text = ((TextBox)(((ContentPlaceHolder)PreviousPage.Master.FindControl(“ContentPlaceHolder1”)).FindControl(“TextBox1”))).Text;

}

 

 

}

Note :- I have not provided the code for default2.aspx

For details checkout here in msdn2:-

http://msdn2.microsoft.com/library/ms178139(en-us,vs.80).aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s