Transfer data from one page to another page in asp.net from different ways
In this short article I will explain how to Transfer data from one page to another page in asp.net from different ways.
1. Use Cookies:
protected void CookiesButton_Click(object sender, EventArgs e)
{
HttpCookie cook = new HttpCookie("id");
cook.Expires = DateTime.Now.AddDays(1);
cook.Value = txtid.Text;
Response.Cookies.Add(cook);
Response.Redirect("Demopage.aspx");
}
//On Demopage:
protected void Page_Load(object sender, EventArgs e)
{
labelid.Text = Request.Cookies["id"].Value;
}
protected void RedirectButton_Click(object sender, EventArgs e)
{
Response.Redirect("Demopage.aspx?Id=" + Server.UrlEncode(txtid.Text));
}
3. Use HTTP POST:
<asp:Button ID="RedirectButton" runat="server" Text="UseHttpPost"
PostBackUrl="~/Demopage.aspx" onclick="RedirectButton_Click"/>
protected void RedirectButton_Click(object sender, EventArgs e)
{
// The PostBackUrl property of the Button takes care of where to send it.
}
4. Use PreviousPage Control Info:
protected void RedirectButton_Click(object sender, EventArgs e)
{
Server.Transfer("Demopage.aspx");
}
//On Demopage:
protected void Page_Load(object sender, EventArgs e)
{
var textbox = PreviousPage.FindControl("txtid.Text") as TextBox;
if (textbox != null)
{
label_Id.Text = textbox.Text;
}
}
5. Use HttpContext Items Collection:
protected void HttpContextButton_Click(object sender, EventArgs e)
{
HttpContext.Current.Items["id"] = txtid.Text;
Server.Transfer("Demopage.aspx");
}
//On Demopage:
protected void Page_Load(object sender, EventArgs e)
{
this.labelid.Text =(String) HttpContext.Current.Items["id"];
}
6. Use Session State:
protected void RedirectButton_Click(object sender, EventArgs e)
{
Session["Id"] = txtid.Text;
Response.Redirect("Demopage.aspx");
}
7. Use public properties:
public string Id
{
get
{
return txtid.Text;
}
}
protected void RedirectButton_Click(object sender, EventArgs e)
{
Server.Transfer("Demopage.aspx");
}
8. Use Cache:
protected void RedirectButton_Click(object sender, EventArgs e)
{
Cache["id"] = txtid.Text;
Server.Transfer("Demopage.aspx");
}
//On Demopage:
protected void Page_Load(object sender, EventArgs e)
{
this.labelid.Text = (string) Cache["id"];
}
Comments
Post a Comment