Sunday 20 April 2014

Transfer data from one web page to another using cookie collection

Accept ItemId, Name and Quantity and store them in cookie collection and show them in next form.

Explanation:- First create two web page in asp.net with name of  Webform1 or WebForm2 ,  where you have to drop some asp.net control to accept  and display the input./output as per as the program requirement  (like TextBox, Label, Button etc) and then finally write down the the  code in the code behind  file respectively. 


WebForm1.aspx.cs

using System;
using System.Web;
using System.Web.UI;

namespace Architecture
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }        
        protected void Button1_Click(object sender, EventArgs e)
        {
            int itemid = Convert.ToInt32(TextBox1.Text);
            string itemname = TextBox2.Text;
            int qty = Convert.ToInt32(TextBox3.Text);

            HttpCookie cookies = new HttpCookie("productdetails");
            cookies["itemid"] = Convert.ToString(itemid);
            cookies["itemname"] = itemname;
            cookies["qty"] =Convert.ToString(qty);

            cookies.Expires = DateTime.Now.AddHours(1);
            Response.Cookies.Add(cookies);

           Response.Redirect("WebForm2.aspx");
           }
    }
}

WebForm2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Architecture
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookies = Request.Cookies["productdetails"];
            if (cookies != null)
            {
                Label1.Text = cookies["itemid"];
                Label2.Text = cookies["itemname"];
                Label3.Text = cookies["qty"];
            }
        }
    }
}
If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)

No comments:

Post a Comment