【问题标题】:Gridview ItemTemplate with If condition of Database DataGridview ItemTemplate 与数据库数据的 If 条件
【发布时间】:2020-04-06 19:55:52
【问题描述】:

GridView 我想使用显示 3 个结果之一 如果条件为真,则显示文本 1,否则如果显示文本 2,否则显示文本 3

我知道使用 if else 来显示 2 条件结果:<%#Eval("result").ToString().ToLower().Equals("0") ? "text 1" : "text 2" %>

但我需要检查三个值的结果:0 和 1 和 2

并为每个人显示消息,但我不知道该怎么做

如下所示,但我知道这是错误的,但我需要正确的模型

<ItemTemplate>
              <%# If Eval("result")=="0" { %>


                       text 1


              <%# } else If Eval("result")=="1" Then { %>


                       text 2

               <%# }else{ %>

                       text 3

              <%# } %>
  </ItemTemplate>

【问题讨论】:

    标签: asp.net if-statement gridview


    【解决方案1】:

    您如何尝试使用下面的 GridView1_RowDataBound 是一个不同的示例,应该也适合您

     <form id="form1" runat="server">  
            <div>  
                <asp:GridView ID="GridView1" runat="server" CellPadding="6" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">  
                    <Columns>  
                        <asp:BoundField DataField="Id" HeaderText="Id"/>  
                        <asp:BoundField DataField="Name" HeaderText="Name"/>  
                        <asp:BoundField DataField="City" HeaderText="City"/>  
                        <asp:BoundField DataField="Salary" HeaderText="Salary"/>  
                    </Columns>  
                </asp:GridView>  
            </div>  
        </form>  
    
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        {  
            //Checking the RowType of the Row  
            if(e.Row.RowType==DataControlRowType.DataRow)  
            {  
                //If Salary is less than 10000 than set the row Background Color to Cyan  
                if(Convert.ToInt32(e.Row.Cells[3].Text)<10000)  
                {  
                    e.Row.BackColor = Color.Cyan;  
                }  
            }  
        }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多