Sunday, 13 November 2011

Different Methods to Pass Values between Web Forms





Untitled Document





Different Methods to Pass Values between Web Forms

Cross page posting means to post data from one page to another page.this is an important concept in web development.

there are many methods to perform this task.some of Them are following.

Cookies:

A small piece of information or message sent by the web server to User's web browser during a request, which is stored on user's system in the form of a text file(mostly as notepad file).

How to use a cookie.

//Cross Page Posting Using Cookies.
HttpCookie samplecookie = new HttpCookie("mycookie");
  samplecookie.Value = TextBox1.Text;
  samplecookie.Expires = DateTime.Now.AddHours(1);
  Response.Cookies.Add(samplecookie);
  Response.Redirect("default2.aspx");
  

ON Second page

Retrive value using cookies.

Label1.Text = Request.Cookies["mycookie"].Value;

Query String

this is another way to pass value between pages.
in it we have to use a question mark (?) as separator. "&" is use to pass multiple query strings.

how to use querystring

Response.Redirect("default2.aspx?pass="+TextBox2.Text);

Retrieve value using Querystring

Label2.Text = Request.QueryString["pass"]

Sessions

it is server side state management method.this is most secure and most acceptable way of cross page posting

how to use session on page.



Session["user"] = txtName.Text;

on second page

string str=Session["user"].ToString();

No comments:

Post a Comment