More in this section

Forums / Developing with Sitefinity / Changing News View Pager Size

Changing News View Pager Size

11 posts, 0 answered
  1. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    13 Aug 2009
    Link to this post
    Hi all,

    I am having a problem with changing News View Pagers' size. I am using Sitefinity 3.7.
    I Have link "View all" in my NewsList.ascx, which I am using to show news in Sitefinity News View control.
    And I want to set the Pager Size (PagesRange Property) depending on having parameter view=1 in query string.
    If view = 1 then the PagesRange should be set to count of all news items - there will be only 1 page and all items will be visible.

    Please help me doing this. I am very short of time and out of ideas.

    Thank you very much!
    Best Nadya
  2. John
    John avatar
    32 posts
    Registered:
    08 Oct 2008
    13 Aug 2009
    Link to this post
    Hi,

    The Pager control has a load event, on the code-behind, and you can set the pages range programmatically:

    protected void Page_Load(object sender, EventArgs e) 
        { 
            pager1.Load += new EventHandler(pager1_Load); 
        } 
     
        void pager1_Load(object sender, EventArgs e) 
        { 
            if (Request.QueryString["View"] == "1"
            { 
                pager1.PagesRange = CountAllNews(); 
            } 
     
        } 
     
        /// <summary> 
        /// Gets a count of all news items 
        /// </summary> 
        /// <returns></returns> 
        public int CountAllNews() 
        { 
            NewsManager manager = new NewsManager("News"); 
            IList myList = manager.Content.GetContent(); 
            return myList.Count; 
        } 

    -John
  3. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    14 Aug 2009
    Link to this post
    Hi John,

    Thanks for your answer but unfortunately it is not working - I've tried all events from both pager and repeater but the PagesRange is awlays 10 at the end of the day.

    I see that this is a property that is set from News View controls - so somewhere may be exactly this property is overwriting my value, but I cannot find the place to do it after this property is set.
    This is the property from "Settings" :
     
    "Divide the list on pages if there are too many items

    Display items per page. "


    How can I overwrite it ? Where is the place to set PagesRange after this property and after I have the Request object so I can check for "vew=1" in QueryString ?

    I really need help,
    Thanks
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    14 Aug 2009
    Link to this post
    Hello Nadya,

    I assume that you have a custom control. If not, please create a control that inherits from NewsView. Override CreateChildControls method and set ItemsPerPage.

    ublic class CustomNewsView : NewsView 
        public CustomNewsView() 
        { 
        } 
     
        public override string ItemListTemplatePath 
        { 
            get { return "~/Sitefinity/ControlTemplates/News/ListPageMaster.ascx"; } 
        } 
     
        public override string SingleItemTemplatePath 
        { 
            get { return "~/Sitefinity/ControlTemplates/News/ListPageDetails.ascx"; } 
        } 
     
         
        protected override void CreateChildControls() 
        { 
             base.CreateChildControls(); 
     
             // Add your logic here 
             this.ItemsPerPage = 2; 
        } 
     
       ... 
          ... 

    Let us know if your approach is different.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    17 Aug 2009
    Link to this post
    Hi Ivan,
    Thanks for your suggestion, but unfortunatelly it is not working again.

    I have my custom class :

    public class CustomNewsView : NewsView  
        {          
            public CustomNewsView()  
            {  
            }  
     
            public override string ItemListTemplatePath  
            {  
                get { return "~/Templates/Controls/Internal/NewsList.ascx"; }  
            }  
     
            public override string SingleItemTemplatePath  
            {  
                get { return "~/Templates/Controls/Internal/NewsDetails.ascx"; }  
            }  
     
     
            protected override void CreateChildControls()  
            {  
                base.CreateChildControls();  
     
                  
                INewsListControl m_newsList = (INewsListControl)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Templates/Controls/Internal/NewsList.ascx"typeof(INewsListControl));  
     
                if (!m_newsList.Initializing)  
                {  
                    this.ItemsPerPage = m_newsList.ItemsPerPage;
                }  
            }  
     
        } 

    And in my NewsList I have the fowolling (to connect both I am using INewsList interface which has only two properties - ItemsPerPage and Initializing. I had to put the second one beacuse otherwise I was getting null reference exception when trying to use the m_newsList field):
     
    protected void Page_Load(object sender, EventArgs e)  
    {  
    ....  
    Initializing = false;  
    }   
     
    private bool ShowAll()  
        {  
            if (!(SJHelper.IsDesignMode || SJHelper.IsEditMode))  
            {  
                string strQuery = Request.QueryString["view"];  
                if (strQuery.IsNotNullOrEmpty())  
                {  
                    if (string.Equals(strQuery, "1"))  
                    {  
                        return true;  
                    }  
                }  
            }  
     
            return false;  
        }  
     
    ....
     #region INewsListControl Members  
     
        public int ItemsPerPage  
        {  
            get 
            {  
                if (ShowAll())  
                {  
                    Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News");  
                    IList listOfNewsItems = newsManager.Content.GetContent();  
                    this.Initializing = false;  
                    return listOfNewsItems.Count + 1;  
                }  
                else 
                {  
                    return 10;  
                }  
            }  
        }  
     
        public bool Initializing  
        {  
            set 
            {  
                ViewState["Initializing"] = value;  
            }  
            get 
            {  
                if (ViewState["Initializing"] == null)  
                {  
                    ViewState["Initializing"] = true;  
                }  
     
                return Convert.ToBoolean(ViewState["Initializing"].ToString());  
            }  
        }
        #endregion 

    Againg the pager's value is awlays 10.
    Did I got the idea wrong ?
    Thanks
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Aug 2009
    Link to this post
    Hello Nadya,

    As I see it your code always returns 10 if view is greater then 1.

    Your code:

    private bool ShowAll()   
        {   
            if (!(SJHelper.IsDesignMode || SJHelper.IsEditMode))   
            {   
                string strQuery = Request.QueryString["view"];   
                if (strQuery.IsNotNullOrEmpty())   
                {   
                    if (string.Equals(strQuery, "1"))   
                    {   
                        return true;   
                    }   
                }   
            }   
      
            return false;   
        }   
      
    .... 
     #region INewsListControl Members   
      
        public int ItemsPerPage   
        {   
            get  
            {   
                if (ShowAll())   
                {   
                    Telerik.News.NewsManager newsManager = new Telerik.News.NewsManager("News");   
                    IList listOfNewsItems = newsManager.Content.GetContent();   
                    this.Initializing = false;   
                    return listOfNewsItems.Count + 1;   
                }   
                else  
                {   
                    return 10;   
                }   
            }   
        }   

    instead of returning 10 you can add add the logic below:

    return Int32.Parse(Request.QueryString["view"]);  

    If you debug the code you should see that it goes into else statement.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    17 Aug 2009
    Link to this post
    Hi Ivan ,

    In my logic view is like a boolean variable - can be 1 for true or anything else for false , including the case when it is not there.
    If view = 1 then the PagesRange should be set to count of all news items - there will be only 1 page and all items will be visible.

    CreateChildControls() of my CustomNewsList is called before and after Page load (this is what debuging is showing) that is why I am using the boolean Initializing property. But still the value is  10.

    I think there's a problem here :

    protected override void CreateChildControls()     
    {     
        base.CreateChildControls();     
        
                     
        INewsListControl m_newsList = (INewsListControl)    System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Templates/Controls/Internal/NewsList.ascx"typeof(INewsListControl));     
        
        if (!m_newsList.Initializing)     
        {     
            this.ItemsPerPage = m_newsList.ItemsPerPage;   
        }     
    }     
     

    may be the m_newsList is not initialazing correct or may be the ItemsPerPage is set at some point further than CreateChildControls().

    Thanks again for the help , but can you review it again please .
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Aug 2009
    Link to this post
    Hi Nadya,

    Have you tried the suggested solution? Also when you debug did you reach CreateChildControls and what was the ItemsPerPage value?

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    17 Aug 2009
    Link to this post
    Hi Ivan,

    Yes I tried it. If I am not using the Initializing property I am getting NullReference exception in the m_newsList variable.

    When I am using it and it reaches to the row where I set the ItemsPerPage it takes the right value from the NewsList 's ItemsPerPage Property, but then it is again 10.

    I believe the bool property is correct here - the NewsList should be initialized when I try to take property's value - may be I am not using it at the right place ?

    Or maybe my logic of connecting the NewsList.ascx and CustomNewsView is not correct ?

    I really don't understand why it is not working.

    Thanks
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Aug 2009
    Link to this post
    Hi Nadya,

    Give a try to the sample below:

    public class CustomNewsView : NewsView 
        public CustomNewsView() 
        { 
        } 
     
        protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e); 
     
            string strQuery = this.Page.Request.QueryString["view"]; 
            if (!String.IsNullOrEmpty(strQuery) && string.Equals(strQuery, "1")) 
            { 
                // when false all items will be listed. 
                this.AllowPaging = false
            } 
            else 
            { 
                // default valule 
                this.ItemsPerPage = 4; 
                this.AllowPaging = true
            } 
            this.ChildControlsCreated = false
        } 
     


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  11. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    19 Aug 2009
    Link to this post
    Hi Ivan,

    It is finally working !
    Thank you very very much for your assistance !

    Wish you all the best,
    Nadya
Register for webinar
11 posts, 0 answered