More in this section

Forums / Developing with Sitefinity / Getting the control GUID on a page

Getting the control GUID on a page

11 posts, 0 answered
  1. Bill
    Bill avatar
    15 posts
    Registered:
    04 Nov 2008
    04 Nov 2008
    Link to this post
    I have a user control that I created.  I assume each time it is dropped onto a page, the instance of the control is given a unique id(GUID).  Within the control code I need to get that ID.
    Scenario:
    I created a control, let's call it Location.
    I created 2 pages, let's call them Home and Office.
    Page 1(Home) has the Location control on it.
    Page 2(Office) also has the Location control on it.
    If page 1(Home) is loaded up and the control is called, from within the control's code I need to get the unique id for the control on the Home page.
    Thanks for your help!
  2. Bill
    Bill avatar
    15 posts
    Registered:
    04 Nov 2008
    04 Nov 2008
    Link to this post
    Essentially, when your control is loading, getting the ID of that current control.
  3. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    04 Nov 2008
    Link to this post
    Jumping off of this thread a little bit ~ I am guessing Sitefinity is loading the controls that we create into its CmsWebControl class. I noticed the base class of CmsWebControl is the WebControlBase class (which contains the ID or GUID stored in Sitefinity I am assuming). Is there a way to programatically retrieve that ID from within our web controls through some form of context at run time?
  4. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    04 Nov 2008
    Link to this post
    I noticed that Sitefinity is doing something similar where it is generating querystring variables containing control guids within a URL that is posting to a page that is loading data specific to that control (this is exactly what I would like to do).
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Nov 2008
    Link to this post
    Hello Bill,

    Thank you for contacting us.

    The control ID should be a string. Could you tell us a bit more about your approach? Also, you can use LoadControl method to load a control on the page. Check out this implementation for Intra-Site modules

    I hope this helps.





    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Bill
    Bill avatar
    15 posts
    Registered:
    04 Nov 2008
    05 Nov 2008
    Link to this post
    If I am in the overridden CreateChildControls of a Web User Control I would like to get at the unique id of that Web User Control.  I am pretty sure it is a GUID.  I can see it in the sf_Web_Control_Base table as well as if I "View Source" on the page as it is rendered(see below).  For each instance of this control within Sitefinity I will have an associated XML file that I plan to prefix with the id of the specific control instance as I need to keep that relationship.
    Below is a snippit of the page source:
    href="/sitefinity/admin/Themes/Default/Skins/Tabstrip/SitefinityNews/styles.css" /></head>
    <body>
        <form name="form1" method="post" action="CtrlProps.aspx?sfctrlid=75141a5e-0a9d-4943-91e1-b476ce620101&amp;sfdefclid=usercontrols_webeditr_location_locator_control_ascx1&amp;sfpageid=5e759dcd-61ab-4ac8-9be2-188ba21ff118" id="form1">
    <div>
    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Nov 2008
    Link to this post
    Hello Bill,

    Below is the desired code that will return the ID for the control on a page.

        protected void Page_Load(object sender, EventArgs e) 
        { 
            Guid cmsControlId = GetCmsControlId("~/UserControls/Location.ascx"); 
        } 
     
        private Guid GetCmsControlId(string controlPath) 
        { 
            CmsPageBase page = this.Page as CmsPageBase; 
            if (page != null
            { 
                ICmsPage cmsPage = page.CmsPage; 
                foreach (ICmsWebControl cmsControl in cmsPage.Controls) 
                { 
                    if (cmsControl.IsAscx 
                        && cmsControl.TypeName.Equals(controlPath, StringComparison.OrdinalIgnoreCase)) 
                    { 
                        return cmsControl.ID; 
                    } 
                } 
            } 
            return Guid.Empty; 
        } 
     

    I hope this helps.


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    05 Nov 2008
    Link to this post
    The problem is we have a second page that needs to load content based on the properties of this control. The idea is the control will dynamically generate a link that when clicked will load a page that will render data specific to that control on a second page. We need to pass information unique to this specific control to that page ~ an alternative would be to pass the property information of the control through a session or querystring variable, but ideally we could just pass the unique control id (stored in the Sitefinity database as a guid).

    I do not see a way to access a controls property values in the database outside of querying the properties related to a specific control ID guid. I am assuming if we do not specify an ID/name string on the control one is automatically generated by Sitefinity... but how does that help us retrieve the property values on a second page?

    I feel I am missing something really simple here, so please be patient with me :).
  9. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    05 Nov 2008
    Link to this post
    Thank you very much for the sample code (will save for future use). Your sample suggests to me that I could make modifications at the page level, check for specific controls, then pass that value into the control so it could display the link how we like... maybe? Ideally this could be done from within the control through some type of run time context and not on the page level (if possible).
  10. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    05 Nov 2008
    Link to this post
    One last point of clarification :). Me and Bill are working on a map control that uses AJAX to load point data dynamically (so if a change is made in our locations module it updates the map on the fly). We recently modified the control to allow people to specify specific locations from the locations module we built (a filter, so they could have a map on one page that displayed a single location and a map on another page that showed all locations etc.). This requires seperate XML feeds to be delivered to each control. We would like to build a single page external from the control that renders XML for AJAX purposes based on the property values of the control running the map. This requires the XML page (aspx) to have knowledge of which control the request is coming from so it can filter the point data accordingly. The control, when loaded, should specify a link to the XML page with a querystring value specifying the guid (so we can retrieve the properties from the Sitefinity database and display the correct data). I would prefer not to put this on the page level or to use sessions, but if it's the only way I am sure we can make it work.
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    11 Nov 2008
    Link to this post
    Hello Joel,

    Please take a look at the example below. Here, we get the list to pages containing the control and then the ID of the control.

    CmsManager cmsManager = new CmsManager(); 
    IList cmsControls = cmsManager.GetControls("~/UserControls/Location.ascx"true); 
    foreach (ICmsWebControl control in cmsControls) 
        // Add your logic here 
        // For example 
        // How to get a list of pages which contain the specified control  
        IList<IPage> pages = control.GetPages(); 
        // How to get the ID of the control 
        Guid controlGuid = control.ID; 
     


    I hope this helps.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
11 posts, 0 answered