Cross-Page post backs using ASP.NET 3.5

By Mohammad Mahdi Ramezanpour at August 30, 2008 02:06
Filed Under: ASP.NET

Some developers are still want to use cross page post backs that we used it in the classic version of ASP. For example: you may want to create a page that contains a form and add them to your database in another page like ASP classic.

In this post I want to show how to do such a thing in ASP.NET and how you can make it easier in ASP.NET 3.5.

Imagine that I have a Default.aspx file that shows a form and users can enter their name and company name; and also I have another page named DataTransfer.aspx that shows user's information in some labels.

Here is my Default.aspx page:

<%@ Page Language="C#" 
AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center;">
        Name:<br />
        <asp:TextBox runat="server" ID="nameTextBox"></asp:TextBox><br />
        Company name:<br />
        <asp:TextBox runat="server" ID="companyTextBox"></asp:TextBox><br />
        <asp:Button runat="server" ID="submitButton"
         Text="Save" 
         PostBackUrl="DataTransfer.aspx" />
    </div>
    </form>
</body>
</html>

As you can see, I have two textboxes named "nameTextBox", "companyTextBox" and also a button named "submitButton" that are normal and there is nothing unusual in it. The interesting part in the PostBackUrl of my button that links this page to our second page.

There is nothing to do more in the first page, so lets take a look at our second page "DataTransfer.aspx":

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataTransfer.aspx.cs" Inherits="DataTransfer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>User's information</h3>
    <div>
        Name : 
        <asp:Label runat="server" ID="nameLabel"></asp:Label><br />
        Company : 
        <asp:Label runat="server" ID="companyLabel"></asp:Label>
    </div>
    </form>
</body>
</html>

Very simple, just two labels in order to show the user's information. I have to write some code in the code-behind section in order to bind information to labels. Here is my code-behind of my DataTransfer.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class DataTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create an instance of our previous page's controls that we want to get.
        TextBox name = null;
        TextBox company = null;
        
        // Fill variables with the values in the previous page using FindControl method of Previous Page property.
        name = (TextBox)this.PreviousPage.FindControl("nameTextBox");
        company = (TextBox)this.PreviousPage.FindControl("companyTextBox");
 
        // Bind lables using values in textboxes.
        nameLabel.Text = name.Text;
        companyLabel.Text = company.Text;
    }
}

In the code above I used PreviousePage property available in every ASP.NET page and FindControl method of that property. With FindControl you can get the object you want by it's ID from your previous page. But because the information returns from FindControl are objects and no specific types, you must cast it as I did.

If you launch your application now, you will see you can gather the information from first page and show them in the second page.

Now let's change it a little bit by add a new directive to our second page. This directive name PreviousPageType and it's new with ASP.NET 3.5 and let you specify your previous page path. So you can have all public or internal (Friend in VB) classes, methods, properties or any other objects in your second page. This is my second page after a little changes:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataTransfer.aspx.cs" Inherits="DataTransfer" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
...

You know, because I'm always trying to be organized :D, I want to create two properties in my first page that holds values in textboxes:

    public string Name
    {
        get { return nameTextBox.Text; }
    }
 
    public string CompanyName
    {
        get { return companyTextBox.Text; }
    }

Now you can get values of textboxes in Default.aspx page much easier. It means that Visual Studio can identify our first page objects and you can use code hints in order to access your objects in the second page as shown in the picture below:

ASPNET35 Previous Page Inteligency

And our second page's code-behind will change to the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class DataTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Fill variables with the values in the previous page using properties.
        nameLabel.Text = PreviousPage.Name;
        companyLabel.Text = PreviousPage.CompanyName;
    }
}

Now you know that with the PreviousPageType new directive in ASP.NET 3.5, it's much easier to access you object in a page from another page.

Comments

3/30/2009 5:51:49 AM #

free proxy list

what a great post, thanks.

free proxy list United States

4/13/2009 9:39:25 PM #

blogging tips

hi, do you know where can i learn blogengine.net?

blogging tips United States

4/21/2009 8:02:56 AM #

Sonali

Great Post !!!!!!!!!!!!!!!

Sonali United States

4/28/2009 1:33:20 PM #

franchise blog

Heya - Just looking through some blogengine.net blogs, seems to be a fairly nice platform, certainly better than blogger but still playing with the idea of wordpress.  Any major plus points you have found over WP at all?

Thanks

Matthew

franchise blog United Kingdom

4/29/2009 4:49:52 PM #

Kampanye Damai Pemilu Indonesia 2009

what a great post, i love to read it.

Kampanye Damai Pemilu Indonesia 2009 United States

5/26/2009 8:36:37 PM #

Craig

Haven't tried this yet, but interested to know ifu got this to work in a master page scenario from a content page? You'd normally only have one form somewhere in the root master (if nested) would this result in posting form variables including all usercontrol values if any etc?

Craig United States

6/9/2009 9:54:36 PM #

Franchises for sale

It's interesting, the blog engine platform seems very variable in form.  My design skills are not so good as my C coding though, I would be interested in seeing what additional skins you can get for it.  Nice blog btw, best wishes for it and keep up the posts. Smile  Kind regards,  Peter sims.

Franchises for sale United States

6/20/2009 4:40:32 PM #

Sulumits Retsambew

hello, this is my first time i visit here. I found so many interesting in your blog especially its discussion. keep up the good work.

Sulumits Retsambew United States

6/23/2009 11:15:44 AM #

vasant

i love this

vasant India

6/30/2009 3:25:12 PM #

Tukang Nggame

thanks for great info, this very useful

Tukang Nggame United States

7/10/2009 8:51:50 AM #

Internet Marketing Company

Good post, but have you thought about Cross-Page post backs using ASP.NET 3.5 before?

Internet Marketing Company India

7/14/2009 11:58:15 PM #

Pierre Cardin

I like your blog curently we are looking for a part time article writer would you be interested?

Pierre Cardin United States

7/19/2009 3:28:03 PM #

Slankepiller

Thanks - Just the info I was looking for.. My search ends here..

Slankepiller

7/22/2009 6:29:36 PM #

Jm-Experts

cool! keep up the good work!

Jm-Experts United States

7/23/2009 1:03:12 AM #

Chase Durer

Hmm strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

Chase Durer United States

7/27/2009 3:38:44 PM #

Jm-Experts!

wow! you guys have nice postingS!

Jm-Experts! United States

8/7/2009 9:07:22 AM #

homeserve boiler

"it's much easier to access you object in a page from another page" - seems a great feature ...  for the solus programmer - how does this work with programming teams, having page code (the second) relying on the other page (first).  Does this up the ante for version control and documentation?

homeserve boiler United Kingdom

8/7/2009 6:41:49 PM #

San Diego Foreclosure

We�re reading, keep writing! Thanks for being so open!

San Diego Foreclosure United States

8/9/2009 9:54:25 PM #

Goose Hunting Canada

Thank you for sharing this fine article. Very inspiring!

Goose Hunting Canada United States

Comments are closed

Currently Reading

Quote of the day

Send Persian SMS