代码之家  ›  专栏  ›  技术社区  ›  Eric

在标记中编写代码

  •  0
  • Eric  · 技术社区  · 15 年前

    我在GridView中有一个模板字段,其中有一个标签,我希望根据if语句更改其文本。当然,下面的内容不起作用,但这正是我想要完成的。

     <%if Eval("Address") != ""%>
        <%{ %>
             <ItemTemplate>
              <asp:Label ID="Label18" nowrap="nowrap" runat="server" Text='<%# Eval("Address") + "<br>" + Eval("City") + "," + Eval("State") + " " + Eval("Zip1") + " " + Eval("Zip2") %>'></asp:Label>
            </ItemTemplate>
      <%} %>
    

    我怎样才能做到这一点?

    3 回复  |  直到 15 年前
        1
  •  0
  •   Phaedrus    15 年前

    这个怎么样?

    <ItemTemplate>
    <asp:Label ID="Label18" nowrap="nowrap" runat="server" Text='<%# Eval("Address") != String.Empty ? Eval("Address") + "<br>" + Eval("City") + "," + Eval("State") + " " + Eval("Zip1") + " " + Eval("Zip2") : String.Empty %>'></asp:Label>
    </ItemTemplate>
    
        2
  •  0
  •   kÍ©eÍ£mÍ®pÍ¥ Í©    15 年前

    你可以有一个 DisplayAddress 数据源上的属性-它是 Address 您想要的连接或空/空。

    不知道你的网格是如何绑定的,我不能建议最好的方法。

        3
  •  0
  •   Community CDub    8 年前

    考虑在项模板中使用占位符:

    <ItemTemplate>
       <asp:PlaceHolder ID="phAddress" runat="server" />
    </ItemTemplate>
    

    对于你的网格视图,有一个 OnRowDataBound 可在其中构造新的 asp:Label 或普通HTML。使用 e.Row.DataItem 查找您需要的地址详细信息。

    然后您可以将该标签或HTML字符串分配给 phAddress .

    This related answer may help .