【发布时间】:2010-09-06 23:54:45
【问题描述】:
我们有几个 ASP.Net 数据视图列模板,它们根据用户选择的列动态添加到数据视图中。
这些模板化单元需要处理自定义数据绑定:
public class CustomColumnTemplate:
ITemplate
{
public void InstantiateIn( Control container )
{
//create a new label
Label contentLabel = new Label();
//add a custom data binding
contentLabel.DataBinding +=
( sender, e ) =>
{
//do custom stuff at databind time
contentLabel.Text = //bound content
};
//add the label to the cell
container.Controls.Add( contentLabel );
}
}
...
myGridView.Columns.Add( new TemplateField
{
ItemTemplate = new CustomColumnTemplate(),
HeaderText = "Custom column"
} );
首先这看起来相当混乱,但也存在资源问题。 Label 已生成,不能在 InstantiateIn 中处理,因为那样它就不会在那里进行数据绑定。
这些控件有更好的模式吗?
有没有办法确保标签在数据绑定和渲染之后被释放?
【问题讨论】: