Transferring Information Between Pages
Transferring Information Between Pages
Corss-Page Posting
Here’s an example—a page named CrossPage1.aspx that defines a form with two text boxes and a button. When the button is clicked, it posts to a page named CrossPage2.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrossPage1.aspx.cs" Inherits="CrossPage1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CrossPage1</title>
</head>
<body>
<form id="form1" runat="server" >
<div>
First Name:
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
Last Name:
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<asp:Button runat="server" ID="cmdPost"
PostBackUrl="CrossPage2.aspx" Text="Cross-Page Postback" /><br />
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CrossPage1</title>
</head>
<body>
<form id="form1" runat="server" >
<div>
First Name:
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
Last Name:
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<asp:Button runat="server" ID="cmdPost"
PostBackUrl="CrossPage2.aspx" Text="Cross-Page Postback" /><br />
</div>
</form>
</body>
</html>
public partial class CrossPage2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
lblInfo.Text = "You came from a page titled " +
PreviousPage.Title;
}
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
lblInfo.Text = "You came from a page titled " +
PreviousPage.Title;
}
}
}
Getting More Information from the Source Page
protected void Page_Load(object sender, EventArgs e)
{
CrossPage1 prevPage = PreviousPage as CrossPage1;
if (prevPage != null)
{
// (Read some information from the previous page.)
}
}
{
CrossPage1 prevPage = PreviousPage as CrossPage1;
if (prevPage != null)
{
// (Read some information from the previous page.)
}
}
In a projectless website, Visual St udio may flag this as an error, indi cating that it does not have the type information for the source page class (in this exampl e, that’s CrossPage1). However, once you compile the website, the error will disappear.
You can solve the problem like this:
Add this code in CrossPage2.aspx
<%@ PreviousPageType VirtualPath="~/CrossPage1.aspx" %>
Now, the PreviousPage property will automatically use the CrossPage1 type. That allows you to skip
the casting code and go straight to work using the previous page object, like this:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
// (Read some information from the previous page.)
}
}
{
if (PreviousPage != null)
{
// (Read some information from the previous page.)
}
}
A better choice is to define specific, limited methods or properties that extract just the information you need.
public partial class CrossPage1 : System.Web.UI.Page
{
public string FullName
{
get { return txtFirstName.Text + " " + txtLastName.Text; }
}
}
{
public string FullName
{
get { return txtFirstName.Text + " " + txtLastName.Text; }
}
}
Here’s how you can rewrite the code in CrossPage2.aspx to display the information from CrossPage1.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
lblInfo.Text = "You came from a page titled " +
PreviousPage.Title + "<br />";
CrossPage1 prevPage = PreviousPage as CrossPage1;
if (prevPage != null)
{
lblInfo.Text += "You typed in this: " + prevPage.FullName;
}
}
}
{
if (PreviousPage != null)
{
lblInfo.Text = "You came from a page titled " +
PreviousPage.Title + "<br />";
CrossPage1 prevPage = PreviousPage as CrossPage1;
if (prevPage != null)
{
lblInfo.Text += "You typed in this: " + prevPage.FullName;
}
}
}
The Query String
Here’s an example:
http://www.google.ca/search?q=organic+gardening
Here’s an example that uses this approach with the Response.Redirect() method:
// Go to newpage.aspx. Submit a single query string argument
// named recordID, and set to 10.
Response.Redirect("newpage.aspx?recordID=10");
// named recordID, and set to 10.
Response.Redirect("newpage.aspx?recordID=10");
You can send multiple parameters as long as they’re separated with an ampersand (&):
// Go to newpage.aspx. Submit two query string arguments:
// recordID (10) and mode (full).
Response.Redirect("newpage.aspx?recordID=10&mode=full");
// recordID (10) and mode (full).
Response.Redirect("newpage.aspx?recordID=10&mode=full");
The receiving page has an easier time working with the query string. It can receive the values from
the QueryString dictionary collection exposed by the built-in Request object:
string ID = Request.QueryString["recordID"];
Note that information is always retrieved as a string, which can then be converted to another simple data type. Values in the QueryString collection are indexed by the variable name. If you attempt to retrieve a value that isn’t present in the query string, you’ll get a null reference.
URL Encoding
To perform URL encoding, you use the UrlEncode() and UrlDecode() methods of the HttpServerUtility class. As you learned in Chapter 5, an HttpServerUtility object is made available to your code in every web form through the Page.Server property. The following code uses the UrlEncode() method to rewrite the previous example, so it works with product names that contain special characters:
string url = "QueryStringRecipient.aspx?";
url += "Item=" + Server.UrlEncode(lstItems.SelectedItem.Text) + "&";
url += "Mode=" + chkDetails.Checked.ToString();
Response.Redirect(url);
url += "Item=" + Server.UrlEncode(lstItems.SelectedItem.Text) + "&";
url += "Mode=" + chkDetails.Checked.ToString();
Response.Redirect(url);
Notice that it’s important not to encode everything. In this example, you can’t encode the & character that joins the two query string values, because it truly is a special character.
You can use the UrlDecode() method to return a URL- encoded string to its initial value. However, you don’t need to take this step with the query s tring. That’s because ASP.NET automatically decodes your values when you access them through the Reque st.QueryString collection.