5 Ways to Send Data Between ASP.NET Pages

How many ways do you know to send data between ASP.NET Pages? In this article I'm going to list 5 ways. The first 2 or 3 ways are very well-known and the last 2 or 3 ways are not well-known. You will find here mostly inline methods to receive the data in various pages.

1.png

So, let's start with the most well-known one.

Using Session State

Using this technique I will store the data in a session variable on the client machine and on the next page, grab it; see:

ASPX Page:

<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnSessionState" runat="server" Text="Session State" OnClick="btnSessionState_Click" />

Code-Behind:

protected void btnSessionState_Click(object sender, EventArgs e)
{
    Session["Data"] = txtData.Text;
    Response.Redirect("SessionState.aspx");
}

Receiver ASPX Page:

<div>
<h1>Session State</h1>
    Data is: <%=Session["Data"] %>

And you are all set, run it and test it.

Using Query String

Using this technique I will add my data with an URL and on the next page grab it; see:

ASPX Page:

<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnQueryString" runat="server" Text="Query String" OnClick="btnQueryString_Click" />

Code-Behind:

protected void btnQueryString_Click(object sender, EventArgs e)
{
    Response.Redirect("QueryString.aspx?Data=" + Server.UrlEncode(txtData.Text));
}

Receiver ASPX Page:

<h1>Query String</h1>
        Data is: <%=Server.UrlDecode(Request.QueryString["Data"]) %>

And you are all set, run it and test it.

Using HpptPost

Using this technique I will call a post back URL and the on next page using Request.From I will grab it; see:

ASPX Page:

<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnHttpPost" runat="server" Text="HTTPPost" PostBackUrl="~/HttpPost.aspx" />

Note: There is no code-behind method call instead of a postbackurl in the button attribute.

Receiver ASPX Page:

<h1>HttpPost</h1>
        Data is: <%=Request.Form["txtData"] %>

And you are all set, run it and test it.

Using Public Properties

Using this technique I will send using a public method and on the next page, grab it using PreviousPage.MethodName; see:

ASPX Page:

<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnPublicProperties" runat="server" Text="Public Properties" OnClick="btnPublicProperties_Click" />

Code-Behind:

protected void btnPublicProperties_Click(object sender, EventArgs e)
{
    Server.Transfer("PublicProperties.aspx");
}
public string PublicData
{
    get 
  {
        return txtData.Text;
  }
}

Receiver ASPX Page:

<h1>Public Properties</h1>
        Data is: <%=PreviousPage.PublicData %>

And you are all set, run it and test it.

Using Controls

Using this technique I will just redirect the user to the next page and on the next page use PreviousPage.FindControl to grab the data; see:

ASPX Page:

<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnControl" runat="server" Text="Control" OnClick="btnControl_Click" />

Code-Behind:

protected void btnControl_Click(object sender, EventArgs e)
{
    Server.Transfer("Control.aspx");
}

Receiver ASPX Page:

<h1>Control</h1>
        Data is: <asp:Label ID="Label1" runat="server" Text="Label" />

Receiver Code-Behind Page:

protected void Page_Load(object sender, EventArgs e)
{
    var textbox = PreviousPage.FindControl("txtData") as TextBox;
    if (textbox != null)
    {
        Label1.Text = textbox.Text;
    }
}

And you are all set, run it test it.

You can use any technique given above depending upon your business requirements.


Similar Articles