Transfer data from one web page to another web page ASP.NET
Transfer data from one web page to another web page ASP.NET.
We are having so many ways to transfer the data from one web page to another web page. Sending data is not a typical task in ASP.net. But we need to aware the technology to do so.
Below I have mentioned mostly all the possible techniques to pass the page controls data or any custom data from page to page transfer.Cookies:
Generally cookies are created at server side and saved at client side. We use HttpCookie to transfer the data from one page to another, here Response.Redirect is mandatory to navigate and share the data across the page. First Page:
protected void btnClickMe_Click(object sender, EventArgs e)
{
HttpCookie testText = new HttpCookie("MyText");
testText.Value = MyTextBox.Text;
Response.Cookies.Add(testText);
Response.Redirect("SecondPage.aspx");
} Second Page:
Session is a server side object and created and maintained at server side only. We can set the properties of session in the web.config file. A session is created when the server receives the first response from the client and exists up to close the browser.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["MyText"] != null)
messageLabel.Text = Request.Cookies["MyText"].Value;
}Response.Redirect:
It is basically used to redirect the webpage from one to another. But we can use this mechanism for Passing the data from first page to second. First Page:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
<asp:Button ID="btnClickMe" Text="Click Me" runat="server"
onclick="btnClickMe_Click" />
</div>
</form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication4
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClickMe_Click(object sender, EventArgs e)
{
Response.Redirect("SecondPage.aspx?MyText=" + MyTextBox.Text);
}
}
} Second Page:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="messageLabel" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class SecondPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["MyText"] != null)
messageLabel.Text = Request.QueryString["MyText"];
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}Session:
First Page:
protected void btnClickMe_Click(object sender, EventArgs e)
{
Session["MyText"] = MyTextBox.Text;
Response.Redirect("SecondPage.aspx");
} Second Page:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["MyText"] != null)
messageLabel.Text = Session["MyText"].ToString();
}Application Variables:
This is one of the ancient feature to store and transfer the data from one page to another or to access in any page. First Page:
protected void btnClickMe_Click(object sender, EventArgs e)
{
Application["MyText"] = MyTextBox.Text;
Response.Redirect("SecondPage.aspx");
} Second Page:
protected void Page_Load(object sender, EventArgs e)
{
if (Application["MyText"] != null)
messageLabel.Text = Application["MyText"].ToString();
}Server.Transfer:
: It is a mechanism to redirect and transfer the data from one page to another. But using this we can't see the difference in the browser but page will be changed accordingly. But this is an efficient mechanism to redirect and transfer the data. First Page:
public string GetMyName
{
get { return "HELLO"; }
}
protected void btnClickMe_Click(object sender, EventArgs e)
{
FirstPage value;
// Gets the Page.Context which is Associated with this page
value = (FirstPage)Context.Handler;
// Assign the Label control with the property "GetName" which returns string
MyTextBox.Text = value.GetMyName;
Server.Transfer("SecondPage.aspx",true);
} Second Page:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Form["MyTextBox"]);
}Cross Page Posting:
It is one of mechanism to transfer the data along with controls from one page to another. To do this needs to set the PostBackUrl property of a button control. First Page:
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
<asp:Button ID="btnClickMe" Text="Click Me" runat="server" PostBackUrl="~/SecondPage.aspx"
onclick="btnClickMe_Click" />
</div>
</form>
</body> Second Page:
protected void Page_Load(object sender, EventArgs e)
{
TextBox MyName = (TextBox)(PreviousPage.FindControl("MyTextBox"));
string myname = MyName.Text;
messageLabel.Text = myname;
}Query String:
We can also transfer the data by using query strings. First Page:
protected void btnClickMe_Click(object sender, EventArgs e)
{
Response.Redirect("SecondPage.aspx?myname=" + MyTextBox.Text);
} Second Page:
protected void Page_Load(object sender, EventArgs e)
{
string myname=string.Empty;
if (Request["myname"] != null)
myname = Request.QueryString["myname"];
}
Like as above demonstration, we are having several ways to transfer controls and send the data from one to another web pages. But each technique having its advantages and disadvantage. You need to select the technique based suitable to your requirement.
Happy Programing!!!.