Get Content ID by Control on Page

Posted by Community Admin on 04-Aug-2018 23:31

Get Content ID by Control on Page

All Replies

Posted by Community Admin on 28-Jul-2011 00:00

I have a page that contains a news module widget that is set to show a single news item.  I am trying to figure out how to get the id of the selected news Item that is showing in the control.  Code to get news item control is below:

var siteMapNode = SiteMapBase.GetActualCurrentNode();
            if (siteMapNode != null)
                            
                PageManager mng = PageManager.GetManager();                              
                var pageNode = mng.GetPageNode(siteMapNode.Id);
                var pageData = mng.GetPageData(siteMapNode.PageId);              
                List<PageControl> list = pageData.Controls.Where(w => w.IsLayoutControl == false & w.Caption == "News").ToList();
                var drafts = pageData.Drafts;                
                foreach (PageControl c in list)
                        //TODO: find the ID of the news item related to the control....
                
  

Am I going about it the wrong way to get the ID by trying to determine it through the control? 

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

Hi Marc,

This can be done using the bellow approach (used in our widgets to determine the current item):

private NewsItem currentItem;
public NewsItem CurrentItem
    get
    
        return this.currentItem;
    
    set
    
        this.currentItem = value;
    
 
protected void Page_Load(object sender, EventArgs e)
    this.ResolveDetailItemFromUrl();
 
/// <summary>
/// Resolves the detail item from URL.
/// </summary>
protected virtual void ResolveDetailItemFromUrl()
    var itemUrl = this.GetUrlParameterString(true);
    if (itemUrl != null)
    
        string redirectUrl;
 
        var manager = NewsManager.GetManager();
        var item = (NewsItem)manager.GetItemFromUrl(typeof(NewsItem), itemUrl, true, out redirectUrl);
        if (item != null)
        
            this.CurrentItem = item;
        
    


Best wishes,
Radoslav Georgiev
the Telerik team
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 26-Aug-2011 00:00

So if I am trying to encapsulate this logic into a widget that can be extended to all pages by inheriting from simpleview how do I get to the GetUrlParameterString method?  For example, I create a widget called MyWidget extend SimpleView and then on the SelectedIndexChanged event of the drop down list I want to get the newsitem using your suggested technique so I can see if there is a draft version that is newer. When I try to use the this.GetUrlParameterString(true) method the method is not found. Thoughts of what I may be missing?

Marc

Posted by Community Admin on 31-Aug-2011 00:00

Hi Marc,

This method is an extension method. Please add a reference to the namespace System.Web.UI to get access to it (the sample I have used is for user control type).

Greetings,
Radoslav Georgiev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

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

Sorry for bumping an older thread, but how would this work in an MVC widget? Especially that this.GetUrlParameterString(true); line

This thread is closed