Now we are going to apply our knowledge into practise. I will develop a simple server control that will accept a title field and a description field from the data binding. The only way that this list control can be populated is through data binding. I have provided a simple collection editor and such in this server control because this is a FAQ at the ww.asp.net forums.
Now, let’s get the item server control on the roll. Basically, I will create a composite server control with a title and a description labels. Here is the basic code for the child and the parent controls (will not be explained because previous articles have gone over these aspects):
public class TitleDescriptionItem : Control, INamingContainer {
private DataRow pDataItem;
protected Label cTitle = new Label();
protected Label cDescription = new Label();
// will be used later with the data binding logic
public DataRow DataItem {
get { return pDataItem; }
set { pDataItem = value; }
}
// provides a wrapper property for the "title" control
public string Title {
get { return cTitle.Text; }
set { cTitle.Text = value; }
}
// provides a wrapper property for the "description" control
public string Description {
get { return cDescription.Text; }
set { cDescription.Text = value; }
}
protected override void CreateChildControls() {
cTitle.Font.Bold = true;
cTitle.ForeColor = Color.Blue;
Controls.Add(cTitle);
Controls.Add(new LiteralControl("<br>"));
Controls.Add(cDescription);
}
// TODO: Binding logic (listed later in article)
}
public class TitleDescription : Control, INamingContainer {
// TODO: Binding logic (listed later in article)
}
Now to spice up the TitleDescription control and the TitleDescriptionItem control. I am just going to add a few attributes. The objective is to allow a collection editor to add items to the TitleDescription control. Firstly, let’s do the simple modification to TitleDescriptionItem control:
[DefaultProperty("Title")]
public class TitleDescriptionItem : Control, INamingContainer {
// ...
}
In the above code, it will give will the collection editor a “name” for the item. Now, to add a few more stuff to the TitleDescription control:
[ParseChildren(true, "Items")]
[PersistChildren(false)]
public class TitleDescription : Control, INamingContainer {
private ControlCollection pItems;
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility
(DesignerSerializationVisibility.Content)]
public ControlCollection Items {
get { return pItems; }
}
// ...
public TitleDescription() {
pItems = new ControlCollection(this);
}
}
The ParseChildren attribute and the PersistChildren attribute are there to tell the ASP.NET parser that the stuff that will appear between the control’s tags are not part of the control – it says to parse all the information off to the Items property.
And finally, the attributes on the Items property says to the ASP.NET parser that the data should appear in inner tags – yet seen as a property. The above will allow the syntax on the ASP.NET page:
<jlc:TitleDescription id="TitleDescription1" runat="server">
<jlc:TitleDscription Title="Me" Description="Hello, my name is Justin" />
<jlc:TitleDscription Title="You" Description="Hi, I am ..." />
</jlc:TitleDescription>
You could read a blog post that I have already made and ask any further questions there. Now that I shed some light on a rather simple matter, yet can be confusing, let’s get back onto track about data binding.