Search This Blog

Wednesday 9 March 2016

how to pass value from one page another page in asp.net

We can use the following technique.

*********Client Side*********

Cookies

Page 1 (Set)
            HttpCookie httpCookie = new HttpCookie("HelloWorld");
            httpCookie["SayHello"] = "Hello World";
            httpCookie.Expires = DateTime.Now.AddMinutes(60);
            Response.Cookies.Add(httpCookie);
Page 2 (Get)
            if (Request.Cookies["HelloWorld"] != null)
            {
                HttpCookie httpCookie = Request.Cookies["HelloWorld"];
                Response.Write(httpCookie["SayHello"].ToString());
            }

Query String

Page 1 (Set)
            Response.Redirect("~/Page_2.aspx?SayHello=Hello World");
Page 2 (Get)
            if (Request.QueryString["SayHello"] != null)
            {
                Response.Write(Request.QueryString["SayHello"].ToString());
            }

Hidden Field

Page 1 (Set)
            TextBox1.Text = "Hello World";
            HiddenField1.Value = "Hello World";
Page 2 (Get)
            if (Page.PreviousPage != null)
            {
                TextBox textBox = (TextBox)Page.PreviousPage.FindControl("TextBox1");
                if (textBox != null)
                {
                    Response.Write(textBox.Text);
                }
                HiddenField hiddenField = HiddenField)Page.PreviousPage.FindControl("HiddenField1");
                if (hiddenField != null)
                {
                    Response.Write(hiddenField.Value);
                }
            }

Note:-
You can get the Previous Page control only if Previous Page redirected using the following technique
            Server.Transfer("~/Page_2.aspx"true);
            Server.Execute("~/Page_2.aspx"true);
            PostBackUrl="~/Page_2.aspx" Property

Cache

Page 1 (Set)
      Cache["SayHello"] = "Hello World";
Page 2 (Get)
            if (Cache["SayHello"] != null)
            {
                Response.Write(Cache["SayHello"].ToString());
            }

*********Server Side*********

Session

Page 1 (Set)
      Session["SayHello"] = "Hello World";
Page 2 (Get)
            if (Session["SayHello"] != null)
            {
                Response.Write(Cache["SayHello"].ToString());
            }
Application

Page 1 (Set)
      Application["SayHello"] = "Hello World";
Page 2 (Get)
            if (Application["SayHello"] != null)
            {
                Response.Write(Cache["SayHello"].ToString());
            }

*********Other Ways*********

Public Properties

Page 1 (Set)
            public string SayHello
            {
                get
                {
                  return "Hello World";
                }
            }
Page 2 (Get)
Add in page directive
<%@ PreviousPageType VirtualPath="~/Page_1.aspx" %>

           if (Page.PreviousPage != null)
           {
              Response.Write(PreviousPage.SayHello); 
     }

Note:-
You can get the Previous Page control only if Previous Page redirected using the following technique
            Server.Transfer("~/Page_2.aspx"true);
            Server.Execute("~/Page_2.aspx"true);
            PostBackUrl="~/Page_2.aspx" Property

HttpContext

Page 1 (Set)
     HttpContext.Current.Items["SayHello"] = "Hello World";
Page 2 (Get)
           if (Page.PreviousPage != null)
           {
         Response.Write((String)HttpContext.Current.Items["SayHello"]);
     }
     
Note:-
You can get the Previous Page control only if Previous Page redirected using the following technique
           Server.Transfer("~/Page_2.aspx"true);
           Server.Execute("~/Page_2.aspx"true);
           PostBackUrl="~/Page_2.aspx" Property


Page Object

Page 1 (Set)
     TextBox1.Text = "Hello World";
           HiddenField1.Value = "Hello World";
Page 2 (Get)
           if (Page.PreviousPage != null)
           {
                Page_1 sourcePage;
                sourcePage = (Page_1)PreviousPage;

                TextBox textBox = (TextBox)sourcePage.FindControl("TextBox1");
                HiddenField hiddenField = (HiddenField)sourcePage.FindControl("HiddenField1");
                if (textBox != null)
                {
                    Response.Write(textBox.Text);
                }
                if (hiddenField != null)
                {
                    Response.Write(hiddenField.Value);
                }           
     }

Note:-
You can get the Previous Page control only if Previous Page redirected using the following technique
           Server.Transfer("~/Page_2.aspx"true);
           Server.Execute("~/Page_2.aspx"true);
           PostBackUrl="~/Page_2.aspx" Property

No comments:

Post a Comment