ユーザーが選択した列に応じて動的にデータビューに追加されるASP.Netデータビュー列テンプレートがいくつかあります。動的に追加されたWeb.UI.ITemplateクラスのベストプラクティス
これらのテンプレートの細胞は、カスタムデータ連結を処理する必要があります。
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
に処分することはできません。これは、データバインドには存在しないからです。
これらのコントロールに適したパターンはありますか?
ラベルがデータバインド後に廃棄されてレンダリングされることを確認する方法はありますか?