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:
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.