Creating the master list view

You use the master list view to display the list of published items on the website. To create the master list view, perform the following:

  1. Create the template for the master list view. Perform the following:

    • In the context menu of folder Web » UI » Public » Resources, click Add » New Item...
    • From the dialog click Visual C# » Code » Code File.
    • In the Name input field, enter MasterListView.ascx and click Add.
    • Open the newly created file and clear its content.
    1. Define the markup by pasting the following code:

      <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
      <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
      <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
       
      <h1>Locations</h1>
       
      <telerik:RadListView ID="LocationsList" ItemPlaceholderID="ItemsContainer" GroupPlaceholderID="GroupContainer" GroupItemCount="3" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
          <LayoutTemplate>
              <div class="Locations">
                  <asp:PlaceHolder ID="GroupContainer" runat="server" />
              </div>
          </LayoutTemplate>
          <GroupTemplate>
              <div class="row">
                  <asp:PlaceHolder ID="ItemsContainer" runat="server" />
              </div>
          </GroupTemplate>
          <ItemTemplate>
              <div class="column-small">
                  <div class="block">
                      <p><sf:DetailsViewHyperLink ID="DetailsViewHyperLink1" runat="server"><div id="photoDiv" runat="server"></div></sf:DetailsViewHyperLink></p>
                      <h3><sf:DetailsViewHyperLink ID="DetailsViewHyperLink" TextDataField="Title" ToolTipDataField="Description" runat="server" /></h3>
                      <address>
                          <sf:FieldListView ID="Address" runat="server" Text="{0}" Properties="Address" /><br />
                           
                          <sf:FieldListView ID="City" runat="server" Text="{0}" Properties="City" />
                          <sf:FieldListView ID="Region" runat="server" Text="{0}" Properties="Region" />
                          <sf:FieldListView ID="PostalCode" runat="server" Text="{0}" Properties="PostalCode" />
                          <sf:FieldListView ID="Country" runat="server" Text="{0}" Properties="Country" />
                      </address>
                       
                  </div>
              </div>
          </ItemTemplate>
      </telerik:RadListView>
       
      <sf:Pager id="pager" runat="server" />
    2. In the Properties pane of the MasterListView.ascx file, change the Build Action to Embedded Resource.

  • From the context menu of folder Web » UI » Public, click Add » Class...
  • In the Name input field, enter MasterListView.
  1. Open the file in Visual Studio and add the following namespaces:

    using System.Globalization;
    using System.Linq;
    using System.Web.UI;
    using LocationsModule.Data;
    using LocationsModule.Model;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Localization.Configuration;
    using Telerik.Sitefinity.Modules;
    using Telerik.Sitefinity.Web.UI;
    using Telerik.Sitefinity.Web.UI.ContentUI.Contracts;
    using Telerik.Sitefinity.Web.UI.ContentUI.Views.Backend;
    using Telerik.Sitefinity.Web.UI.Templates;
    using Telerik.Sitefinity.Web.UrlEvaluation;
    using Telerik.Web.UI;
  2. Change the class definition to:

    class MasterListView : ViewBase
    {
     
    }
  3. Reference the embedded template by pasting the following code into the class body:

    protected override string LayoutTemplateName
            {
                get { return null; }
            }
     
    public override string LayoutTemplatePath
            {
                get
                {
                    var path = "~/LocationTemplates/" + layoutTemplateName;
                    return path;
                }
                set
                {
                    base.LayoutTemplatePath = value;
                }
            }
     
    internal const string layoutTemplateName = "LocationsModule.Web.UI.Public.Resources.MasterListView.ascx";
  4. Provide properties for accessing the controls in the template in the following way:

    protected internal virtual RadListView LocationsListControl
    {
        get
        {
            return this.Container.GetControl<RadListView>("LocationsList", true);
        }
    }
     
    protected internal virtual Pager Pager
    {
        get
        {
            return this.Container.GetControl<Pager>("pager", true);
        }
    }
  5. Override the InitializeControls method to retrieve and display the items by pasting this code:

    protected override void InitializeControls(GenericContainer container, IContentViewDefinition definition)
    {
        var masterDefinition = definition as IContentViewMasterDefinition;
        if (masterDefinition == null) return;
     
        var manager = LocationsManager.GetManager(this.Host.ControlDefinition.ProviderName);
        var query = manager.GetLocations();
     
        if (masterDefinition.AllowUrlQueries.HasValue && masterDefinition.AllowUrlQueries.Value)
        {
            query = this.EvaluateUrl(query, "Date", "PublicationDate", this.Host.UrlEvaluationMode, this.Host.UrlKeyPrefix);
            query = this.EvaluateUrl(query, "Author", "Owner", this.Host.UrlEvaluationMode, this.Host.UrlKeyPrefix);
            query = this.EvaluateUrl(query, "Taxonomy", "", typeof(LocationItem), this.Host.UrlEvaluationMode, this.Host.UrlKeyPrefix);
        }
     
        int? totalCount = 0;
        int? itemsToSkip = 0;
        if (masterDefinition.AllowPaging.HasValue && masterDefinition.AllowPaging.Value)
            itemsToSkip = this.GetItemsToSkipCount(masterDefinition.ItemsPerPage, this.Host.UrlEvaluationMode, this.Host.UrlKeyPrefix);
     
        CultureInfo uiCulture = null;
        if (Config.Get<ResourcesConfig>().Multilingual)
            uiCulture = System.Globalization.CultureInfo.CurrentUICulture;
     
        var filterExpression = DefinitionsHelper.GetFilterExpression(masterDefinition);
     
        query = Telerik.Sitefinity.Data.DataProviderBase.SetExpressions(
            query,
            filterExpression,
            masterDefinition.SortExpression,
            uiCulture,
            itemsToSkip,
            masterDefinition.ItemsPerPage,
            ref totalCount);
        this.IsEmptyView = (totalCount == 0);
     
        if (totalCount == 0)
            this.LocationsListControl.Visible = false;
        else
        {
            this.ConfigurePager(totalCount.Value, masterDefinition);
            this.LocationsListControl.DataSource = query.ToList();
        }
    }
  6. Configure the pager by pasting this code:

    protected virtual void ConfigurePager(int virtualItemCount, IContentViewMasterDefinition masterDefinition)
    {
        if (masterDefinition.AllowPaging.HasValue &&
            masterDefinition.AllowPaging.Value &&
            masterDefinition.ItemsPerPage.GetValueOrDefault() > 0)
        {
            this.Pager.VirtualItemCount = virtualItemCount;
            this.Pager.PageSize = masterDefinition.ItemsPerPage.Value;
            this.Pager.QueryParamKey = this.Host.UrlKeyPrefix;
        }
        else
        {
            this.Pager.Visible = false;
        }
    }
  7. Save the file.

The master list view is loaded by the Locations view to display the list of published items on the website. It inherits from ViewBase. You create the Locations view in Creating the Locations view.

In the markup, first, you register the necessary assemblies, so that they can be used in the template. You create a RadListView to display all items.

In MasterListView.cs, you reference the controls from the MasterListView.ascx by calling GetControl. Then, you override the InitializeControls method to retrieve and display the items and add support for paging results by configuring the pager.







Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK