网格视图项模板中的 if 语句

本文关键字:if 语句 视图 网格 | 更新日期: 2023-09-27 18:33:59

我有 Gridview 项模板,我需要在其中添加一个条件。

<asp:TemplateField HeaderText="Opened Date">
                <ItemTemplate>
                    <%#Eval("OpenedDate")%>
                </ItemTemplate>
            </asp:TemplateField>

我需要添加一个条件来运行 <%#Eval("打开日期")%> 只有当另一个模板值的值等于"是"时

<%#Place.GetColoredGetYESNOfromBOOL1(Eval("OpenHouse").ToString())%>

我正在处理下面的代码,但出现语法错误。

<asp:TemplateField HeaderText="Opened Date">
                    <ItemTemplate>
<% if (%>
<%#Place.GetColoredGetYESNOfromBOOL1(Eval("OpenHouse").ToString())=="yes" )%>
                        <%#Eval("OpenedDate")%>
                       <% ) %>
                    </ItemTemplate>
                </asp:TemplateField>

网格视图项模板中的 if 语句

不能将 if 语句放在项模板中,而是可以在行绑定到网格视图后检查并放置代码

试试这个代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     //here you put the if statement to get the "OpenHouse" column value
     if(e.Row.Cells[Column Index].Text.Equals("Yes")){
         //your code here
     }
}