ARTICLE

Different Ways to Pass Data between Web Forms

Posted by Suthish Nair Articles | Learn .NET February 27, 2011
Here you will see Different Ways to Pass Data between Web Forms.
Reader Level:
Download Files:
 

Introduction

This article helps to list the most used ways to pass or transfer data information from one web page to another.

The important part of website development is how to manage data across web page requests. As you all know about

the HTTP (Hypertext Transfer Protocol) it is the transport protocol or the application layer above TCP/IP

that is responsible for each server and client request; it is stateless. It does not keep any data information for the next 

page response-request; the connection is closed from the server.

 

Getting Started

There are various ways to pass data information across pages.

1.      Cookies

       A small piece of information or message sent by the web server to web browser during a web request, 

      which is stored on user's system in the form of a text file.

 

      Using a Cookie

       HttpCookie UserCookie = new HttpCookie("User");

              UserCookie.Value = txtName.Text;

              UserCookie.Expires = DateTime.Now.AddHours(1);

              Response.Cookies.Add(UserCookie);

      Retrieving a Cookie value

                    lblMsg.Text = Request.Cookies["User"].Value;

2.      QueryString

 

       This is another way to pass data between pages using URL. 


       e.g.: http://www.c-sharpcorner.com/Notification/Notification.aspx?Mode=out

       

       The section beyond part marked in red is where the query string starts, using a question mark (?) as separator. 


       Using "&" we can pass multiple query strings.

    

      Using a Query String

              Response.Redirect(String.Format("RetrieveData.aspx?user={0}&id={1}", txtName.Text, 1));

      Retrieving a Query String value

                    if (Request.QueryString["user"] != null{

                     lbl2.Text = Request.QueryString["user"]; }

 

3.   Sessions

 

       The most acceptable and secure method is by using Session variables.


      Using a Session

              Session["user"] = txtName.Text;

              Session["id"] = 1;

              List<string> colors = new List<string>();

       colors.Add("red");

       colors.Add("green");

       colors.Add("blue");

       Session["colors"] = colors;

       colors = null;

              Response.Redirect("RetrieveData.aspx");

      Retrieving a Session value

                   if (Session["user"] != null)

       {

              lbl3.Text = Session["user"].ToString();

       }

       if (Session["colors"] != null)

       {

              List<string> col = new List<string>();

              col = (List<string>) Session["colors"];

 

              foreach (var item in col)

              {

                     lbl3.Text += " " + item + ", ";

              }

              col = null;

       }

 

 

 4. Cross-Page Posting

 

     Another technique used to post from one page to another page is by setting the PostBackUrl property to the target page

     

     The PreviousPage property in the target page contains a reference to the source page.

    

            Using Cross-Page Posting

              <asp:Button ID="btnPage2" Text="Call Page2" runat="server" width="132px" PostBackUrl="~/RetrieveData.aspx" />

            Retrieving a Cross-Page Posting value

                   if (Page.PreviousPage != null{

                     DropDownList ddl = (DropDownList)Page.PreviousPage.FindControl("ddl1");

                     if (ddl != null{

                           lbl4.Text = ddl.SelectedItem.Text;

                           lbl4.ForeColor = System.Drawing.Color.FromName(lbl4.Text); }

 

5. Server.Transfer

 


    Using this technique, form values of one page can be accessed in another page by enabling second parameter to true. 


    Also, the same for the Server.Execute method.

   Using Server.Transfer

              protected void btnPage2_Click(object sender, EventArgs e) {

                     Server.Transfer("RetrieveData.aspx", true); }

 

           Retrieving a Server.Transfer value

                   if (Request.Form["ddl1"] != null{

                     lbl5.Text = Request.Form["ddl1"]; }


Other ways that are used much are Submit Form, Context.Handler, Static variables, Saving data to XML, Text file etc.. 

or a Database and later retrieve it another form. 


Conclusion


Hope you all like it, post your comments also.


Login to add your contents and source code to this article
post comment
     

Hi Anand, post your queries to Forums.

Posted by Suthish Nair Jul 01, 2011

function getVendorallotFiles($file_date,$account_id=NULL) { $account_id_condition = ($account_id)?' AND avf.account_id = '.$account_id:NULL; $query="SELECT avt.file_id , cca.account_name , avf.file_name , avf.file_size, avf.play_time, vvc.company_name from vendor.vendor_fileallot vvf JOIN vendor.vendor_contacts vvc ON vvf.vendor_id = vvc.vendor_id JOIN admin.voice_filetrans avt ON avt.reference_id=vvf.allot_id JOIN admin.voice_filedetails avf USING(file_id) JOIN client.client_accounts cca ON cca.account_id = avf.account_id WHERE vvf.allot_date = '".$file_date."' ".$account_id_condition." AND avt.thead_id IN (1,2,4) ORDER BY cca.account_name , avf.file_name "; $arr = $this->returnQueryArray($query); return $arr; pls send me the sp for that

Posted by anad aramandla Jul 01, 2011

It is very useful article.

Posted by Datta Kharad May 29, 2011

It is very useful article.

Posted by Datta Kharad May 29, 2011

very good helpfull artical for beginners...

Posted by shane woz May 27, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.