Examples to work With Cookes:-

Example1:-

Create two WebPages Source.aspx & Target.Aspx

Design Source.aspx by taking a Button



Set the following Properties for the Button1
ID                  ---        btnSubmit
Name             ---        Submit
Goto Source and write the Following Code in btnSubmit Click Event:-

protected void btnSubmit_Click(object sender, EventArgs e)
{
        HttpCookie obj1 = new HttpCookie("a");
        HttpCookie obj2 = new HttpCookie("b");
        HttpCookie obj3 = new HttpCookie("UName");
        obj1.Value = "99";
        obj2.Value = "99.9";
        obj3.Value = "sampath";
        Response.Cookies.Add(obj1);
        Response.Cookies.Add(obj2);
        Response.Cookies.Add(obj3);
        Response.Redirect("Target.aspx");
 }

         
Design Target.aspx by taking 3 TextBoxes Like
 
Set the following Properties for the TextBox1


            ID                    ---        txtSample1

Set the following Properties for the TextBox2


            ID                    ---        txtSample2
                                                            
 Set the following Properties for the TextBox3

           ID                    ---        txtUName

Goto Source and write the following code in Page_Load Event

protected void Page_Load(object sender, EventArgs e)
    {
        txtSample1.Text = Request.Cookies["a"].Value;
        txtSample2.Text = Request.Cookies["b"].Value;
        txtUName.Text = Request.Cookies["UName"].Value;
    }


Example2:-

  • Create Two WebPages Default2.aspx,Defaut3.aspx
  • Design Default2.aspx by taking a Textbox & a Button like





Set the following Properties for theTextBox1
ID                    ---        txtEmpId
Set the following Properties for the Button1
ID                  ---        btnSubmit
Name             ---        Submit
Goto Source and write the Following Code in btnSubmit Click Event:-
                   
protected void btnSubmit_Click(object sender, EventArgs e)
{
        HttpCookie obj1 = new HttpCookie("EmpId");
        obj1.Value = txtEmpId.Text;
        Response.Cookies.Add(obj1);
        Response.Redirect("Default3.aspx");
}


Design Default3.aspx by taking a GridView like

Run the Application & Check

No comments:

Post a Comment