Q:

sending a variable to another page in asp.net

i am working with c# asp.net.I am trying to take an input from user and use it in another aspx page.This is my code in first page.
<asp:Button ID="btnProduct1Addtocart" runat="server" Text="Add to cart" postbackurl="~/CartPage.aspx"/>

And this is second page's code which is in codebehind.
Label lblProduct1Name = (Label)PreviousPage.FindControl("lblProduct1Name");
Label1.Text = lblProduct1Name.Text;

But i get an error like "NullReferenceException was unhandled by user code" which is in the  "Label1.Text = lblProduct1Name.Text;" line.Thank you for your help.

A:

you need to pass the value using query string like this:
<asp:Button ID="btnProduct1Addtocart" runat="server" Text="Add to cart" onclick="btnAddToCart_Clicked"/>

Code behind event:
private void btnAddToCart_Clicked(object sender, System.EventArgs e)
{
Response.Redirect("~/CartPage.aspx?Name=" +lblProduct1Name.Text);
}

On page load of CartPage.aspx do like this:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = Request.QueryString["Name"];
} 

Also have a look at this article to get understanding of it.

