The Ways Passing Values between Different Web Pages Using C#
In the web application, pages often need the information from the previous pages. Here, I summarized the various ways that can be used to pass values between different web pages by giving the following source example.
1. Sources Files
1.1 Prevous page:
Previouspage.aspx
Previouspage.aspx.cs
1.2 Current page
Currentpage.aspx
Currentpage.aspx.cs
2. Source
2.1 PreviousPage.aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”PreviousPage.aspx.cs” Inherits=”ValuePassing.PreviousPage” %>
<head runat=”server”>
<title>Previous Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr><td>Query String Item : </td><td><asp:TextBox ID=”QueryStringItem” runat=”server” Text=”Query String Passed”></asp:TextBox><br /></td></tr>
<tr><td>Post Information: </td><td><asp:TextBox ID=”PostInformation” runat=”server” Text=”Post Information Passed”></asp:TextBox><br /></td></tr>
<tr><td>Session Object: </td><td><asp:TextBox ID=”SessionObject” runat=”server” Text=”Session Value Passed”></asp:TextBox><br /></td></tr>
<tr><td>Public Property: </td><td><asp:TextBox ID=”PublicProperty” runat=”server” Text=”Public Value Passed”></asp:TextBox><br /></td></tr>
<tr><td>Text Control: </td><td><asp:TextBox ID=”TextControl” runat=”server” Text=”Text Control Value Passed”></asp:TextBox><br /></td></tr>
<tr><td colspan=”2″ align=”center”><asp:Button ID=”Submit” runat=”server” Text=”To Current Page” OnClick = “Submit_Click”/></td></tr>
</table>
</div>
</form>
</body>
</html>
2.2 PreviousPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ValuePassing
{
public partial class PreviousPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
//Passing value to another page through Session
Session["SessionObject"] = SessionObject.Text;
//Passing value to next page through adding query string at the end of URL
Server.Transfer(“CurrentPage.aspx?QueryStringItem=” + QueryStringItem.Text);
}
//Passing value to next page through Public Property
public String CurrePublicProperty
{
get
{
return PublicProperty.Text;
}
}
}
}
2.3 CurrentPage.aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”CurrentPage.aspx.cs” Inherits=”ValuePassing.CurrentPage” %>
<%@ PreviousPageType VirtualPath=”~/PreviousPage.aspx” %>
<head runat=”server”>
<title>Current Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr><td>Previous Query String Item :</td><td><asp:TextBox ID=”QueryStringItem” name=”PreviousQueryStringItem: ” runat=”server” Text=”"></asp:TextBox> </td></tr>
<tr><td>Previous Post Information :</td><td><asp:TextBox ID=”PostInformation” name=”PreviousPostInformation: ” runat=”server” Text=”"></asp:TextBox> </td></tr>
<tr><td>Previous Session Object :</td><td><asp:TextBox ID=”SessionObject” name=”PreviousSessionObject: ” runat=”server” Text=”"></asp:TextBox> </td></tr>
<tr><td>Previous Public Property :</td><td><asp:TextBox ID=”PublicProperty” name=”PreviousPublicProperty: ” runat=”server” Text=”"></asp:TextBox> </td></tr>
<tr><td>Previous Text Control :</td><td><asp:TextBox ID=”TextControl” name=”PreviousTextControl: ” runat=”server” Text=”"></asp:TextBox> </td></tr>
</table>
</div>
</form>
</body>
</html>
2.4 CurrentPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ValuePassing
{
public partial class CurrentPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Getting query string value from previous page
QueryStringItem.Text = Request.QueryString["QueryStringItem"];
//Getting post information value from previous page
PostInformation.Text = Request.Form["PostInformation"];
//Getting session value value from previous page
SessionObject.Text = (string)(Session["SessionObject"]);
//Getting public property value from previous page
PublicProperty.Text = PreviousPage.CurrePublicProperty;
//Getting control value from previous page
TextBox txtBox = (TextBox)PreviousPage.FindControl(“TextControl”);
if (txtBox != null)
{
TextControl.Text = txtBox.Text;
}
}
}
}
3. Pages
3.1 Previous Page

3.2 Current Page






AACSB Accredited
3500 Old Main Hill
|
Logan, UT 84322-3500
|
Phone: 435.797.2272
|
Fax: 435.797.2399
|
Email:
Comments