At times there becomes a need to transfer data
from one page to another page in ASP.NET and for doing this, there are
various ways through which we can transfer data from one page to another page.
For transferring the data we need to store the data either on the client side or
on the server side.
Client Side Storage Mechanism:-
- Cookies
- Query String
- Hidden Field (View State)
- Cache
Server Side Storage Mechanism:-
- Session
- Application
We can easily transfer data through a query
string but the drawback of using this concept is that the string is visible in the
address bar and your site can be easily hacked. This means we cannot transfer
sensitive data such as passwords or any other data. And the 2nd limitation is
that we can transfer data maximum up to 255 characters and not more than that.
There is one more approach that can be used for transferring data that is the PostBackUrl property. This property can be set on a Button when you need to
transfer data from one page to another page. The value of this property should
be the transferred page. Let's suppose that we have 2 pages
- Survey.aspx
- SurveyReceipt.aspx
And we need to transfer data from survey.aspx to
surveyReceipt.aspx. In this case we set the PostBackUrl of the button
control on survey.aspx to SurveyReceipt.aspx.
Following is the design of Survey.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Survey.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>Untitled
Page</title>
<style type="text/css">
.style1
{
height: 46px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:
100%; color:
#0E0E6C">
<tr>
<td align="left" valign="top" colspan="2" style="background-color:
Maroon; color:
White;">
<span style="font-size:
18pt; font-family:
Verdana; font-style:
normal;"> Help
us to help you!</span>
<br />
Personal Info
</td>
</tr>
<tr>
<td align="right" valign="top">
Name:</td>
<td>
<input id="txtName" type="text" name="txtName" runat="server" enableviewstate="true" />
</td>
</tr>
<tr>
<td align="right" valign="top" class="style1">
Gender:</td>
<td class="style1">
<input id="Radio1" name="optGender" type="radio" value="Female" runat="server" enableviewstate="true" />Female
<br />
<input id="Radio2" name="optGender" type="radio" value="Male" runat="server" enableviewstate="true" />Male
</td>
</tr>
<tr>
<td align="right" valign="top">
Mountain Bike
Rider?</td>
<td>
<input id="chkMBR" checked="checked" name="chkMBR" type="checkbox" runat="server"
enableviewstate="true" />
</td>
</tr>
<tr>
<td align="right" valign="top">
Road Rider?</td>
<td>
<input id="chkRR" checked="checked" name="chkRR" type="checkbox" runat="server" enableviewstate="true" />
</td>
</tr>
<tr>
<td align="right" valign="top">
Favorite Trails
from <br />
around the world:</td>
<td>
<textarea id="txtTrails" cols="50" name="txtTrails" rows="3" runat="server" enableviewstate="true"></textarea>
</td>
</tr>
</table>
<hr />
<asp:Table ID="tblOuter" runat="server" ForeColor="#0E0E6C" Width="100%">
<asp:TableRow ID="outerR1" runat="server">
<asp:TableCell ID="outerR1C1" runat="server" ColumnSpan="2">
<asp:Panel ID="pnlActivity" runat="server" Width="100px">
Recent and
Planned Activity</asp:Panel>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR2" runat="server">
<asp:TableCell ID="outerR2C1" runat="server" VerticalAlign="Middle" Width="1%" Wrap="False">Rides</asp:TableCell>
<asp:TableCell ID="outerR2C2" runat="server">
<asp:Table ID="tblInner" runat="server" GridLines="None" Width="100%">
<asp:TableRow ID="innerR1" runat="server">
<asp:TableCell ID="innerR1C1" runat="server" Wrap="false" Width="1%">
<asp:Literal ID="itlLast" runat="server" Text="Last Trip>>>"></asp:Literal>
</asp:TableCell>
<asp:TableCell ID="innerR1C2" runat="server">
<asp:Calendar ID="calLast" runat="server" TodayDayStyle-BackColor="Maroon"></asp:Calendar>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="innerR2" runat="server">
<asp:TableCell ID="innerR2C1" runat="server" Wrap="false" Width="1%">
<asp:Literal ID="ltlNext" runat="server" Text="Next Trip>>>"></asp:Literal>
</asp:TableCell>
<asp:TableCell ID="innerR2C2" runat="server">
<asp:Calendar ID="calNext" runat="server" TodayDayStyle-BackColor="Maroon"></asp:Calendar>|
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR3" runat="server">
<asp:TableCell ID="outerR3C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False">Your
Ability:</asp:TableCell>
<asp:TableCell ID="outerR3C2" runat="server">
<asp:DropDownList ID="ddAbility" runat="server">
<asp:ListItem Text="Beginner"></asp:ListItem>
<asp:ListItem Text="Competent"></asp:ListItem>
<asp:ListItem Text="Expert"></asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR4" runat="server">
<asp:TableCell ID="outerR4C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False">Your
Experience:</asp:TableCell>
<asp:TableCell ID="outerR4C2" runat="server">
<asp:ListBox ID="lstExperience" runat="server">
<asp:ListItem Text="Ridden only on the
road"></asp:ListItem>
<asp:ListItem Text="Ridden on some
forest fire-breaks"></asp:ListItem>
<asp:ListItem Text="Ridden singletrack
trails"></asp:ListItem>
<asp:ListItem Text="Ridden down
mountains"></asp:ListItem>
<asp:ListItem Text="Raced down
mountains"></asp:ListItem>
<asp:ListItem Text="Won races down
mountains"></asp:ListItem>
</asp:ListBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR5" runat="server">
<asp:TableCell ID="outerR5C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False">Your
Goals:</asp:TableCell>
<asp:TableCell ID="outerR5C2" runat="server">
<asp:CheckBoxList ID="chkGoals" runat="server">
<asp:ListItem Text="To get more
road-riding experience"></asp:ListItem>
<asp:ListItem Text="To get more
mountain-biking experience"></asp:ListItem>
<asp:ListItem Text="To upgrade my
bike"></asp:ListItem>
<asp:ListItem Text="To get the latest
accessories"></asp:ListItem>
</asp:CheckBoxList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR6" runat="server">
<asp:TableCell ID="outerR6C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False">Contact:</asp:TableCell>
<asp:TableCell ID="outerR6C2" runat="server">
<asp:RadioButtonList ID="optMarketing" runat="server">
<asp:ListItem Selected="True">Do
not send me sales information</asp:ListItem>
<asp:ListItem>Only
send me sales information from Adventure Works</asp:ListItem>
<asp:ListItem>Send
me any sales information I might be interested in</asp:ListItem>
</asp:RadioButtonList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR7" runat="server">
<asp:TableCell ID="outerR7C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False">Where
do you normally ride?</asp:TableCell>
<asp:TableCell ID="outerR7C2" runat="server">
<asp:ImageMap ID="imgMap" OnClick="imgMap_Click" runat="server" ImageUrl="~/Images/BikeTheWorld.gif"
HotSpotMode="PostBack">
<asp:PolygonHotSpot Coordinates="0,0,150,0,75,60,75,100,0,75" HotSpotMode="PostBack"
PostBackValue="USA" TabIndex="1" />
<asp:PolygonHotSpot Coordinates="160,0,225,0,200,60,125,60,125,25" HotSpotMode="PostBack"
PostBackValue="Europe" TabIndex="2" />
</asp:ImageMap>
<asp:HiddenField ID="hdnRegion" runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="outerR8" runat="server">
<asp:TableCell ID="outerR8C1" runat="server" HorizontalAlign="Left" VerticalAlign="Top"
Width="1%" Wrap="False"></asp:TableCell>
<asp:TableCell ID="outerR8C2" runat="server">
<asp:ImageButton ID="ImageButton1" runat="server" PostBackUrl="~/SurveyReceipts.aspx"
ImageUrl="~/images/Submit.GIF"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Following is the Design of SurveyReceipt.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SurveyReceipts.aspx.cs" Inherits="SurveyReceipts" %>
<!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>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Following is the code of Survey.aspx.cs
using
System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
}
protected void
imgMap_Click(object sender,
ImageMapEventArgs e)
{
hdnRegion.Value = e.PostBackValue;
}
}
Following is the code of SurveyReceipts.aspx.cs
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public partial class SurveyReceipts : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
if (Page.PreviousPage !=
null)
{
HtmlInputText txtName = (HtmlInputText)PreviousPage.FindControl("txtName");
HtmlInputCheckBox chkMBR = (HtmlInputCheckBox)PreviousPage.FindControl("chkMBR");
HtmlInputCheckBox chkRR = (HtmlInputCheckBox)PreviousPage.FindControl("chkRR");
HtmlTextArea txtTrails = (HtmlTextArea)PreviousPage.FindControl("txtTrails");
Calendar calLast = (Calendar)PreviousPage.FindControl("calLast");
Calendar calNext = (Calendar)PreviousPage.FindControl("calNext");
DropDownList ddAbility = (DropDownList)PreviousPage.FindControl("ddAbility");
ListBox lstExperience = (ListBox)PreviousPage.FindControl("lstExperience");
CheckBoxList chkGoals = (CheckBoxList)PreviousPage.FindControl("chkGoals");
RadioButtonList optMarketing = (RadioButtonList)PreviousPage.FindControl("optMarketing");
HiddenField hdnRegion = (HiddenField)PreviousPage.FindControl("hdnRegion");
string sName =
txtName.Value.ToString();
string sGender = Request.Form["optGender"];
bool bMBR = chkMBR.Checked;
bool bRR = chkRR.Checked;
string sTrails = txtTrails.Value;
string sLast =
calLast.SelectedDate.ToLongDateString();
string sNext =
calNext.SelectedDate.ToLongDateString();
string sAbility =
ddAbility.SelectedValue.ToString();
string sExperience =
lstExperience.SelectedValue.ToString();
string sGoals =
"";
foreach (ListItem
goal in chkGoals.Items)
{
if (goal.Selected)
{
sGoals += goal.Text + "<br />";
}
}
string sMarketing =
optMarketing.SelectedValue.ToString();
string sRegion = hdnRegion.Value;
Label1.Text =
"Your Survey Details:<br />"
+
"Name: " + sName + "<br
/>"
+
"Gender: " + sGender +
"<br />"
+
"Mountain Bike Rider: " + bMBR + "<br />"
+ "Road
Rider: " + bRR + "<br
/>"
+ "Favourite
Trails: " + sTrails +
"<br />"
+ "Last
Ride: " + sLast + "<br
/>"
+ "Next
Ride: " + sNext + "<br
/>"
+ "Your
Ability: " + sAbility + "<br />"
+ "Your
Experience: " + sExperience + "<br />"
+ "Your
Goals: " + sGoals + "<br
/>"
+
"Marketing Info: " + sMarketing + "<br />"
+ "Your
Region: " + sRegion + "<br />";
}
else
{
Label1.Text = "Welcome Guest";
}
}
}
Now just run the page and test the functionality. Enter some values in the
survey.aspx page and click on the button; you'll see that the values are
retrieved and displayed on the surveyreceipt.aspx page
Here you saw how to transfer data from one page to another page but what if
we were having a master page applied on webpage then in that case than the way
of transferring data from one page to another page becomes different because all
your control lies inside the contentPlaceholder of your master page then in this
case we need to first refer to the contentplaceholder control and then we can
access the control within the webpage.
Following is the example First of all we'll have to create a master page for
doing this activity just for explanation purpose we'll create a demo master
page.
Following is the master page design and save it as TopLevel.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="TopLevel.master.cs" Inherits="TopLevel" %>
<!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>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%;">
<tr>
<td align="right" colspan="3">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/AdvWorksLogo.gif" />
</td>
</tr>
<tr>
<td style="width:
10%">
<asp:Image ID="Image2" runat="server" ImageUrl="~/images/AdvWorksSideBar.gif" />
</td>
<td valign="top">
<asp:ContentPlaceHolder id="pageContent" runat="server">
<p>
<br />
</p>
<p>
</p>
<p>
</p>
<p>
</p>
</asp:ContentPlaceHolder>
</td>
<td style="width:
10%">
</td>
</tr>
<tr>
<td align="right" colspan="3">
<asp:Label ID="Label1" runat="server"
Text="<b>Copyright&copy;2011@AdventureWork.com</b>"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
Now we need to apply this master page on 2 web
page for this we'll have to add 2 new web page having TopLevel.master as the
master page.
1st web page design following is the code
<%@ Page Language="C#" MasterPageFile="~/Toplevel.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled
Page" %>
<asp:Content ID="Content2" ContentPlaceHolderID="pageContent" Runat="Server">
<table style="width:30%;">
<tr>
<td class="style1" style="width:
120px">
Enter
Name :</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1" style="width:
120px">
Enter
Add :</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1" style="width:
120px">
Enter
City :</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1" style="width:
120px">
Enter
State :</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" class="style1" colspan="2">
<asp:Button ID="btnSend" runat="server" Text="Send" PostBackUrl="~/Default2.aspx" />
</td>
</tr>
</table>
</asp:Content>
After designing you web page should look like this.

Set the PostBackUrl property of the btnSend button to Default2.aspx page.
Now design 2nd web page with just one label control added to it, following is
the design code of default2.aspx page
<%@ Page Language="C#" MasterPageFile="~/Toplevel.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled
Page" %>
<asp:Content ID="Content2" ContentPlaceHolderID="pageContent" Runat="Server">
<asp:Label ID="Label1" runat="server"></asp:Label>
</asp:Content>
Now in default2.aspx.cs on the page load event
we'll write code for accessing the values from default.aspx page and display it
on the label control.
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
if (Page.PreviousPage !=
null)
{
ContentPlaceHolder content = (ContentPlaceHolder)Page.PreviousPage.Form.FindControl("pageContent");
string name = ((TextBox)content.FindControl("TextBox1")).Text;
string add = ((TextBox)content.FindControl("TextBox2")).Text;
string city = ((TextBox)content.FindControl("TextBox3")).Text;
string state = ((TextBox)content.FindControl("TextBox4")).Text;
Label1.Text = "Your Details are : <br/>Your
Name : " + name + "<br/>Your Add : " +
add + "<br/>Your City : " + city +
"<br/>Your State : " +
state;
}
else
Label1.Text =
"Welcome Guest";
}
}
Hope this activity you have liked and it may help you in your projects.