More in this section

Forums / Developing with Sitefinity / Determine ordinal position when usercontrol loads

Determine ordinal position when usercontrol loads

5 posts, 0 answered
  1. amckell
    amckell avatar
    4 posts
    Registered:
    19 Jul 2007
    17 Jan 2008
    Link to this post
    Hi,

    I have a need to toggle a usercontrol's background color, based on the control's ordinal position in a contentplaceholder.  In the usercontrol's OnLoad event I need to figure out its position.  I've managed to get the collection of controls for the page my usercontrol is on, but I'm having difficulty figuring out which control is the exact one I'm looking for.  I've attempted looking into the CmsWebControl.Properties collection, and I did find an "ID" property to which I could compare, but that "ID" property doesn't seem to exist on all controls - confusing. 

    Any thoughts on how I could accomplish this?  The Sitefinity API documentation provides very little, if any, help.

    Thanks in advance.
  2. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    17 Jan 2008
    Link to this post

    Hi amckell,

    I haven't done specifically what you are describing, but I did create a function which checks if a given Sitefinity Page contains a "type" of control. 

    By altering this code you might be able to accomplish your own goal.  Here it is:

        protected void Page_Prerender(object sender, EventArgs e)  
        {  
            CmsSiteMapNode node = (CmsSiteMapNode)SiteMap.CurrentNode;  
            if (HasControl(node.CmsPage.Controls) == true)  
            {  
                Response.Write("YAY!!! The HasControl() is true!!!  YAY!!!");  
            }  
     
        }  
     
     
        private bool HasControl(System.Collections.Generic.IList<ICmsWebControl> Controls)  
        {  
            bool found = false;  
     
            try 
            {  
                foreach (ICmsWebControl control in Controls)  
                {  
                    // If this page contains a "Document" child control, then open the link in a new window.  
                    if (control.ControlType.ToString() == "ASP.goondocks_usercontrols_document_ascx")  
                    {  
                        found = true;  
                        break;  
                    }  
                }  
            }  
            catch 
            {  
     
            }  
     
            return found;  
        }  
     


    The "ASP.goondocks_usercontrols_document_ascx" is just a string corresponding to the control type that I am looking for.  I'm using this little function to determine if a Sitefinity page contains a "Document" control. 

    My guess is that, instead of looking at the "control.ControlType" you could instead look at "control.ID".   ????

    I'm not really sure.  This post may or may not help you.  Please share your code with us if you find a solution.  Take care.

    Gabe Sumner
    http://www.goondocks.com/

  3. amckell
    amckell avatar
    4 posts
    Registered:
    19 Jul 2007
    22 Jan 2008
    Link to this post
    Gabe,

    Thanks for your input.  I was going down a similar path as you were following.  I get all controls on a cmspage and then iterate through the collection, checking each control.ID for a match.  That does work, with one major caveat:  Sitefinity does not require the ID of a control to be unique, nor does it autogenerate different ID's for each instance of a control in a contentplaceholder.  I'll explain:  When I drop 3 PromoContent controls in my contentplaceholder, they all get the same ID:  "controls_promo_promobox_ascx1."  Now, when asp.net renders each of these controls, their ClientIDs are unique, and thus the page renders properly.  However, when I iterate through a collection of controls that are the exact same type, I'll always get the 1st one.  I've submitted a bug to Telerik because of this.  I told them I cannot rely on a user, who is loading content, to give each control a unique ID.  If Sitefinity generated a unique ID for each control instance, like I believe it should, I'd be much better off.

    amckell
  4. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    22 Jan 2008
    Link to this post
    This sounds similar to a problem I was having once, although I can't seem to find the "forum thread" relating to it.

    If I recall, Telerik released a patch that took care of the problem with control ID's being duplicated or not set. 

    Are you using the latest version of Sitefinity with the latest patch?

    Gabe
    ===============
  5. amckell
    amckell avatar
    4 posts
    Registered:
    19 Jul 2007
    22 Jan 2008
    Link to this post
    Gabe,

    Our Telerik.Cms assembly is versioned "3.1.1458.2."  Not sure if we're at SP2, but my fellow devs do not believe we are.  So, we're going to upgrade today, and I'll post back and let you know if my issue gets resolved.

    amckell
Register for webinar
5 posts, 0 answered