Get custom control id from control's code-behind

Posted by Community Admin on 04-Aug-2018 15:05

Get custom control id from control's code-behind

All Replies

Posted by Community Admin on 01-Aug-2013 00:00

Howdy folks!  I'm having a problem that is driving me nuts and I'm hoping someone here might have a solution.  I'm making several custom web-forms controls in a Sitefinity project and so far most things seem to be working correctly.  I have been able to add them as widgets, and I have been able to edit their public properties in the Sitefinity page edit-view no problem.  When I edit these custom properties, they are correctly saved to the sf_control_properties table in my server (see screenshot1 and screenshot2).  So far so good...however I am having a peculiar issue with programatically accessing the auto-generated control_id field in my control's code behind.  As you can see in screenshot1:  there is a unique control_id that refers to a particular instance of widget on a page.  I would very much like to use that control_id to uniquely associate my own collection of items with a particular control.  Effectively what I'm looking for is something like screenshot3.  I want to be able to call a method on the code behind that will return the current control_id. 

It seems like this should be something extremely easy to do, but it has me completely stumped.  I have tried searching through documentation and also entering a help ticket, but haven't had anything relevant turn up. 

Posted by Community Admin on 02-Aug-2013 00:00

Hi,

The sitefinity page will assign the ID on the control when its placed on one of the placeholder in the page, refer to this documentation to work with control on page. After retrieving the page get its control and find the current control type. To determine at which page is the control placed execute this code

var currentPage = SiteMapBase.GetActualCurrentNode().Id;
            var pageManager = PageManager.GetManager();
            var currentPageNode = pageManager.GetPageNode(currentPage);
            //retreive the page
           var pages = App.WorkWith().Pages().LocatedIn(PageLocation.Frontend).Where(pt => pt.Id.Equals(currentPageNode.Id)).Get().FirstOrDefault();
            //get the page data where controls are stored
           var pageData = pages.Page;
           var tempPage = pageManager.EditPage(pageData.Id);
            //find particulat control by its name
           var uControls = tempPage.Controls.Where(c => c.Caption == "WebUserControl1").FirstOrDefault();
            //get the control id saved in sf_control_peroperts
           var control_id = uControls.Id;


Regards,
Stanislav Velikov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 02-Aug-2013 00:00

Just to confirm your suggestion:  are you saying that inside of the control's code behind, I should get the current control's page information and then drill back down again to the control?  That seems...a bit round-about.  I'll give it a shot and see what happens.  Thanks for the response!

Posted by Community Admin on 02-Aug-2013 00:00

Alright, so this gets really close to the solution, but I am still unable to automatically get the control_id for a particular control.  This is what I have so far:

public Guid CurrentControlId()
    var currentPage = SiteMapBase.GetActualCurrentNode().Id;
    var pageManager = PageManager.GetManager();
    var currentPageNode = pageManager.GetPageNode(currentPage);
 
    var pages = App.WorkWith().Pages().LocatedIn(PageLocation.Frontend).Where(pt => pt.Id.Equals(currentPageNode.Id)).Get().FirstOrDefault();
 
    var pageData = pages.Page;
 
    var theseControls = pageData.Controls.Where(x => x.Caption == "Control Name Here");
    var thisControl = theseControls.FirstOrDefault();
 
    return thisControl.Id;

This will return the control_id of the FIRST one of these controls on the page, but not the control_id of the CURRENT control.  In other words, if I have 3 controls that all have the caption "CustomControl" then this will retrieve the same control_id for all of them.  That's not specific enough for me, since I am loading unique content to each of those controls.  Any thoughts on how to only get the control_id for that specific custom control?

I'm attaching a sample screenshot so you can see what I mean.

Posted by Community Admin on 07-Aug-2013 00:00

Hi,

I have changed the query to query the control by their type so it will return all controls of the mentioned type and after this iterate over the retrieved controls of the specified type to get their ID, input the type of the control in the highlighted part of the query.

var currentPage = SiteMapBase.GetActualCurrentNode().Id;
var pageManager = PageManager.GetManager();
var currentPageNode = pageManager.GetPageNode(currentPage);
//retreive the page
var pages = App.WorkWith().Pages().LocatedIn(PageLocation.Frontend).Where(pt => pt.Id.Equals(currentPageNode.Id)).Get().FirstOrDefault();
//get the page data where controls are stored
var pageData = pages.Page;
var tempPage = pageManager.EditPage(pageData.Id);
//find particulat control by its type
var uControls = tempPage.Controls.Where(c => c.ObjectType.StartsWith(typeof(ContentBlock).FullName)
//loop trough all controls
foreach (var controls in uControls)
    var control_id = controls.Id;

Regards,
Stanislav Velikov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 05-Jan-2016 00:00

I came up with a similar solution, but I have fewer steps and I'm not sure if the steps I've omitted are necessary. Specifically, I see that in your example you:

 

1) First lookup the page node GUID by calling SiteMapBase.GetActualCurrentNode().Id, but then you look it up again by retrieving the PageNode, and then you pass in currentPageNode.Id to retrieve the same PageNode again... why the repeated querying to retrieve the same data (the page node GUID)? Or is it different and I'm not picking up on the differences?

2) Once you've got the PageData object, you edit the page, creating a draft page, and iterate over the controls. Why don't you simply look in the PageData.Controls collection? Why create a draft page? Creating a draft page (i.e., calling pageManager.EditPage()) won't work while the page is locked, by the way. You'll get an exception.

You can see what I did in my thread. Is there some situation I'm not covering for by eliminating the steps mentioned above?

Posted by Community Admin on 11-Jan-2016 00:00

Hi,

Thank you Joshua, its a good resource.

Regards,
Stanislav Velikov
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed