mandag den 14. december 2009

Cookies in ASP.NET

I always find myself struggling with Cookies in asp.net.

I always fail to add the cookies the right way or to remove them.
I always tend to open old projects to see how I coped then.
But most of the project are filled with different tries to get the cookie stuff to work so it doesnt help much.

There are three issues that we are dealing with

1. Adding a cookie that never expires
2. Adding a cookie that expires with a day or so
3. Removing a cookie

1 + 2. The first mistake I make is trying to add a cookie to the Request.Cookie collection which obviously doesnt work due to the fact it is a collection comming from the server.

All cookie should be added to the Response.Cookie collection.
For adding expiration you just set the Expire property on the HttpCollection before adding the cookie. For a cookie that never expires you set Expire = DateTime.MaxValue;

3. Removing a cookie is not done "just" by removing the cookie from the request cookie collection. You also need to add a new cookie with the same name as the one you want to remove and setting Expire to yesterday to Response.Cookie collection . This forces the cookie to expire the next time the client requests the page.

 HttpCookie c = new HttpCookie(CookieName, "false");  
 c.Expires = DateTime.Now.AddDays(-1);  
 Response.Cookies.Add(c);  
 Request.Cookies.Remove(CookieName);   

Very simple but a hell of a job if you forget just one of them.

onsdag den 9. december 2009

Changing control names on postback

I recently spent a whole day figuring out why my UserControl continuesly displayed inline errormessages instead of the validation summary I did tell it to use.

After af lot of investigation I found out that the controls in my UserControl did have changing control names upon postback due to inserted into sitecore placeholder.

I have failed to reproduce this scenario in a controlled matter but did find a fix to the problem.

Find the control that changes name upon postback and use this implementation of the sitecore placeholder.


 public class SCPlaceholder : Sitecore.Web.UI.WebControls.Placeholder  
   {  
     private List UsedKeys = System.Web.HttpContext.Current.Items["pckeys"] as List;  
     public SCPlaceholder()  
     {  
       if (UsedKeys == null)  
       {  
         UsedKeys = new List();  
         System.Web.HttpContext.Current.Items["pckeys"] = UsedKeys;  
       }  
     }  
     protected override void AddedControl(System.Web.UI.Control control, int index)  
     {  
       Sublayout s = control as Sublayout;  
       if (s != null && s.Controls.Count == 1)  
       {  
         string name = this.Key + "_" + s.Controls[0].GetType().Name;  
         if (UsedKeys.Contains(name))  
         {  
           for (int i = 0; i < 5; i++)  
           {  
             if (!UsedKeys.Contains(name + "_" + i))  
               name += "_" + i;  
           }  
         }  
         s.ID = name + "_sub";  
         s.Controls[0].ID = name;  
         UsedKeys.Add(name);  
       }  
       base.AddedControl(control, index);  
     }  
   }   

Experiences on how to reproduce this in a safe and clean environment is appreciated.