More in this section

Forums / Developing with Sitefinity / Displaying Filtered by: in NewsView (or blogs or events, etc)

Displaying Filtered by: in NewsView (or blogs or events, etc)

4 posts, 1 answered
  1. jose pacheco
    jose pacheco avatar
    4 posts
    Registered:
    29 Dec 2009
    27 Jan 2010
    Link to this post
    I have news view controls that display news articles and I have them set to display what they have been tagged with and their categories. (pretty standard behavior) When I click the category, it reloads the page and displays articles that are only of that category/tag inside of that newsview(unless i point it to a different page, in which case it will load that same information inside the newsview of that page). 

    I have updated the itemlist template to have a header that says "Our News."  I would like to be able to have it say "Our News" if the newscategory has not been selected (it currently displays all articles) but if someone is filtering on a category, I would like it to say "Our Business News" or "Our International News"  I have searched and have not seen anyone make this request.

    I have an idea, but I woudl like to know if it's the best approach at doing so.

    My idea:
    In the template being used; add:

    Our <asp:literal... id="CategoryFilteredBy" /> News

    Then extend the newsview control.  in there I would use the newsmanager class (or whatever class i need to use) to get the category name via guid in the querystring (if tehre is no gui, then display nothing there, since it will be all articles) and then use Page.FindControl("CategoryFilteredBy") to get the control and set it's text to that title.

    Is there a built in way to do this already?   Does my approach sound reasonable or would you recommend a different way?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jan 2010
    Link to this post
    Hi jose pacheco,

    You have to create a custom control that inherits from NewsView. In CreateChildControl read the querystring that is appended to the News item title. Generally you may use the key which by default is NewsCatID. The value is the name of the category. Then you can dynamically switch the title of a lable or another control. For instance if the category name is Bussines, you can easily get it from the querystring and then change the label to Our Bussines News.

    sample code

    CreateChildControls

    if (!String.IsNullOrEmpty(Context.Request[CategoryKey]))
                      string categoryName = string.Empty;
                        ICategory parent = null;
                        if (CategoryKeyType == CategoryKeyTypes.ID)
                        {
                            if (IsGuid(Context.Request.QueryString[CategoryKey]))
                            {
                                parent = Manager.GetCategory(new Guid(Context.Request.QueryString[CategoryKey]));
                                if (parent!= null)
                                {
                                    categoryName = parent.CategoryName;
                                    label.Text = "Our" + categryName + "news";
                                   
                                }
                            }
                         }


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  3. jose pacheco
    jose pacheco avatar
    4 posts
    Registered:
    29 Dec 2009
    27 Jan 2010
    Link to this post
    Thanks for the quick response.  Another quick, related question.

    I am new to this concept of having an external template for a control to render the markup. If I want to accomplish this would I do this:

    Template:
    Our <asp:literal... id="label" /> News

    AND

    Code:
    private Literal label;

    and then in my codebehind when I call label.Text = "blah" would it know to render that literal where the template is telling it to be located (between the "our" and "news") or would I have to call Page.Controls.Add(label) in order for it to display (which would probably render it at the very top of the control and defeat the purpose, but I could put it in a placeholder control or something) . I ask because in your example you didnt call

    Literal label = (literal)Page.FindControl("label");
    label.Text = "blah"

    you assumed it was already a variable for the extended control that was to be created.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jan 2010
    Link to this post
    Hello jose pacheco,

    Yes, you can do it in the template code behind as well or in a custom control that inherits from NewsView. Directly with the template will be more easy.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
4 posts, 1 answered