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.

Ingen kommentarer:

Send en kommentar