【问题标题】:ASP.NET Change Text and Color in Gridview cell in a Template FieldASP.NET 更改模板字段中 Gridview 单元格中的文本和颜色
【发布时间】:2013-04-09 16:18:02
【问题描述】:

我在 ASP.net 中有显示数据的 Gridview。根据数据,它会根据单元格的值更改颜色和文本。 当列不是模板字段时,这可以正常工作。

 //WORKS WHEN IS NOT A TEMPLATE FIELD
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
        if (e.Row.Cells[2].Text == "1")
        {

           e.Row.Cells[2].Text = "IN";
           e.Row.Cells[2].BackColor = Color.Blue;
           e.Row.Cells[2].ForeColor = Color.White;
        }

  }

现在我将 Column 转换为 Template 字段,但没有任何效果。

     //DOEST NOT WORK WHEN IS a TEMPLATE FIELD
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         if (e.Row.Cells[2].Text == "1")
         {

             e.Row.Cells[2].Text = "IN";
             e.Row.Cells[2].BackColor = Color.Blue;
             e.Row.Cells[2].ForeColor = Color.White;
         }

     }

我得到了颜色,但现在我需要将文本更改为以下内容。如果 statusID == 1 则显示 IN,否则如果 statusID == 2 则显示 OUT

<asp:TemplateField HeaderText="StatusID" SortExpression="StatusID">
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue = '<%# Bind("StatusID") %>'>
                                    <asp:ListItem Value="1">IN</asp:ListItem>
                                    <asp:ListItem Value="2">OUT</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblStatus" runat="server" Text='<%# Bind("StatusID") %>' ForeColor='<%# Convert.ToString(Eval("StatusID")) == "1" ? System.Drawing.Color.Green: Convert.ToString(Eval("StatusID")) == "2" ? System.Drawing.Color.Red: System.Drawing.Color.Purple%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

你们中的任何人都知道如何解决这个问题。提前致谢。

【问题讨论】:

  • 将列文本添加到模板字段中的标签并找到控件并更改控件
  • 你能详细说明一下吗?
  • 你能放你的gridview代码吗.....我的意思是设计代码?
  • 代码已启动。我得到了颜色,请参阅 ItemTemplate,现在我需要更改文本。谢谢

标签: c# asp.net gridview


【解决方案1】:

在模板列中不起作用的原因是状态值为空。请尝试以下操作。

// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
   var status = (Label)e.Row.FindControl("lblStatus");
   if (status.Text == "1")
   {
      e.Row.Cells[2].Text = "IN";
      e.Row.Cells[2].BackColor = Color.Blue;
      e.Row.Cells[2].ForeColor = Color.White;
   }
}

或将 DataItem 转换为适当的对象并获取状态值。

GridViewRow.DataItem Property

// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
   var obj = (MyObject)e.Row.DataItem;
   if (obj.Status == 1)
   {
      e.Row.Cells[2].Text = "IN";
      e.Row.Cells[2].BackColor = Color.Blue;
      e.Row.Cells[2].ForeColor = Color.White;
   }
}

【讨论】:

    【解决方案2】:
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      Label lbl=(Label)e.Row.FindControl("lblStatus");
       if (lbl.Text == "1")
       {
          lbl.Text = "IN";
          e.Row.Cells[2].BackColor = Color.Blue;
          e.Row.Cells[2].ForeColor = Color.White;
       }
    }
    

    【讨论】:

      【解决方案3】:

      我是vb程序员,这里是一个示例代码。

      If e.Row.RowType = DataControlRowType.DataRow Then
                      Dim abc As Label = TryCast(e.Row.FindControl("label1"), Label)
      
                  If abc.Text = "ADMIN" Then
                      e.Row.Cells(7).ForeColor = Drawing.Color.Blue
      
                  End If
              End If
      

      我真的希望它有效。

      【讨论】:

        【解决方案4】:

        使用ItemTemplate 并在Eval 周围加上您需要的任何HTML,例如更改颜色。

        Text='<%# "Your HTML(use FONT COLOR=BLUE)" + Eval("HealthUnit") + "Close your HTML here" %>
        

        在这里:

        <asp:TemplateField HeaderText="Health Unit Website">
            <ItemTemplate>
                <asp:HyperLink runat="server" 
                               NavigateUrl='<%# Eval("Website") %>' 
                               tabindex="-1" 
                               Target="_blank" 
                               Text='<%# "<font color=blue><b>" +
                                          Eval("HealthUnit") + 
                                          "</b></font>" %>'>
                </asp:HyperLink>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" width="14%"  />
        </asp:TemplateField>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-16
          • 1970-01-01
          • 2016-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多