简体   繁体   中英

How can I compare column data with a String in GridView

I need to hide content of a column based on comparing its data with a string. I like to do it in Page itself (Page does not have code behind)

For some reason I cannot use Eval or Bind to retrieve data for column. I'm looking for something like,

<asp:GridView  ID="GridView1" runat="server" >
  <Columns>
    <asp:TemplateField>
      <%
        if ([data from row] == aVarContainingDataToCompare){
          Response.Write("Hidden");
        } else {
          Response.Write([data from row]);
        }
      %>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate><%# Eval("AnotherData") %></ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

Can I do this without using code behind

Is it alright to use following instead? Please note the # sign at the start

<asp:GridView ID="GridView1" runat="server">
   <Columns>
     <asp:TemplateField>
       <%# (Eval("TheColumn").ToString() == aVarContainingDataToCompare ? "Hidden": Eval("TheColumn")) %>
     </asp:TemplateField>
       ...
   </Columns>
 </asp:GridView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM