【问题标题】:if statement inside gridview ItemTemplategridview ItemTemplate 中的 if 语句
【发布时间】:2014-12-10 14:34:59
【问题描述】:

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

<asp:TemplateField HeaderText="Opened Date">

                <ItemTemplate>

                    <%#Eval("OpenedDate")%>

                </ItemTemplate>
            </asp:TemplateField>

我需要添加一个条件来运行 仅当来自另一个模板值的值等于“yes”时

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

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

<asp:TemplateField HeaderText="Opened Date">

                    <ItemTemplate>
<% if (%>
<%#Place.GetColoredGetYESNOfromBOOL1(Eval("OpenHouse").ToString())=="yes" )%>

                        <%#Eval("OpenedDate")%>
                       <% ) %>
                    </ItemTemplate>
                </asp:TemplateField>

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/5676803/…
  • 您不能将数据绑定块放在 if 语句的中间。将条件代码放在 ItemDataBound 事件或类似事件后面的代码中。不在标记中。

标签: c# asp.net gridview


【解决方案1】:

您不能将 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(e.Row.Cells[5].Text.Equals("Yes")){ e.Row.单元格[6].Text.Equals("") }
  • 如果你想为 Row.Cells[6] 设置新值,试试这个:e.Row.Cells[6].Text="new value"
  • 我可以让它设置新值,但我遇到的问题是要设置一个条件(如果 e.Row.Cells[5] 上的值等于(“some text”)然后在 e.Row.Cells[6].Text="blank") 上设置新值。出于某种原因,我提出的任何条件都返回 null(看不到它)。
  • 在项目模板中添加标签并给它 id "Mylabel" 然后在代码中试试这个: Label Mylabel = (Label)e.Row.FindControl("Mylabel"); if (e.Row.Cells[5].Text.Equals("somtext")) { Mylabel.Text = "new value"; }
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
相关资源
最近更新 更多