+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get Content ID by Control on Page

Get Content ID by Control on Page

5 posts, 0 answered
  1. Marc
    Marc avatar
    24 posts
    Registered:
    09 Jun 2011
    28 Jul 2011
    Link to this post

    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? 

  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    01 Aug 2011
    Link to this post
    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
  3. Marc
    Marc avatar
    24 posts
    Registered:
    09 Jun 2011
    26 Aug 2011
    Link to this post
    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
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    31 Aug 2011
    Link to this post
    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 >>

  5. Bart
    Bart avatar
    1 posts
    Registered:
    02 May 2013
    19 Aug 2013
    Link to this post
    Sorry for bumping an older thread, but how would this work in an MVC widget? Especially that this.GetUrlParameterString(true); line
5 posts, 0 answered