Here i will show how to set and get the cookie value.Cookies are used to store the small size of data in browser.The below example gives , how perform manipulate functions on cookies .In my application i have three page and each page having two image buttons.When ever click on the image button ,it redirect to next page.In this process we will also pass the value of cookie.
c#:
c#:
protected void imgpinksubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e) { SetCookie("p"); Response.Redirect("test.aspx"); } protected void Imgbluesubmit_Click(object sender, System.Web.UI.ImageClickEventArgs e) { SetCookie("b"); Response.Redirect("test1.aspx"); } private void SetCookie(string c) { if (Request.Cookies("Colors") == null) { Request.Cookies.Add(new HttpCookie("Colors", "")); } int pageIndex = 1; string colorCode = Request.Cookies("Colors").Value; if (!string.IsNullOrEmpty(colorCode)) { if (colorCode.Contains("1," + c)) { } else { string[] colors = colorCode.Split('|'); if (colors.Length >= pageIndex) { colors[pageIndex - 1] = pageIndex + "," + c; string updatedColorcode = string.Empty; for (int i = 0; i <= colors.Length - 1; i++) { updatedColorcode += colors[i] + "|"; } Response.Cookies("Colors").Value = updatedColorcode.TrimEnd('|'); } } } else { Response.Cookies("Colors").Value = "1," + c; } }vb#:
Protected Sub imgpinksubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgpinksubmit.Click SetCookie("p") Response.Redirect("test.aspx") End Sub Protected Sub Imgbluesubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Imgbluesubmit.Click SetCookie("b") Response.Redirect("test1.aspx") End Sub Private Sub SetCookie(ByVal c As String) If Request.Cookies("Colors") Is Nothing Then Request.Cookies.Add(New HttpCookie("Colors", "")) End If Dim pageIndex As Integer = 1 Dim colorCode As String = Request.Cookies("Colors").Value If Not String.IsNullOrEmpty(colorCode) Then If colorCode.Contains("1," & c) Then Else Dim colors As String() = colorCode.Split("|"c) If colors.Length >= pageIndex Then colors(pageIndex - 1) = pageIndex & "," & c Dim updatedColorcode As String = String.Empty For i As Integer = 0 To colors.Length - 1 updatedColorcode += colors(i) & "|" Next Response.Cookies("Colors").Value = updatedColorcode.TrimEnd("|"c) End If End If Else Response.Cookies("Colors").Value = "1," & c End If End Sub
No comments:
Post a Comment