追加のラベル コントロールを動的に追加するにはどうすればよいですか (特定の条件でのみ追加する必要があります)。私はこのようなことをしようとしています:
<asp:DataGrid id="dg" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn SortExpression="Column1">
<HeaderTemplate>
<asp:LinkButton Runat="server" text="Column1 Hdr" ID="col1Hdr">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="col1Label" runat="server" Text='<%# Method1(DataBinder.Eval(Container.DataItem, "Column1").ToString(), DataBinder.Eval(Container.DataItem, "Column2").ToString()) %>' >
<asp:PlaceHolder ID="col2Holder" runat="server"></asp:PlaceHolder>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
または、プレースホルダーを別のテンプレートに入れてみました:
<EditItemTemplate>
<asp:PlaceHolder ID="col2Holder" runat="server"></asp:PlaceHolder>
</EditItemTemplate>
しかし、役に立たない。リピーターアプローチを選択するのではなく、一部のケース (Column1/Column2 の一部の値など) でのみプレースホルダーを作成する方法に関するヒント... null 参照例外が発生しましたが、明示的に言及する必要があったときに解決されました:
protected PlaceHolder col2Holder = new Placeholder();
それ以外の
protected PlaceHolder col2Holder;
しかし、method1 は 'Column1 のテキスト値を正しく設定できますが、Column2 の値に対しては何もしません...何か足りないものがありますか、それとも別の方法がありますか?
method1 の定義は次のとおりです。
public string Method1(string col1, string col2)
{
col1 += "Called method1";
Label col2label= new Label();
col2label.Visible = true;
col2label.Text = col2;
col2Holder.Controls.Add(col2label);
col2Holder.DataBind();
return col1;
}