Showing posts with label Pass Values Between ASP.NET Web Forms Pages. Show all posts
Showing posts with label Pass Values Between ASP.NET Web Forms Pages. Show all posts

Friday, January 9, 2015

How We Can the Pass the Value from One Page to Another page in ASP.Net with C#

Mainly  we can transfer the value in 4 Ways:
 
     1-   Via Session Variable
     2-   Via Query String
     3-   Via Cookies
     4-   Via Post Back URL

Now we move one by one:
 
     1-   Session Variable-
 

A)   How We Send Value Via Session:
 
There is a Textbox, we can assign the value of this Textbox to the Session with this code,We write this code on button click event to send Textbox Value-

 
protected void btnSend_Click(object sender, EventArgs e)
    {
        Session["Name"] = txtSendName.Text;
        Response.Redirect("Receive.aspx");
    }
B)    How We Receive Value On Another Page From Session-

protected void Page_Load(object sender, EventArgs e)
    {
        txtReceiveName.Text = Convert.ToString(Session["Name"]);
    }


2-   Query String-
 
A)   How to Send Value Via Query String :

 
protected void btnSend_Click(object sender, EventArgs e)
    {
        Response.Redirect("Receive.aspx?QueryStringName=" + txtSendName.Text);
    }

B)How to Receive Value From Query String:

protected void Page_Load(object sender, EventArgs e)
    {
        txtReceiveName.Text = Convert.ToString(Request.QueryString["QueryStringName"]);
    }
3-Cookies-
 
A)   How To Send Value Via Cookies:
 
protected void btnSend_Click(object sender, EventArgs e)
     {
         if (Request.Browser.Cookies)   // To Check Browser is Supporting Cookies or Not
         {
             HttpCookie cookie = newHttpCookie("Name");
             cookie.Value = txtSendName.Text;
             cookie.Expires = DateTime.Now.AddDays(1);
             Response.Cookies.Add(cookie);
             Response.Redirect("Receive.aspx");
         }
     }
 
B)    How To Receive Value From Cookie:
 
protected void Page_Load(object sender, EventArgs e)
    {
        txtReceiveName.Text = Request.Cookies["Name"].Value;
    }
3-Post Back URL-
 
A)   How we Can send Value Via Post Back URL:

 
Just Set Post Back URL Property Like-

<asp:ButtonID="btnSend"runat="server"ForeColor="Maroon"Text="Post Back URL"PostBackUrl="~/Receive.aspx"/>
 
B)    How we Can Receive Value:

protected void Page_Load(object sender, EventArgs e)
    {
        TextBox Name = (TextBox)PreviousPage.FindControl("txtSendName");
        txtReceiveName.Text = Name.Text;
    }
Author-Er. Rahul Kumar Yadav