Introduction
I will explain How to Create and Read Cookies in Asp.net.
Description
To read this example, Web Server users enter data. Cookies must create a file stored
on the user. When users enter information and click the button "Login" Property
"OnClick" to Run Procedure "bttLoing_Click" created in the language in which Lipts
used for file names Cookies "yourInfo" to collect information on these machines
because users. to Cookies "yourInfo" stored value greater than 1 and therefore need
to set to store all values such as Key Key "yourName" fee that users enter in TextBox
"txtUsername" and Key "yourlD" stored value from TextBox "txtPassword" then. All
stored values are sent to a user's command with "Response.Cookies.Add [myCookie].
To read the value from the Web Server Cookies "yourInfo" will need to click the
button "Read Cookies Value" to property "OnClick" the Button "bttGet" Run Procedure
"bttGet_Click" which create Object "getCookie" to read values from Cookies "yourInfo.
". (From command "getCookie = Request.Cookies [" yourInfo "] using the Property"
HasKeys "check before that Cookies" yourInfo "Key is not or if it will create Object"
valueCookie "from class" NameValueCollection "which uses archive Key. and the value
of each of Key and then to access each of the Blu-ray Disc Key in the name of the
Cookies will keep at each Key Object "keyTemp" and then use the command. getCookie.Values
[keyTemp] "show the value of each Key Key if no Cookies Cookies will show the value
of the show after all costs and then use the Key Label" lblSum "read a message from
the value of the Key" yourName "again
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to Creat and Read Cookies</title> </head> <body> <form id="form1" runat="server"> <div> <asp:label id="lblSum" runat="server"></asp:label> <br /> Username :<asp:textbox id="txtUsername" runat="server" /><br /> Password :<asp:textbox id="txtPassword" runat="server" textmode="Password" /><br /> <br /> <asp:button id="bttLogin" runat="server" text="Login" onclick="bttLogin_Click" /> <asp:button id="bttGet" runat="server" onclick="bttGet_Click" text="Read Cookies Value" /> </div> </form> </body> </html> |
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.Specialized; public partial class How_to_Creat_and_Read_Cookies_c : System.Web.UI.Page { protected void bttLogin_Click(object sender, EventArgs e) { HttpCookie myCookie = new HttpCookie("yourInfo"); myCookie.Values["yourName"] = txtUsername.Text; myCookie.Values["yourID"] = txtPassword.Text; Response.Cookies.Add(myCookie); } protected void bttGet_Click(object sender, EventArgs e) { HttpCookie getCookie; getCookie = Request.Cookies["yourInfo"]; if (getCookie.HasKeys) { NameValueCollection valueCookie = new NameValueCollection(getCookie.Values); foreach (string keyTemp in valueCookie.AllKeys) { Response.Write(keyTemp + " : "); Response.Write(getCookie.Values[keyTemp] + "<br />"); } } else { Response.Write(getCookie.Value); } lblSum.Text = "Welcome .. " + getCookie.Values["yourName"]; } } |
0 comments :
Post a Comment