jump to navigation

Cross-Page postbacks in ASP.Net October 5, 2010

Posted by fofo in asp.net, C#, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010.
Tags:
trackback

In this post I would like to talk about cross-page postbacks in ASP.Net.

When you have a .aspx page with a button on it and you click that button the page will be posted back to the original page.

What we want to do now is to click that button and post back to an alternate page rather than the original one.

I am going to use Visual Studio 2010 Ultimate edition. You can use VS 2008/2005. All the express editions will suffice for our example.

I am going to use C# as the development language.

Let’s move on to our hands-on example.

1) Create an ASP.Net web site with an appropriate name in your file system.

2) In this first example we will see the typical scenario on how you postback back to the original page.

You should add some web server controls in the default.aspx page. Add 4 textboxes for ( Name,Age,Address,City ) and 4 labels and a button.

Basically, the markup should look like this

     Name:
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    Age:  <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    Address:
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <br />
    City:
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Submit" 
          onclick="Button1_Click" />

<br />

     <asp:Label ID="labName" runat="server" Text="Label"></asp:Label>
    <br />
     <asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="labAddress" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="labCity" runat="server" Text="Label"></asp:Label>
    <br />

3)  In the Page_Load event handling routine of the default.aspx page type.

 if (IsPostBack)
    {
      
      labName.Text = TextBox1.Text;
      lblAge.Text = TextBox4.Text;
      labAddress.Text = TextBox2.Text;
      labCity.Text = TextBox3.Text;
    }

4) Run you application and see all the data that we inputed in the textboxes appearing in the label controls.This is a typical postback within the same page.

5) Add another page in your site and name it SecondPage.aspx. Cut the label controls from the default.aspx and paste them in the SecondPage.aspx so we have,

    <asp:Label ID="labName" runat="server" Text="Label"></asp:Label>
    <br />
     <asp:Label ID="lblAge" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="labAddress" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="labCity" runat="server" Text="Label"></asp:Label>

6) Now we want to post back from the Default.aspx page to the SecondPage.aspx.

We go to the default.aspx page and we set the PostBackUrl property of the button to the SecondPage.aspx.

          <asp:Button ID="Button1" runat="server" Text="submit" 
          onclick="Button1_Click" PostBackUrl="~/SecondPage.aspx" />

7)  We go to the Page_Load event handling routine of the SecondPage.aspx and we type

labName.Text = TextBox1.Text;
lblAge.Text = TextBox4.Text;
labAddress.Text = TextBox2.Text;
labCity.Text = TextBox3.Text;

Straight away we see that we have error indicators that inform us that TextBox1,TextBox2,TextBox3,TextBox4 are dot declared.

We have to modify our code and try to reference the textbox controls from the SecondPage.aspx .

So the new code in the Page_Load event is like this

if (this.PreviousPage.IsCrossPagePostBack)

{

TextBox text1 = this.PreviousPage.FindControl(“TextBox1”) as TextBox;
TextBox text2 = this.PreviousPage.FindControl(“TextBox2”) as TextBox;
TextBox text3 = this.PreviousPage.FindControl(“TextBox3”) as TextBox;
TextBox text4 = this.PreviousPage.FindControl(“TextBox4”) as TextBox;
labName.Text = text1.Text;
lblAge.Text = text4.Text;
labAddress.Text = text2.Text;
labCity.Text = text3.Text;
}

8) We do use FindControl() method to locate a reference to the controls on the source page (Default.aspx).We store those references in new object variables.

Then we assign in the label controls the Text values we get from the referenced controls. We use the IsCrossPagePostBack property to determine if a postback occurred from the source page.

9) Run your application, fill in the  and see for yourself that textbox control values are posted back to the SecondPage.aspx

Email me if you want the source code.

Hope it helps!!!

Comments»

No comments yet — be the first.

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

%d bloggers like this: