C# gridview ItemTemplate中的if语句
我有Gridview项目模板,我需要在其中添加一个条件C# gridview ItemTemplate中的if语句,c#,asp.net,gridview,C#,Asp.net,Gridview,我有Gridview项目模板,我需要在其中添加一个条件 <asp:TemplateField HeaderText="Opened Date"> <ItemTemplate> <%#Eval("OpenedDate")%> </ItemTemplate> </asp:TemplateField> 我
<asp:TemplateField HeaderText="Opened Date">
<ItemTemplate>
<%#Eval("OpenedDate")%>
</ItemTemplate>
</asp:TemplateField>
我需要添加一个条件,仅当另一个模板值的值等于“是”时才运行
我正在处理下面的代码,但我得到了语法错误
<asp:TemplateField HeaderText="Opened Date">
<ItemTemplate>
<% if (%>
<%#Place.GetColoredGetYESNOfromBOOL1(Eval("OpenHouse").ToString())=="yes" )%>
<%#Eval("OpenedDate")%>
<% ) %>
</ItemTemplate>
</asp:TemplateField>
您不能将if语句放在项模板中,而是可以在行绑定到gridview后检查并放置代码 试试这个代码
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
}
}
这可能会有帮助:不能在IF语句的中间放置数据绑定块。将条件代码放在ItemDataBound事件或类似事件中的代码中。不在标记中。我如上所述更新了后面的代码,但如果(e.Row.Cells[5].Text.Equals(“Yes”){e.Row.Cells[6].Text.Equals(“”}),则每个单元格的读数都为“”,如果要为Row.Cells[6]设置新值,请尝试以下操作:e.Row.Cells[6].Text=“new value”我可以让它设置新值,但我遇到的问题是,要设置一个条件(如果e.Row.Cells[5]上的值等于(“某些文本”),则在e.Row.Cells[6].text=“blank”上设置新值)。出于某种原因,我放置的任何条件返回时都带有null(看不到它)。在项模板中添加label并给它id“Mylabel”,然后在代码中尝试以下操作:label Mylabel=(label)e.Row.FindControl(“Mylabel”);if(e.Row.Cells[5].Text.Equals(“somtext”){Mylabel.Text=“新值”;}
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
}
}