Create custom datasource

fabian maes asked on March 11, 2015 20:38

Hi,

I'm using Kentico 8.17 and i'm trying to use a custom datasource throw this tutorial: https://docs.kentico.com/display/K8/Developing+data+source+web+parts

I've created 2 user controls: CMS_CMSGlobalFiles_UCDataSourceMember and CMS_CMSGlobalFiles_WPDataSourceMember

I created a new webpart data source type and with the 3 properties where, orderby and filtername point to WPDataSourceMember And a basic repeater using datasource with id of webpart datasource and a basic tramsformation

<p><%# Eval("name") %></p>

Here is the code for my Web part

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WPDataSourceMember.ascx.cs" Inherits="CMS_CMSGlobalFiles_WPDataSourceMember" %>
<%@ Register Src="~/CMSGlobalFiles/UCDataSourceMember.ascx" TagPrefix="cms" TagName="DataSourceControl" %>
 <asp:Label ID="lbtest" runat="server" EnableViewState="false"/>
<cms:DataSourceControl ID="srcUsers" runat="server" />

And here code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.Helpers;


public partial class CMS_CMSGlobalFiles_WPDataSourceMember : CMSAbstractWebPart
{
    public string WhereCondition
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("WhereCondition"), "");
        }
        set
        {
            this.SetValue("WhereCondition", value);
            srcUsers.WhereCondition = value;
        }
    }

    /// <summary>
    /// Gets or sets the value of the OrderBy web part property.
    /// </summary>
    public string OrderBy
    {
        get
        {
            return ValidationHelper.GetString(this.GetValue("OrderBy"), "");
        }
        set
        {
            this.SetValue("OrderBy", value);
            srcUsers.OrderBy = value;
        }
    }

    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 properties of the internal data source control.
    /// </summary>
    protected void SetupControl()
    {
        if (this.StopProcessing)
        {
            // Does nothing if the web part is disabled
        }
        else
        {
            this.srcUsers.WhereCondition = this.WhereCondition;
            this.srcUsers.OrderBy = this.OrderBy;


            // Sets the inner data source's name according to the web part's 'Web part control ID' property.
            // This allows listing web parts to connect to the inner control.
            // The identifier property is named 'FilterName' because data sources inherit the property from 
            // a base class shared with filter objects.
            //this.srcUsers.FilterName = ValidationHelper.GetString(this.GetValue("WebPartControlID"), this.ClientID);

            // Connects the attached filter, as specified by the 'Filter name' property of the web part
            //this.srcUsers.SourceFilterName = this.FilterName;
        }
    }

}

And now code of my datasource

Co  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCDataSourceMember.ascx.cs" Inherits="CMS_CMSGlobalFiles_UCDataSourceMember" %>
<asp:Label ID="test" runat="server" EnableViewState="false" Text="loaded"/>

And code behind

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CMS.DataEngine;
using CMS.Controls;
using CMS.Membership;


public partial class CMS_CMSGlobalFiles_UCDataSourceMember : CMSBaseDataSource
{
    #region "Properties"

    public override object DataSource
    {
        get
        {
            return mDataSource ?? (mDataSource = base.GetDataSource());
        }
        set
        {
            mDataSource = (DataSet)value;
        }
    }

    #endregion

    #region "Methods"

    /// <summary>
    /// Assigns a handler for the 'OnFilterChanged' event if there is a filter attached to the data source.
    /// You do not need to implement this method if you do not plan to use filters with the data source web part.
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
        //if (SourceFilterControl != null)
        //{
        //    SourceFilterControl.OnFilterChanged += new ActionEventHandler(DataFilter_OnFilterChanged);
        //}

        base.OnInit(e);
    }

    /// <summary>
    /// Handles the OnFilterChanged event.
    /// You do not need to implement this method if you do not plan to use filters with the data source web part.
    /// </summary>
    //void DataFilter_OnFilterChanged()
    //{
    //    // Clears the old data
    //    InvalidateLoadedData();

    //    // Raises the change event
    //    this.RaiseOnFilterChanged();
    //}

    /// <summary>
    /// Loads the data of the datasource.
    /// </summary>
    protected override object GetDataSourceFromDB()
    {
        Response.Write("aaaaa");
        // Initializes the data properties according to the filter settings
        //if (SourceFilterControl != null)
        //{
        //    SourceFilterControl.InitDataProperties(this);
        //}
        //string where = "";// (string.IsNullOrEmpty(WhereCondition)) ? string.Empty : " WHERE " + WhereCondition;
        //string order = "";// (string.IsNullOrEmpty(OrderBy)) ? string.Empty : " ORDER BY " + OrderBy; 
        //string query = "SELECT UserName,FirstName,LastName,FullName,Email,CIMUserTitle FROM CMS_Role JOIN CMS_UserRole ON CMS_Role.RoleID = CMS_UserRole.RoleID JOIN CMS_user ON CMS_user.UserID = CMS_UserRole.UserID  " + where +  order;
        //QueryDataParameters param = new QueryDataParameters();
        //DataSet ds = ConnectionHelper.ExecuteQuery(query, param, QueryTypeEnum.SQLQuery, true);



        DataTable table1 = new DataTable("user");
        table1.Columns.Add("name");
        table1.Columns.Add("id");
        table1.Rows.Add("sam", 1);
        table1.Rows.Add("mark", 2);

        DataSet set = new DataSet();
        set.Tables.Add(table1);

        test.Text = "test";



        DataSource = set;
        //DataSource = UserInfoProvider.GetFullUsers(WhereCondition, OrderBy);
        return DataSource;
    }

    #endregion

}

Nothing particular but nothing is displayied in repeater. I added label in datasource user control and assign text in getdatasourcefromdb function but nothing is display. Response.Write() doesn't work too. How can i be sure it's fired? (i can't use debug)

Thanks for your help

Recent Answers


Brenden Kehren answered on March 12, 2015 00:33

Are you just trying to get a custom list of users? If so, use a custom query repeater, you can then specify a custom query for the users with the ##WHERE## and ##ORDERBY## macros

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.