1

I have a few fields that look like this in a website that uses ASP and VB (the data is displayed in a gridview):

<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
     <ItemTemplate>                                                                     
         <asp:Label ID ="Label_Comp" runat="server"                            
              Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' /> 
         </ItemTemplate>
</asp:TemplateField>

And what I'm trying to do is display nothing in the field if the data is empty, and display the string you see in the Text property if there is data. Currently it displays the hyphen used in the Text string when there is no data. I tried several methods of formatting the Eval that I found online but was unable to find a working solution. I also tried using the

EmptyDataText

property however this seemed to have no effect.

I am new to ASP so that could be user error. Any help is greatly appreciated.

4

1 回答 1

0

您还可以将 eval 用于可见并检查数据

<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
     <ItemTemplate>                                                                     
         <asp:Label ID ="Label_Comp" runat="server" visible='<%# If(String.IsNullOrEmpty(Eval("CDE_CMPT")), false, true)'                            
              Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' /> 
         </ItemTemplate>
</asp:TemplateField>

我有一段时间没用过 VB.net,所以语法可能会关闭。

于 2013-11-12T16:05:07.450 回答