ASP.NET 2.0 DataBoundControls, Part 2
In a previous post I wrote about how the data binding base classes in ASP.NET were not working for me. Today, I sat down and created what I needed.
The end result does what I want in that I can add data binding to my classes without having to derive from BaseDataBoundControl (or one of its subclasses). In the test example below, I've added the data connector and a single callback delegate and I have all the data binding worked out for me.
I created a sample control that includes all of the source code, you can download it here.
[ToolboxData("<{0}:TestControl runat=server></{0}:TESTCONTROL>")]
public class TestControl : WebControl
{
public TestControl()
: base(HtmlTextWriterTag.Div)
{
DataConnector = new DataSourceConnector(this);
DataConnector.PerformDataBindingCallback += new EventHandler>(DataConnector_PerformDataBindingCallback);
}
DataSourceConnector m_connector;
[DefaultValue(null)]
[NotifyParentProperty(true)]
[Category("Data")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DataSourceConnector DataConnector
{
get { return m_connector; }
protected set { m_connector = value; }
}
private void DataConnector_PerformDataBindingCallback(object sender, EventArgs args)
{
if (args.Data != null)
{
Table tbl = new Table();
TableRow row;
TableCell cell;
string dataStr = String.Empty;
foreach (object dataItem in args.Data)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(dataItem);
if (props.Count >= 1)
{
if (null != props[0].GetValue(dataItem))
{
dataStr = props[0].GetValue(dataItem).ToString();
}
}
row = new TableRow();
tbl.Rows.Add(row);
cell = new TableCell();
cell.Text = dataStr;
row.Cells.Add(cell);
}
this.Controls.Add(tbl);
}
}
}
There are no comments.