Hi Rolf,
Since Steven is on sick leave today, I will follow-up your question.
Any server control can be designed to automatically retrieve some of its
data from an external data source. Based on the interaction between the
data source and the control, you can identify three distinct forms of data
binding and, subsequently, data-bound controls-simple binding, list
controls, and complex binding.
Simple data binding consists of binding an object and one or more control
properties. The data source binds to an individual item as opposed to a
list of items. The internal structure of a control that uses simple binding
is nearly identical to that of a complex bound control-only simpler.
List controls are controls that display a list of data items through a
fixed and immutable user interface. Popular examples of list controls are
RadioButtonList, CheckBoxList and, new in ASP.NET 2.0, BulletList.
Complex data-bound controls are typically composite controls that display a
list of items with no limitation at all on the rendering mechanism. A good
example of complex data-bound control is the DataGrid control.
In ASP.NET 1.x, data binding works only in one direction, meaning that the
control has typically no way to update the data source. Each data-bound
control generally reads data from the data source and fires events when
something happens that requires updates on the bound source. In ASP.NET
2.0, data source controls from the underlying machinery for two-way data
binding. In the whole ASP.NET 2.0 control toolbox, only three controls
support two-way data binding-GridView, FormView, and DetailsView. Two-way
binding requires additional logic in the control code to invoke proper
methods on the bound data source, which has to be a data source control.
All data-bound controls derive from BaseDataBoundControl. The
BaseDataBoundControl class defines the machinery through which the data
binding occurs and validates any bound data. Common data source properties
are defined on this class-DataSource for enumerable data, and DataSourceID
for data source controls:
public virtual object DataSource { get; set; }
public virtual string DataSourceID { get; set; }
The DataSource property accepts objects that implements the IEnumerable
(for example, collections) or IListSource (for example, DataTable)
interface.
The DataSourceID property is a string and refers to the ID of a bound data
source control. Once a control is bound to a data source, any further
interaction between the two (in both reading and writing) is out of your
control and hidden from view.
Even though control developers are offered two distinct ways of binding,
under the hood of data-bound controls data retrieval occurs in just one
way-through data source view objects. If the control is bound to a data
source control, the incorporated data source view object is retrieved via
the members of the IDataSource interface. If the control is bound to an
enumerable object, a data source view object is dynamically built and
returned by GetData.
By design, a DataSourceView object features the Select method and returns
the bound data through it. The Select method accepts some input arguments
and a callback function. The callback function receives an enumerable
collection of data-the items to bind to the control.
The bottom line is that whatever data source you bind to the control, a
data source view object is created. A data source view object is a class
that can perform SELECT, INSERT, DELETE, and UPDATE operations on a bound
object.
Now we get to the question you initially asked: as a control developer, how
can you access this bindable collection of data?
The callback function that gets to process the results of the SELECT
operation on the data source view ends up calling a protected overridable
method-PerformDataBinding:
protected virtual void PerformDataBinding(IEnumerable data)
{
// data is the collection of data to show in the data-bound control's user
interface
}
Based on my understanding, your objective is to build a data-bound list
control. A list control builds its own user interface by repeating a fixed
template for each bound data item within the boundaries of the control's
mainframe. For example, a CheckBoxList control just repeats a CheckBox
control for each bound data item. Likewise, a DropDownList control iterates
through its data source and creates a new <option> element within a parent
<select> tag.
In ASP.NET 2.0, all list controls inherit from ListControl. ListControl
adds quite a few new members to the interface of its parent
class-DataBoundControl (which inherits from BaseDataBoundControl).
For all list controls, the data item type is ListItem. When you bind a list
control to its data, the Items collection gets filled, typically within the
PerformDataBinding method.
To demonstrate how to create a very simple list control, we will inherit
from ListControl and create a control named SimpleHyperLinkList:
public class SimpleHyperLinkList : ListControl
{
private HyperLink _controlToRepeat;
private HyperLink ControlToRepeat
{
get
{
if (_controlToRepeat == null) _controlToRepeat = new
HyperLink();
return _controlToRepeat;
}
}
protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < Items.Count; i++)
{
HyperLink ctl = ControlToRepeat;
ctl.ApplyStyle(ControlStyle);
ctl.Text = Items
.Text;
ctl.NavigateUrl = Items.Value;
ctl.RenderControl(writer);
writer.Write("<br />");
}
}
}
However, deriving your control from ListControl forces you to adopt
ListItem as the data item object (you can't build your own) and
ListItemCollection as the data item collection type. The names of the
existing mapping properties cannot be changed, though new mapping
properties can be added.
To use your own data item object and item collection, you need to derive
your control from DataBoundControl and implement the interface
IRepeatInfoUser. I can depict more on this if you need to go this way, or
you could describe your requirement more specifically so that I can suggest
you how to build such control.
By the way, I would highly recommend you read the book <<Programming
Microsoft ASP.NET 2.0 Applications: Advanced Topic>> by Dino Esposito.
Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.