开发者

Add an additional label control to an <ItemTemplate> dynamically in certain cases

How can I add an extra label control dynamically (should be开发者_如何学JAVA 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;

}


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 say e.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 a DataGridItem. When you call Method1 you add it like this Method1(Container) where Container refers to the DataGridItem in question. Then you can say in the Method1

public string Method1(DataGridItem item)
{
    string col1 = DataBinder.Eval(item.DataItem, "Column1").ToString();
    string col2 = DataBinder.Eval(item.DataItem, "Column2").ToString();

    var col2label = new Label() { Visible = true, Text = col2 };
    var col2Holder = item.FindControl("col2Holder");

    col2Holder.Controls.Add(col2label);

    return col1 + "Called method1";
}

Btw, you can't add any controls to a Label, your ItemTemplate should look like this

<ItemTemplate>
   <asp:Label ID="col1Label" runat="server" Text="<%# Method1(Container) %>" />
   <asp:PlaceHolder ID="col2Holder" runat="server" />
</ItemTemplate>

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:

<ItemTemplate>
   <asp:Label ID="label" runat="server" Text="<%# Method1(Container) %>" />
</ItemTemplate>

public string Method1(DataGridItem item)
{
    string col1 = DataBinder.Eval(item.DataItem, "Column1").ToString();
    string col2 = DataBinder.Eval(item.DataItem, "Column2").ToString();

    var label = item.FindControl("label");
    var col2label = new Label() { Visible = true, Text = col2 };

    col1Holder.Controls.Add(col2label);

    return col1 + "Called method1";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜