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;
        }

2. Use the querystring:

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

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)