Search code examples
c#asp.netajaxcookiesviewstate

Cookies viewstate are not maintaining in AjaxControlToolkit


I have cookies and viewstate in below control . This ajax Control is used to upload multiple file images.

protected void OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {           
        int userid = 25;
        DAL_Cart objdalcart = new DAL_Cart();
        if (Viewstate["Imagestringname"] == null)
        {
            objdalcart.InsertTempImage(userid, ImageName, 1);
        }
        else
        {
            objdalcart.InsertTempImage(userid, ImageName, 0);
        }
        Response.Cookies["JewelleryUserCookiesUserId"].Value = Convert.ToString(userid);
        Response.Cookies["JewelleryUserCookiesUserId"].Expires = DateTime.Now.AddYears(1);
        Viewstate["Imagestringname"] = ImageName + ",";
    }

The issue is when I try to retrive view state value or Cookies value on different click event of button in same page I am not able to retrive the value

 protected void lnkcheckout_Click(object sender, EventArgs e)
    {
      if (Request.Cookies["JewelleryUserCookiesUserId"] == null || Request.Cookies["JewelleryUserCookiesUserId"].Value == "")
            {
             }
    if (Viewstate["Imagestringname"] != null)
        {}
     }

For both the case it is going in if condition. for viewstate I have placed Enableviewstate=true on master page .Any idea why?

Review

Want ajax multiple file upload on my button click event


Solution

  • var c = new HttpCookie("JewelleryUserCookiesUserId");
    c.Value =  Convert.ToString(userid);
    c.Expires = DateTime.Now.AddYears(1);
    
    Response.Cookies.Add(c);
    

    Just note: this is insecure. the client can manipualte the cookie...