Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用标记编写代码_C#_.net_Asp.net_Visual Studio - Fatal编程技术网

C# 用标记编写代码

C# 用标记编写代码,c#,.net,asp.net,visual-studio,C#,.net,Asp.net,Visual Studio,我在gridview中有一个模板字段,其中有一个标签,我想根据if语句修改其文本。以下当然不起作用,但这正是我想要完成的 <%if Eval("Address") != ""%> <%{ %> <ItemTemplate> <asp:Label ID="Label18" nowrap="nowrap" runat="server" Text='<%# Eval("Address") + "<br

我在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>
  <%} %>
我怎样才能做到这一点呢?

这个怎么样

<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>
您的数据源上可以有一个DisplayAddress属性,它可以是您想要的地址连接,也可以是null/blank


由于不知道如何绑定网格,我无法建议最好的方法。

考虑在itemTemplate中设置占位符:

<ItemTemplate>
   <asp:PlaceHolder ID="phAddress" runat="server" />
</ItemTemplate>
对于gridview,有一个OnRowDataBound事件,您可以在其中构造一个新的asp:Label或普通HTML。使用e.Row.DataItem查找所需的地址详细信息

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


.

我真的很喜欢这个主意。但它仍然通过代码。我尝试了String.IsnullorEmpty,但没有成功。我从未见过这种方法-但是如果值为null,我如何使用它?String.IsNullOrEmptyEvalAddress