Querystring
This is the most common way to pass data and can be seen in almost all webform thru the URL, e.g. www.website.com?userid=1&username=abc. From the link, we know the userid value which is 1 and username as abc seperated by &
page1.aspx
protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("page2.aspx?userid=" + TextBox1.Text + "&username=" + Textbox2.Text); } |
page2.aspx
string strUserID = Request.QueryString["userid"]; string strUserName = Request.QueryString["username"]; |
note
You can access the parameters in the URL using Request.QueryString by specifying the parameter name or index anywhere within the page
Session
This varible will be stored in the memory and accessible thoughout the websiteSince this variable will be stored in the server's memory, its
page1.aspx
protected void Button1_Click(object sender, EventArgs e) { Session["userid"] = TextBox1.Text; Session["username"] = TextBox2.Text; Response.Redirect("page2.aspx"); } |
page2.aspx
string strUserID = Session["userid"].ToString(); string strUserName = Session["username"].ToString(); |
note
You can access the parameters in the Session variable by specifying the variable name or index anywhere within the website
Server.Tranfer
Beside using the Response.Redirect above, here im going to use Server.Trasfer to open page2
page1.aspx
protected void Button1_Click(object sender, EventArgs e) { Server.Transfer("page2.aspx", true); } |
page2.aspx
string strUserID = Request.Form["TextBox1"]; string strUserName = Request.Form["TextBox2"]; |
note
You can access the value of the control in the old page by specifying the control name
Cookies
Is a small text file stored on the client machine ~4kbs
page1.aspx
protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("UserID"); cookie.Value = TextBox1.Text; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); HttpCookie cookie = new HttpCookie("UserName"); cookie.Value = TextBox2.Text; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); Response.Redirect("WebForm2.aspx"); } |
page2.aspx
string strUserID = Request.Cookies["UserID"].Value; string strUserName = Request.Cookies["UserName"].Value; |
note
You can access the cookies by specifying the cookies name within the website
http://msdn.microsoft.com/en-us/library/ms178194.aspx
PreviousPage
2.0 and above
page1.aspx
protected void Button1_Click(object sender, EventArgs e){ Server.Transfer("page2.aspx"); } |
page2.aspx
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { TextBox txt1 = (TextBox)Page.PreviousPage.FindControl("TextBox1"); TextBox txt2 = (TextBox)Page.PreviousPage.FindControl("TextBox2"); string strUserID = txt1.Text; string strUserName = txt2.Text; } } |
note
I used Server.Transfer in this case