This section in the mover control has been added upon the
advice of my colleague Miguel Castro who is an MVP and a well known consultant
in the field of .NET. He said that since this is a composite control, which
means we are encapsulating controls within this control, it is not wise to
simple expose the whole child controls to page developers! And he is absolutely
true! What we can do is add some required properties and allow the page
developers to configure to make the listboxes and buttons function properly.
For instance, we have added style properties to let the page developer, if
interested, to add some formatting properties. In addition, we want to allow
the page developer to bind some data to the two listboxes. Therefore, it is
wise to provide four main properties:
ListBoxDataSource
ListBoxItems
ListBoxDataTextField
ListBoxDataValueField
Those properties have been added for both listobxes. So, we
will explain them in a generic way to show you why they have been added.
The ListboxDataSource will be used by page developers to
bind any of the two listboxes to any custom collection, array list, dataset,
etc … therefore, it is a “set” property only. One of those properties is shown
below.
Listing 8
public object LeftListDataSource
{
set
{
this.leftListDataSource = value;
}
}
The ListBoxItems is a simple “get” property that returns the
items present at any moment that control is being accessed. The code is show
below:
Listing 9
public ListItemCollection LeftItems
{
get
{
this.EnsureChildControls();
return this.leftListBox.Items;
}
}
First of all, we make sure the controls on the composite
control are created and this is done by calling the EnsureChildControls method.
This internally checks whether the ChildControlsCreated property is false or
true and accordingly calls the CreateChildControls method that we will discuss
soon in this article and will mainly build the control tree of this control
being developed.
The ListBoxDataTextField, as its name suggests, is used to
specify the field that will be shown to the users when the items are added to
any of the listboxes. It mainly corresponds to the DataTextField of the
controls found in .NET Framework used to display a list of data.
Listing 10
public string LeftListDataTextField
{
get
{
if ((object)ViewState["LeftListDataTextField"] == null)
return "";
return (string)ViewState["LeftListDataTextField"];
}
set
{
ViewState["LeftListDataTextField"] = value;
this.ChildControlsCreated = false;
}
}
As mentioned above, the idea behind those properties came to
me from Miguel’s article on code-Magazine, Custom
Web Controls Demystified, Part 2, and we would strongly recommend checking
it out for the richness of information it gives developers in order to develop
rich and well constructed controls.
Hint: Never expose the child controls contained in a
composite control as public properties. Always do a selection on some
properties required for those inner controls and expose them as public properties.