将附加标签控件添加到中在某些情况下动态地
如何动态添加额外的标签控件(仅应在某些条件下添加)。 我正在尝试做这样的事情:
<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 的某些值)创建占位符的任何提示,而不是选择重复器方法... 我收到一个空引用异常,但是当我必须明确提及时,这个问题就得到了解决:
protected PlaceHolder col2Holder = new Placeholder();
而不是
protected PlaceHolder col2Holder;
But虽然 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;
}
How can I add an extra label control dynamically (should be added only on certain conditions).
I am trying to do something like this:
<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>
Alternatively, I tried putting the placeholder in a seperate template:
<EditItemTemplate>
<asp:PlaceHolder ID="col2Holder" runat="server"></asp:PlaceHolder>
</EditItemTemplate>
but to no avail.
Any tips on how I can create the Placeholder only in some cases (like for some values of Column1/Column2), rather than opt for a repeater approach...
I get a null reference exception but that was solved when I had to explicitly mention:
protected PlaceHolder col2Holder = new Placeholder();
instead of
protected PlaceHolder col2Holder;
But though method1 is able to set the 'Column1's text value correctly, it does nothing for the Column2's value...
Is there something I'm missing or is there a different way to do this?
Here's method1's defn:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在何时何地插入额外的控件?
您很可能应该将一个方法连接到
OnItemDataBound
事件,并在其中决定是否添加控件。该事件为您提供了对所绑定项目的引用,因此您可以说e.Item.Controls.Add(your_control)
Update
啊,现在我明白了您的意思要求。您需要向采用
DataGridItem
的Method1
添加另一个参数。当您调用 Method1 时,您可以像Method1(Container)
一样添加它,其中Container
引用相关的DataGridItem
。然后你可以在Method1
顺便说一句,你不能向标签添加任何控件,你的 ItemTemplate 应该如下所示
如果你希望新标签嵌套在第一个标签内,你应该这样做方法中明确的,并省略占位符:
Where and when do you need the extra control to be inserted?
You should most likely hookup a method to the
OnItemDataBound
event and in there decide whether to add a control or not. The event gives you a reference to the item being bound so you can saye.Item.Controls.Add(your_control)
Update
Ah, now i get it what you're asking for. You need to add another argument to your
Method1
that takes aDataGridItem
. When you call Method1 you add it like thisMethod1(Container)
whereContainer
refers to theDataGridItem
in question. Then you can say in theMethod1
Btw, you can't add any controls to a Label, your ItemTemplate should look like this
If you want the new Label to be nested inside the first label, you should do that explicit in the method, and leave out the placeholder: