The next control is the SubscriberAdministration control.
This is a composite control, which combines a drop down of newsletter names,
with a custom list of subscribers that belong to the list. This control
provides a simple way to lookup subscriber names that are registered in a list.
The two primary controls that are used within this composite are:
Listing 7
private SimpleDataList _dataList;
private DropDownList _dropDown;
SimpleDataList is another list that works with simple data,
such as string arrays and such, and supports the {0} construct for the
DataField property. The setup of this control, called whenever the child
control collection is created, is shown below:
Listing 8
protected override void CreateChildControls()
{
this.Controls.Clear();
this._dropDown = new DropDownList();
this.Controls.Add(this._dropDown);
this._dropDown.AutoPostBack = true;
this._dropDown.Items.Add("Select One");
this._dropDown.SelectedIndexChanged += new EventHandler
(this._dropDown_SelectedIndexChanged);
this._dataList = new SimpleDataList();
this.Controls.Add(this._dataList);
this._dataList.DataField = "{0}";
if (!this.DesignMode && (!Page.IsPostBack || !this.EnableViewState))
{
string[]newsletters = Newsletter.GetAllNewsletters();
foreach (string newsletter in newsletters)
this._dropDown.Items.Add(newsletter);
}
}
In the above code, a drop down list is created, given a
default value, and sets up an event handler. For the data list, since it
supports the {0} notation, this is what is assigned the DataField property. This
control doesn't expose properties that affect binding or setup of these
controls. The reason is that the base data source for the drop down is the
list of newsletters from the Newsletter provider. These items are added to the
drop down after the "Select One" default value.
There is one more thing to note; there isn't any need to
render the controls; just by adding them to the collection, each child control
is rendered automatically (because the render process calls each child control's
render method).
Whenever the drop down selection changes, the event handler
fires, and the subscribers for that newsletter are returned in a simple
iterated list form:
Listing 9
private void _dropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddList = sender as DropDownList;
if (ddList.SelectedItem.Text != "Select One")
{
string[]subscribers = Newsletter.GetSubscribers
(this._dropDown.SelectedItem.Text);
this._dataList.DataSource = subscribers;
this._dataList.DataBind();
}
}
If the selected item is not the default option, then the
list of subscribers is passed to the simple data list and bound. This control
is primarily used to show the list of users that have subscribed, and to get a
count of how many people did subscribe. In the future, this control could be
expanded to delete the subscribers from this list, as well as to send the users
in that list an HTML-based email through an editor. Paging and sorting would
be a benefit to the list.