Fast repeater

Mateusz Żebrowski asked on July 1, 2014 06:41

I've wrote this code to create new web part:

public partial class CMSWebParts_Olympic_PPGFaq : CMSAbstractWebPart

{ #region "Properties"

private readonly string Path = "Path";
private readonly string Transformation = "Transformation";
private readonly string SelectedTransformation = "SelectedTransformation";
private readonly string DocumentType = "DocumentType";
private readonly string ColumnsNumber = "ColumnsNumber";

#endregion

#region "Methods"

protected override void OnLoad(EventArgs e)
{       
    base.OnLoad(e);

    DocumentRepeater.DataSource = DocumentDataSource.DataSource;
    DocumentRepeater.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    UnorderList.Attributes.Add("Class", this.GetValue(ColumnsNumber).ToString());
    DocumentDataSource.Path = this.GetValue(Path).ToString();
    DocumentDataSource.ClassNames =  this.GetValue(DocumentType).ToString();
    DocumentRepeater.ClassNames = this.GetValue(DocumentType).ToString();
    DocumentRepeater.TransformationName = this.GetValue(Transformation).ToString();
    DocumentRepeater.SelectedItemTransformationName = this.GetValue(SelectedTransformation).ToString();
}
#endregion

}

It almost works fine. The problem is with:

 DocumentRepeater.SelectedItemTransformationName = this.GetValue(SelectedTransformation).ToString();

When I'm selecting document type it is display via TransformationName, not via SelectedItemTransformationName.

Recent Answers


Brenden Kehren answered on July 1, 2014 07:26

When working with Webparts you need to ensure you're setting the properties in the proper page lifecycle. In this case, I believe the page load event is too late. Here is some boilerplate code for a webpart. Using the public properties allows you to create the properties in the UI for the end user to set vs. hard coding them. Here's the Kentico documentation on creating webparts

public partial class CMSWebParts_MyWebParts_TestUser : CMSAbstractWebPart
{
#region "Properties"

/// <summary>
/// Gets or sets value of MyProperty
/// </summary>
public string MyProperty
{
    get
    {
        return ValidationHelper.GetString(GetValue("MyProperty"), "");
    }
    set
    {
        this.SetValue("MyProperty", value);
    }
}

#endregion


#region "Methods"

/// <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
    {
        // PLEASE PUT YOUR CODE HERE TO ASSIGN PROPERTIES AND LOAD DATA
    }
}


/// <summary>
/// Reloads data
/// </summary>
public override void ReloadData()
{
    base.ReloadData();

    // Reload data in all controls if needed
    // PLEASE PUT YOUR CODE HERE
}

#endregion

}

0 votesVote for this answer Mark as a Correct answer

Mateusz Żebrowski answered on July 2, 2014 03:23

I've modified my code thanks. But Problem still remain the same. After some exploration I've manage to find that after click on document type this document isn't selected. I mean the property DocumentRepeater.IsSelected is False. So my question is: how to select document type after click on it (and go to his URL);

0 votesVote for this answer Mark as a Correct answer

Mateusz Żebrowski answered on July 2, 2014 05:54

Ok I think It will be better if I show code and write what I intended to do. Here is aspx code:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="something" CodeFile="~path.ascx.cs" %>
<cms:CMSDocumentsDataSource ID="DocumentData" runat="server" Path="./%" EnableSelectedItem="true"/>
<ul runat="server" id="UnorderList" ClientIDMode="Static">        
 <cms:CMSRepeater runat="server" ID="DocumentRepeater" DataSourceName="DocumentData"</cms:CMSRepeater>
</ul>

As you can see we have here two controls: Document Data Source and Basic Repeater. The Point is to combine this two controls that Basic Repeater display documents stored in Document Data Source. The default Path for Document Data Source is "./%" which means that documents will be under page on which this web part will be put. The rest is in code behind:

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

using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;

public partial class something : CMSAbstractWebPart
{
#region "Properties"

private readonly string Path = "Path";
private readonly string Transformation = "Transformation";
private readonly string SelectedTransformation = "SelectedTransformation";
private readonly string DocumentType = "DocumentType";
private readonly string ColumnsNumber = "ColumnsNumber";

#endregion

#region "Methods"
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
}

protected override void OnLoad(EventArgs e)
{       
    base.OnLoad(e);
}

public override void OnContentLoaded()
{
    base.OnContentLoaded();
    SetupControl();
}

 protected void SetupControl()
{
    if (this.StopProcessing)
    {
        // Do not process
    }
    else
    {

        UnorderList.Attributes.Add("Class", this.GetValue(ColumnsNumber).ToString());
        DocumentData.Path = this.GetValue(Path).ToString();
        DocumentData.ClassNames = this.GetValue(DocumentType).ToString();
        DocumentRepeater.DataSource = DocumentData.DataSource;
        DocumentRepeater.TransformationName = this.GetValue(Transformation).ToString();
     DocumentRepeater.SelectedItemTransformationName=this.GetValue(SelectedTransformation).ToString()
        DocumentRepeater.DataBind();

    }
}

 public override void ReloadData()
 {
     base.ReloadData();
     SetupControl();
 }

#endregion
}

In #region "properties" there are some privat readonly strings. I'm using them as web part properties wchih are filled out by user via Form Controls. Nothing interesting here.

The place which is interesting is

DocumentRepeater.SelectedItemTransformationName=this.GetValue(SelectedTransformation).ToString()

Data Base stores column named SelectedTransformation and filled out by user via form control called Transformation Selector. We can put there for example "Custom.Document.Default". It seems that after write to browser document url this document should be display by Default Transformatin. But it is display by transformation from DocumentRepeater.TransformationName. What should I do?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 2, 2014 07:13

Did you read the documentation on creating webparts I supplied? Also, what version are you using? What development method are you using (portal or aspx)?

Based on your example, there is no reason you can't use the out of the box repeater Kentico has already created. By default, the <cms:CMSRepeater> is already looking for documents AND does NOT need a datasource, simply a class or multiple class names (cms.news, your.customdoctype, etc.)

1 votesVote for this answer Mark as a Correct answer

Mateusz Żebrowski answered on July 2, 2014 08:56

Misteken like this somteimes happend when two programist develop one web part. I thought It was a Basic Repeater which need datasource but we used Repeater control. I've solve the problem. Thanks.

0 votesVote for this answer Mark as a Correct answer

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