none
ASP.NET Web Forms FAQ: Bagaimana cara menggunakan cookie? RRS feed

  • Diskusi Umum

  • Buat halaman bernama page1.aspx, dan kemudian drag tombol dan TextBox ke halaman tersebut. Double klik pada button dan kemudian tambahkan kode berikut:

    protected void Button1_Click(object sender, EventArgs e) 

        HttpCookie cookie = new HttpCookie("UserName"); 

        cookie.Value = TextBox1.Text; 

        cookie.Expires = DateTime.Now.AddDays(1); 

        Response.Cookies.Add(cookie); 

        Response.Redirect("Page2.aspx"); 

    }

     

    Berikut kode pada page1.aspx,

    <div> 
         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    </div> 

     

    Pada page2.aspx,  double klik pada form dan taruh kode berikut di dalamnya:

    protected void Page_Load(object sender, EventArgs e) 

    { 
        if (Request.Cookies["UserName"] != null) 
            Response.Write(Request.Cookies["UserName"].Value); 
    }
     
    Thread terkait: 

    http://forums.asp.net/t/1223291.aspx

    Asp.Net Cookies Overview

    How to: Read a Cookie

    How to: Delete a Cookie 

    ASP.NET State Management Overview

    ASP.NET State Management Recommendations

     


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

     

     




    Kamis, 11 Agustus 2011 03.22
    Moderator