Sitefinity CMS

Create the Lists Control Designer Send comments on this topic.
How-to > Controls > Control Designers > Create the Lists Control Designer

Glossary Item Box

Following is the implementation of the Lists control designer. It consists of a control designer class and a template:

ListsControlDesigner.ascx Copy Code
<div class="ctrlProps">
 
<div class="ctrlContent">
   
<p class="editcontent"><asp:LinkButton ID="edit" runat="server" Text="<$Resources>"></asp:LinkButton></p>
   
<h3><asp:Literal ID="Literal1" runat="server" Text="<$Resources>"></asp:Literal></h3>
   
<p>
     
<asp:LinkButton ID="picker" runat="server" Text="<$Resources>" CssClass="picker"></asp:LinkButton>
   
</p>
   
<h4>
     
<asp:Literal ID="Literal2" runat="server" Text="<$Resources>"></asp:Literal>
   
</h4>
   
<div class="selectedItems selectedItemsLists">
     
<asp:GridView ID="selectedLists"
                   
runat="server"
                   
AllowPaging="true"
                   
DataKeyNames="ID"              
                   
AutoGenerateColumns="false"
                   
PageSize="30"              
                   
AllowSorting="true"
                   
GridLines="none" ShowHeader="false">
       
<Columns>
         
<asp:BoundField DataField="Name" />
         
<asp:ButtonField ButtonType="link" Text="<$Resources>" CommandName="View"><ItemStyle CssClass="gridActionsView" /></asp:ButtonField>
         
<asp:ButtonField ButtonType="link" Text="<$Resources>" CommandName="Remove"><ItemStyle CssClass="gridActionsRemove" /></asp:ButtonField>
       
</Columns>   
       
<EmptyDataTemplate>
         
<asp:Literal ID="Literal4" runat="server" Text="<$Resources>"></asp:Literal>
       
</EmptyDataTemplate>          
     
</asp:GridView>
   
</div>
   
<div class="listModeOptions" id="listModeOptions">
     
<h3><asp:Literal ID="Literal3" runat="server" Text="<$Resources>"></asp:Literal></h3>
     
<h4><asp:Literal ID="popularModesLabel" runat="server" Text="<$Resources>"></asp:Literal></h4>
     
<asp:RadioButtonList ID="Mode" runat="server" RepeatDirection="Horizontal">
       
<asp:ListItem Text="<$Resources>" Value="ExpandedLists" Selected="true"></asp:ListItem>
       
<asp:ListItem Text="<$Resources>" Value="SimpleList"></asp:ListItem>
       
<asp:ListItem Text="<$Resources>" Value="ExpandableLists"></asp:ListItem>
       
<asp:ListItem Text="<$Resources>" Value="AnchorList"></asp:ListItem>
       
<asp:ListItem Text="<$Resources>" Value="PageList"></asp:ListItem>
     
</asp:RadioButtonList>
     
<p class="allListModeOptions"><asp:Label ID="allModesLabel" AssociatedControlID="AllModes" runat="server" Text="<$Resources>"></asp:Label>
     
<asp:DropDownList ID="AllModes" runat="server"></asp:DropDownList></p>
   
</div>
   
<script type="text/javascript">
     
function jscss(a,o,c1,c2)
     {
       switch (a){
          case 'swap':
            o.className=!jscss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
          break;
          case 'add':
            if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
          break;
          case 'remove':
            var rep=o.className.match(' '+c1)?' '+c1:c1;
            o.className=o.className.replace(rep,'');
          break;
          case 'check':
            return new RegExp('\\b'+c1+'\\b').test(o.className)
          break;
       }
     }
     var lis = document.getElementById("listModeOptions").getElementsByTagName("li");
     var selectedRadioButton = null;
     for(var i = 0; i
< lis.length; i++) {
       (function () {
         var
j = i;
         
//alert(j+ " " +parseInt(lis.length-1));
         if(
j == parseInt(lis.length-1)) {
           jscss(
"add",lis[i],"lastListModeOption");
       }
       var
leftOffset = -j*142;
       lis[i].style.
backgroundPosition = leftOffset +"px 0";
       lis[i].
onmouseover = function () {
                       if(!jscss(
"check",this,"selected"))
                         jscss(
"add",this,"over");
                       }
       lis[i].
onmouseout = function () {
                       jscss(
"remove",this,"over");
                       }
       lis[i].
onclick = function () {
                       this.getElementsByTagName(
"input")[0].checked = true;
                       if(selectedRadioButton != this){                         
                           jscss(
"remove",selectedRadioButton,"selected");
                           if(!jscss(
"check",this,"selected"))
                               jscss(
"swap",this,"over","selected");
                           
selectedRadioButton = this;
                       }
                     }
      })();
       }
           
       var
inputs = document.getElementById("listModeOptions").getElementsByTagName("input");
           for(
i = 0; i < inputs.length; i++) {
               if(inputs[i].
type == "radio") {
                   (function () {
           var
j = i;
           if(inputs[i].
checked == true) {
               
selectedRadioButton = inputs[i].parentNode;
               
//inputs[i].parentNode.className = "selected";
               jscss(
"add",inputs[i].parentNode,"selected");
            }
            
/*
           inputs[i].
onclick = function () {
                           if(selectedRadioButton != this.parentNode) {
                               
                               jscss(
"remove",selectedRadioButton,"selected");
                               if(!jscss(
"check",this.parentNode,"selected"))
                                   jscss(
"swap",this.parentNode,"over","selected");
                               
                               
selectedRadioButton = this.parentNode;
                           }
                       }
                       *
/
          }
)();
               }
           }
       <
/script>
   
</div>
</
div>

 

ListsControlDesigner.cs Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Framework.Web.Design;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using System.ComponentModel;
using Telerik.Utilities.Reflection;
using Telerik.Cms.Web.UI;
using Telerik.Lists.Resources;
using Telerik.Lists.WebControls;
using Telerik.Security.Permissions;
namespace Telerik.Lists.Design
{
   
/// <summary>
   
/// Inherits ControlDesigner class and represents the control designer of Lists control.
   
/// </summary>
   
public class ListsControlDesigner : ControlDesigner
   {
       #region Constructors
       
/// <summary>
       
/// Default constructor. Initializes a new instance of ListsControlDesigner class.
       
/// </summary>
       
public ListsControlDesigner()
       {
       }
       #endregion
       #region Properties
       
/// <summary>
       
/// Gets or sets the template used by ListsControlDesigner control
       
/// </summary>
       
public ITemplate ItemTemplate
       {
           get
           {
               
return this.itemTemplate;
           }
           set
           {
               
this.itemTemplate = value;
           }
       }
       
/// <summary>
       
/// Gets or sets the path of the template used by ListsControlDesigner control
       
/// </summary>
       
public string ItemTemplatePath
       {
           get
           {
               
string value = (string)this.ViewState["ItemTemplatePath"];
               
return String.IsNullOrEmpty(value) ? "~/Sitefinity/Admin/ControlTemplates/Lists/ListsControlDesigner.ascx" : value;
           }
           set
           {
               
this.ViewState["ItemTemplatePath"] = value;
           }
       }
      
       #endregion
       #region Public and Protected methods
       
/// <summary>
       
/// Overriden. Cancels the rendering of a beginning HTML tag for the control.
       
/// </summary>
       
/// <param name="writer">The HtmlTextWriter object used to render the markup.</param>
       
public override void RenderBeginTag(HtmlTextWriter writer)
       {
           
//Do not render
       }
       
/// <summary>
       
/// Overriden. Cancels the rendering of an ending HTML tag for the control.
       
/// </summary>
       
/// <param name="writer">The HtmlTextWriter object used to render the markup.</param>
       
public override void RenderEndTag(HtmlTextWriter writer)
       {
           
//Do not render
       }
       
/// <summary>
       
/// Creates the child controls in ListsControlDesigner control.
       
/// </summary>
       
protected override void CreateChildControls()
       {
           
base.RecreateChildControls();
           
this.Controls.Clear();
           
           
this.cnt = new Container(this);
           
if (this.itemTemplate == null)
           {
               
if (this.Page != null)
               {
                   
string path = this.ItemTemplatePath;
                   
if (File.Exists(this.Page.MapPath(path)))
                       
this.itemTemplate = this.Page.LoadTemplate(path);
                   
else
                       
this.itemTemplate = new DefaultItemTemplate();
               }
           }
           
this.itemTemplate.InstantiateIn(this.cnt);
           
if (base.DesignedControl != null)
           {
               
this.component = (ListDisplay)base.DesignedControl;
               
this.editorDialog = new PropertyEditorDialog();
               
this.editorDialog.TypeContainer = this.component;
               
this.editorDialog.PropertyChanged += new PropertyValueChangedEventHandler(EditorDialog_PropertyChanged);
               
this.Controls.Add(this.editorDialog);
               
               
this.properties = TypeDescriptor.GetProperties(this.component);
               PropertyDescriptor desc;
               
this.providerName = this.component.ProviderName;
               
if (String.IsNullOrEmpty(this.providerName))
                   
this.providerName = ListManager.DefaultProviderName;
               
if (this.manager == null || this.manager.Provider.Name != this.providerName)
                   
this.manager = new ListManager(this.providerName);
               desc =
this.properties.Find("ListsIds", false);
               
this.cnt.Picker.CommandName = base.GetEditorType(desc);
               
this.cnt.Picker.CommandArgument = "ListsIds";
               
this.cnt.Picker.Command += new CommandEventHandler(Picker_Command);
               
if (this.component.ListsIds != null)
                   
this.selectedIds = new List<Guid>(this.component.ListsIds);
               
else
                   
this.selectedIds = new List<Guid>();
               
this.cnt.SelectedLists.DataSource = this.manager.GetListsByIds(this.selectedIds.ToArray());
               
this.cnt.SelectedLists.RowCommand += new GridViewCommandEventHandler(SelectedLists_RowCommand);
               
this.cnt.SelectedLists.RowDataBound += new GridViewRowEventHandler(SelectedLists_RowDataBound);
               
this.cnt.Mode.AutoPostBack = true;
               
this.cnt.Mode.SelectedIndexChanged += new EventHandler(Mode_SelectedIndexChanged);
               
string name = this.component.Mode.ToString();
               
this.SetMode(name, this.cnt.Mode);
               desc =
this.properties.Find("Mode", false);
               
this.cnt.AllModes.AutoPostBack = true;
               
this.cnt.AllModes.SelectedIndexChanged += new EventHandler(Mode_SelectedIndexChanged);
               
this.BindAllModesDropDown(name, desc);
               
               
this.cnt.Edit.Command += new CommandEventHandler(Edit_Command);
               
if (!this.manager.GetPermission(CrudRights.View).CheckDemand())
                   ((LinkButton)
this.cnt.Edit).Visible = false;
               
else
                   
((LinkButton)this.cnt.Edit).Attributes.Add("onclick", "if(confirm('" + Messages.SavePropertyChanges + "')){return true;}else{parent.controlManager.Refresh(); parent.location='Modules.aspx?module=Lists'; return false;}");
           }
           
this.Controls.Add(this.cnt);
           
this.cnt.SelectedLists.DataBind();
       }
      
       #endregion
       #region Helper methods
       
/// <summary>
       
/// Sets the value of ListDisplay Mode property to the value of the corresponding property
       
/// of the specified control.
       
/// </summary>
       
/// <param name="ctrl">The control that provides the new property value.</param>
       
/// <param name="component">The designed control</param>
       
public override void UpdateProperty(object ctrl, object component)
       {
           
string value = ((ITextControl)ctrl).Text;
           ((ListDisplay)component).Mode = (ListDisplay.ListDisplayMode)Enum.Parse(
typeof(ListDisplay.ListDisplayMode), value);
       }
       
private void SetMode(string value, ListControl list)
       {
           list.ClearSelection();
           ListItem item = list.Items.FindByValue(value);
           
if (item != null)
               item.Selected = true;
       }
       
private void Redirect(string url)
       {
           
base.OnCloseWindow(EventArgs.Empty);
           
string script = "parent.location='" + url + "';";
           
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "redirect", script, true);
       }
       
private void BindAllModesDropDown(string name, PropertyDescriptor desc)
       {
           
string[] names = Enum.GetNames(desc.PropertyType);
           Array values = Enum.GetValues(desc.PropertyType);
           
for (int j = 0; j < values.Length; j++)
           {
               
int val = (int)values.GetValue(j);
               ListItem item =
new ListItem(names[j], val.ToString());
               
if (names[j] == name)
                   item.Selected = true;
               
this.cnt.AllModes.Items.Add(item);
           }
       }
       #endregion
       #region Event handlers
       
void Mode_SelectedIndexChanged(object sender, EventArgs e)
       {
           
this.UpdateProperty(sender, this.component);
           
this.RecreateChildControls();
       }
       
void Picker_Command(object sender, CommandEventArgs e)
       {
           
string name = (string)e.CommandArgument;
           
this.editorDialog.Show(name, e.CommandName, this.component.ListsIds, this.component);
       }
       
void EditorDialog_PropertyChanged(object source, PropertyValueChangedEventArgs e)
       {
           
this.component.ListsIds = e.PropertyValue as Guid[];
           
base.OnPropertyChanged(EventArgs.Empty);
           
this.RecreateChildControls();
       }
       
void SelectedLists_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           
int index = Convert.ToInt32(e.CommandArgument);
           Guid itemGuid =
this.selectedIds[index];
           
switch (e.CommandName)
           {
               
case "Remove":
                   
this.selectedIds.RemoveAt(index);
                   
this.component.ListsIds = this.selectedIds.ToArray();
                   
base.OnPropertyChanged(EventArgs.Empty);
                   
this.RecreateChildControls();
                   
break;
               
case "View":
                   
this.Redirect("Modules.aspx?module=Lists&Id=" + itemGuid);
                   
break;
           }
       }
       
void SelectedLists_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           
if (e.Row.RowType == DataControlRowType.DataRow)
           {
               
if (e.Row.Cells[1] != null)
               {
                   
if (!this.manager.GetPermission(CrudRights.View).CheckDemand())
                       e.Row.Cells[1].Visible = false;
                   
else
                   {
                       
int index = e.Row.RowIndex;
                       Guid itemGuid =
this.selectedIds[index];
                       WebControl view = (WebControl)e.Row.Cells[1].Controls[0];
                       
if (view != null)
                           view.Attributes.Add(
"onclick", "if(confirm('" + Messages.SavePropertyChanges + "')){return true;}else{parent.controlManager.Refresh(); parent.location='Modules.aspx?module=Lists&Id=" + itemGuid.ToString() + "'; return false;}");
                   }
               }
           }
       }
       
void Edit_Command(object sender, CommandEventArgs e)
       {
           
this.Redirect("Modules.aspx?module=Lists");
       }
       #endregion
       
private ITemplate itemTemplate;
       
private Container cnt;
       
private PropertyEditorDialog editorDialog;
       
private ListDisplay component;
       
private PropertyDescriptorCollection properties;
       
private string providerName;
       
private ListManager manager;
       
private List<Guid> selectedIds;
       #region Default Template
       
private class DefaultItemTemplate : ITemplate
       {
           
public void InstantiateIn(Control container)
           {
           }
       }
       #endregion
       #region Container
       
private class Container : GenericContainer<ListsControlDesigner>
       {
           
public Container(ListsControlDesigner owner)
               :
base(owner)
           {
           }
           
public IButtonControl Picker
           {
               get
               {
                   
if (this.picker == null)
                       
this.picker = (IButtonControl)base.FindControl(typeof(IButtonControl), "picker", true);
                   
return this.picker;
               }
           }
           
public IButtonControl Edit
           {
               get
               {
                   
if (this.edit == null)
                       
this.edit = (IButtonControl)base.FindControl(typeof(IButtonControl), "edit", true);
                   
return this.edit;
               }
           }
           
public GridView SelectedLists
           {
               get
               {
                   
if (this.selectedLists == null)
                       
this.selectedLists = (GridView)base.FindRequiredControl<GridView>("selectedLists");
                   
return this.selectedLists;
               }
           }
           
public ListControl Mode
           {
               get
               {
                   
if (this.mode == null)
                       
this.mode = (ListControl)base.FindControl(typeof(ListControl), "Mode", true);
                   
return this.mode;
               }
           }
           
public ListControl AllModes
           {
               get
               {
                   
if (this.allModes == null)
                       
this.allModes = (ListControl)base.FindControl(typeof(ListControl), "AllModes", true);
                   
return this.allModes;
               }
           }
           
private IButtonControl picker;
           
private IButtonControl edit;
           
private GridView selectedLists;
           
private ListControl mode;
           
private ListControl allModes;
       }
       #endregion
   }
}