将附加标签控件添加到中在某些情况下动态地

发布于 2024-10-07 03:51:33 字数 1743 浏览 2 评论 0原文

如何动态添加额外的标签控件(仅应在某些条件下添加)。 我正在尝试做这样的事情:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-10-14 03:51:33

您需要在何时何地插入额外的控件?

您很可能应该将一个方法连接到 OnItemDataBound 事件,并在其中决定是否添加控件。该事件为您提供了对所绑定项目的引用,因此您可以说 e.Item.Controls.Add(your_control)

Update

啊,现在我明白了您的意思要求。您需要向采用 DataGridItemMethod1 添加另一个参数。当您调用 Method1 时,您可以像 Method1(Container) 一样添加它,其中 Container 引用相关的 DataGridItem。然后你可以在 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";
}

一句,你不能向标签添加任何控件,你的 ItemTemplate 应该如下所示

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

如果你希望新标签嵌套在第一个标签内,你应该这样做方法中明确的,并省略占位符:

<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";
}

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文