using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using CMS.Controls; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.SettingsProvider; public partial class CMSWebParts_Viewers_Basic_BasicRepeater : CMSAbstractWebPart { #region "Variables" // Base datasource instance private CMSBaseDataSource mDataSourceControl = null; // Indicates whether control was binded private bool binded = false; // BasicRepeter instance private BasicRepeater BasicRepeater = new BasicRepeater(); // Indicates whether current control was added to the filter collection private bool mFilterControlAdded = false; #endregion #region "Public properties" /// /// Gets or sets name of source. /// public string DataSourceName { get { return ValidationHelper.GetString(GetValue("DataSourceName"), ""); } set { SetValue("DataSourceName", value); } } /// /// Control with data source. /// public CMSBaseDataSource DataSourceControl { get { // Check if control is empty and load it with the data if (mDataSourceControl == null) { if (!String.IsNullOrEmpty(DataSourceName)) { mDataSourceControl = CMSControlsHelper.GetFilter(DataSourceName) as CMSBaseDataSource; } } return mDataSourceControl; } set { mDataSourceControl = value; } } /// /// Gets or sets AlternatingItemTemplate property. /// public string AlternatingItemTransformationName { get { return ValidationHelper.GetString(GetValue("AlternatingItemTransformationName"), ""); } set { SetValue("AlternatingItemTransformationName", value); } } /// /// Gets or sets FooterTemplate property. /// public string FooterTransformationName { get { return ValidationHelper.GetString(GetValue("FooterTransformationName"), ""); } set { SetValue("FooterTransformationName", value); } } /// /// Gets or sets HeaderTemplate property. /// public string HeaderTransformationName { get { return ValidationHelper.GetString(GetValue("HeaderTransformationName"), ""); } set { SetValue("HeaderTransformationName", value); } } /// /// Gets or sets ItemTemplate property. /// public string TransformationName { get { return ValidationHelper.GetString(GetValue("TransformationName"), ""); } set { SetValue("TransformationName", value); } } /// /// Gets or sets SeparatorTemplate property. /// public string SeparatorTransformationName { get { return ValidationHelper.GetString(GetValue("SeparatorTransformationName"), ""); } set { SetValue("SeparatorTransformationName", value); } } /// /// Gets or sets HideControlForZeroRows property. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), true); } set { SetValue("HideControlForZeroRows", value); } } /// /// Gets or sets ZeroRowsText property. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), ""); } set { SetValue("ZeroRowsText", value); } } /// /// Gets or sets FooterTemplate for selected item. /// public string SelectedItemFooterTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemFooterTransformationName"), ""); } set { SetValue("SelectedItemFooterTransformationName", value); } } /// /// Gets or sets HeaderTemplate for selected item. /// public string SelectedItemHeaderTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemHeaderTransformationName"), ""); } set { SetValue("SelectedItemHeaderTransformationName", value); } } /// /// Gets or sets ItemTemplate for selected item. /// public string SelectedItemTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemTransformationName"), ""); } set { SetValue("SelectedItemTransformationName", value); } } #endregion #region "Methods" /// /// On content loaded override. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } protected override void OnInit(EventArgs e) { //Handle filter change event if (DataSourceControl != null) { DataSourceControl.OnFilterChanged += new ActionEventHandler(DataSourceControl_OnFilterChanged); } base.OnInit(e); } /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { // Set properties if (!String.IsNullOrEmpty(ZeroRowsText)) { BasicRepeater.ZeroRowsText = ZeroRowsText; } BasicRepeater.HideControlForZeroRows = HideControlForZeroRows; BasicRepeater.DataBindByDefault = false; BasicRepeater.OnPageChanged += new EventHandler(BasicRepeater_OnPageChanged); EnsureFilterControl(); } } /// /// Ensures current control in the filters collection. /// protected void EnsureFilterControl() { if (!mFilterControlAdded) { // Add basic repeater to the filter collection CMSControlsHelper.SetFilter(ValidationHelper.GetString(GetValue("WebPartControlID"), ClientID), BasicRepeater); mFilterControlAdded = true; } } /// /// OnPageChaged event handler. /// /// Sender /// EventArg private void BasicRepeater_OnPageChanged(object sender, EventArgs e) { EnsureChildControls(); // Reload data if (DataSourceControl != null) { BasicRepeater.DataSource = DataSourceControl.DataSource; LoadTransformations(); BasicRepeater.DataBind(); binded = true; } } protected override void CreateChildControls() { // Add control to the control collection plcBasicRepeater.Controls.Add(BasicRepeater); base.CreateChildControls(); } /// /// Loads and setups web part. /// protected override void OnLoad(EventArgs e) { EnsureChildControls(); // Check whether postback was executed from current transformation item if (RequestHelper.IsPostBack()) { // Indicates whether postback was fired from current control bool bindControl = false; // Check event target value and callback parameter value string eventTarget = ValidationHelper.GetString(Request.Form["__EVENTTARGET"], String.Empty); string callbackParam = ValidationHelper.GetString(Request.Form["__CALLBACKPARAM"], String.Empty); if (eventTarget.StartsWithCSafe(UniqueID) || callbackParam.StartsWithCSafe(UniqueID) || eventTarget.EndsWithCSafe(ContextMenu.CONTEXT_MENU_SUFFIX)) { bindControl = true; } // Check whether request key contains some control assigned to current control else { foreach (string key in Request.Form.Keys) { if ((key != null) && key.StartsWithCSafe(UniqueID)) { bindControl = true; break; } } } if (bindControl) { // Reload data if (DataSourceControl != null) { BasicRepeater.DataSource = DataSourceControl.DataSource; LoadTransformations(); BasicRepeater.DataBind(); binded = true; } } } base.OnLoad(e); } /// /// Load transformations with dependence on datasource type and datasource state. /// protected void LoadTransformations() { CMSBaseDataSource docDataSource = DataSourceControl as CMSBaseDataSource; if ((docDataSource != null) && (docDataSource.IsSelected) && (!String.IsNullOrEmpty(SelectedItemTransformationName))) { BasicRepeater.ItemTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemTransformationName, false); if (!String.IsNullOrEmpty(SelectedItemFooterTransformationName)) { BasicRepeater.FooterTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemFooterTransformationName, false); } else { BasicRepeater.FooterTemplate = null; } if (!String.IsNullOrEmpty(SelectedItemHeaderTransformationName)) { BasicRepeater.HeaderTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemHeaderTransformationName, false); } else { BasicRepeater.HeaderTemplate = null; } } else { // Apply transformations if they exist if (!String.IsNullOrEmpty(TransformationName)) { BasicRepeater.ItemTemplate = CMSDataProperties.LoadTransformation(this, TransformationName, false); } if (!String.IsNullOrEmpty(AlternatingItemTransformationName)) { BasicRepeater.AlternatingItemTemplate = CMSDataProperties.LoadTransformation(this, AlternatingItemTransformationName, false); } if (!String.IsNullOrEmpty(FooterTransformationName)) { BasicRepeater.FooterTemplate = CMSDataProperties.LoadTransformation(this, FooterTransformationName, false); } if (!String.IsNullOrEmpty(HeaderTransformationName)) { BasicRepeater.HeaderTemplate = CMSDataProperties.LoadTransformation(this, HeaderTransformationName, false); } if (!String.IsNullOrEmpty(SeparatorTransformationName)) { BasicRepeater.SeparatorTemplate = CMSDataProperties.LoadTransformation(this, SeparatorTransformationName, false); } } } /// /// OnFilter change event handler. /// private void DataSourceControl_OnFilterChanged() { EnsureChildControls(); // Set forcibly visibility Visible = true; // Reload data if (DataSourceControl != null) { BasicRepeater.DataSource = DataSourceControl.DataSource; LoadTransformations(); BasicRepeater.DataBind(); binded = true; } } /// /// OnPreRender override. /// protected override void OnPreRender(EventArgs e) { // Datasource data object ds = null; // Set transformations if data source is not empty if (DataSourceControl != null) { // Get data from datasource ds = DataSourceControl.DataSource; // Check whether data exist if ((!DataHelper.DataSourceIsEmpty(ds)) && (!binded)) { // Initialize related data if provided if (DataSourceControl.RelatedData != null) { RelatedData = DataSourceControl.RelatedData; } BasicRepeater.DataSource = DataSourceControl.DataSource; LoadTransformations(); BasicRepeater.DataBind(); } } base.OnPreRender(e); // Hide control for zero rows if (((DataSourceControl == null) || (DataHelper.DataSourceIsEmpty(ds))) && (HideControlForZeroRows)) { Visible = false; } } /// /// Reload data. /// public override void ReloadData() { SetupControl(); EnsureFilterControl(); base.ReloadData(); } #endregion; }