Developing Data source web parts

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The code example below shows the creation of a sample custom DataSource web part for providing users from the CMS. The web part consists of two controls, so you need to take the following two steps when developing your custom DataSource web part:

 

1. Create a user control that inherits from CMSBaseDataSource. This will be the control that gets the data.

 

The OnInit and DataFilter_OnFilterChanged methods are necessary for handling the connected filter. If you are not planning to use a filter with the data source web part, you do not need to implement these methods.
The GetDataSourceFromDB method ensures the loading of the actual data.

 

DataSourceControl.ascx

 

Please keep in mind that the following code is only an example, the actual values of the CodeFile and Inherits attributes will be different according to the name and location of the user control.

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DataSourceControl.ascx.cs" Inherits="CMSTestingSite_APIExamples_Controls_DataSourceControl" %>

 

DataSourceControl.ascx.cs

 

Please be aware that the name of the class will be different according to the name and location of the newly created user control.

 
[C#]
 

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using CMS.Controls;

using CMS.SiteProvider;

 

/// <summary>

/// Users data source. Loads all users with dependence on specified where condition

/// </summary>

public partial class CMSTestingSite_APIExamples_Controls_DataSource : CMSBaseDataSource

{

   #region "Variables"

 

  private string mWhereCondition = String.Empty;

  private string mOrderBy = String.Empty;

 

   #endregion

 

 

   #region "Properties"

 

  /// <summary>

  /// Gets or sets the where condition

  /// </summary>

  public string WhereCondition

   {

      get

       {

          return mWhereCondition;

       }

      set

       {

           mWhereCondition = value;

       }

   }

     

 

  /// <summary>

  /// Gets the data source

  /// </summary>

  public override object DataSource

   {

        get

        {

          return base.GetDataSource();

        }

   }

 

  /// <summary>

  /// Gets or sets the order by clause

  /// </summary>

  public string OrderBy

   {

      get

       {

          return mOrderBy;

       }

      set

       {

           mOrderBy = value;

       }

   }

 

   #endregion

 

 

   #region "Methods"

 

  /// <summary>

  /// Handle filter change event

  /// </summary>

  protected override void OnInit(EventArgs e)

   {

      if (SourceFilterControl != null)

       {

           SourceFilterControl.OnFilterChanged += new ActionEventHandler                           (DataFilter_OnFilterChanged);

       }

 

      base.OnInit(e);

   }

 

 

  /// <summary>

  /// OnFilterChange handler

  /// </summary>

  void DataFilter_OnFilterChanged()

   {

      // Clear old data

       InvalidateLoadedData();

      // Raise change event

      this.RaiseOnFilterChanged();

   }

 

 

  /// <summary>

  /// Gets the datasource data

  /// </summary>

  public override object GetDataSourceFromDB()

   {

      // Check whether datasource contains data

      if (base.DataSource != null)

       {

          return base.DataSource;

       }

      // Initialize data and return datasource

      else

       {

          // Initialize properties with dependence on filter settings

          if (SourceFilterControl != null)

           {

               SourceFilterControl.InitDataProperties(this);

           }

 

          // Load data, in this example nothing is loaded

          base.DataSource = UserInfoProvider.GetFullUsers(this.WhereCondition, this.OrderBy);

          return base.DataSource;

       }

   }

 

   #endregion
}

 

2. Create another user control and drop the first control on it. This control will become the actual web part.

 

The control must inherit from CMSAbstractWebPart.
The line of the SetupControl method beginning with this.srcUsers.FilterName ensures that the inner datasource's name is set the same as the Web part control ID property of this web part. Don't get perplexed by the property being called FilterName. This is by design, because datasources use the same objects as filters.

 

DataSourceWebPart.ascx

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DataSourceWebPart.ascx.cs" Inherits="CMSTestingSite_APIExamples_Controls_DataSourceWebPart" %>

 

<%@ Register src="DataSourceControl.ascx" tagname="DataSourceControl" tagprefix="cms" %>

 

<cms:DataSourceControl ID="srcUsers" runat="server" />

 

DataSourceWebPart.ascx.cs

 
[C#]
 

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using CMS.PortalControls;

using CMS.GlobalHelper;

 

public partial class CMSTestingSite_APIExamples_Controls_DataSourceWebPart : CMSAbstractWebPart

{

  /// <summary>

  /// Gets or sets WHERE condition.

  /// </summary>

  public string WhereCondition

   {

      get

       {

          return ValidationHelper.GetString(this.GetValue("WhereCondition"), "");

       }

      set

       {

          this.SetValue("WhereCondition", value);

           srcUsers.WhereCondition = value;

       }

   }

 

 

  /// <summary>

  /// Gets or sets ORDER BY condition.

  /// </summary>

  public string OrderBy

   {

      get

       {

          return ValidationHelper.GetString(this.GetValue("OrderBy"), "");

       }

      set

       {

          this.SetValue("OrderBy", value);

           srcUsers.OrderBy = value;

       }

   }

 

 

  /// <summary>

  /// Gets or sets the source filter name

  /// </summary>

  public string FilterName

   {

      get

       {

          return ValidationHelper.GetString(this.GetValue("FilterName"), "");

       }

      set

       {

          this.SetValue("FilterName", value);

           srcUsers.SourceFilterName = value;

       }

   }

 

 

  /// <summary>

  /// Content loaded event handler

  /// </summary>

  public override void OnContentLoaded()

   {

      base.OnContentLoaded();

       SetupControl();

   }

 

 

  /// <summary>

  /// Initializes the control properties

  /// </summary>

  protected void SetupControl()

   {

      if (this.StopProcessing)

       {

          // Do nothing

       }

      else

       {

          this.srcUsers.WhereCondition = this.WhereCondition;

          this.srcUsers.OrderBy = this.OrderBy;

         

          // Sets the current web part name as datasource name

          this.srcUsers.FilterName = ValidationHelper.GetString(this.GetValue("WebPartControlID"), this.ClientID);

          // Sets the name of the extending filter

          this.srcUsers.SourceFilterName = this.FilterName;

       }

   }

}

 

You can now register this control as a web part in Kentico CMS as described in the Developing web parts topic.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?developing_datasource_web_parts.htm