如何获取标签的值并将颜色设置为gridview itemTemplate中的该值 [英] How to get the value of a label and set the color to that value inside a gridview itemTemplate
本文介绍了如何获取标签的值并将颜色设置为gridview itemTemplate中的该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想将颜色设置为状态字段的标签值,如您在gridview中看到的那样
其中值为待定但它不起作用
这是我正在使用的代码:
Hi I want to set the color to a label value of the status field as u see in the gridview
where the value is "Pending" but it is not working
this is the code I am using :
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="5"
Width="100%" AllowSorting="True" CssClass="GridViewStyle" OnRowCommand="gv_Rowcommand"
OnPageIndexChanging="gv_PageIndexChanging" OnRowDataBound="grv_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<%# Eval("OrderID")%>
<asp:HiddenField ID="hdnOrderId" runat="server" Value='<%# Eval("OrderID") %>' />
</ItemTemplate>
<ItemStyle Width="25" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和文件后面的代码是
and the code behind file is
protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Retrieve the current Row
GridViewRow row=e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
if(row.FindControl("lblStatus").ToString()=="Pending")
{
row.Cells[2].ForeColor = Color.Red;
}
}
}
请有人帮忙解决这个问题.. 。
Please can someone help fixing this...
推荐答案
像这样使用
use like this
row.Cells[2].Style.Add(HtmlTextWriterStyle.Color,"Red");
在这里设置单元格的Fore Color,我猜它不会工作。
所以设置你的颜色标签。
试试这个: -
Here you are setting Fore Color of cell, I guess it won''t work.
So set the for color of your label.
try this :-
GridViewRow row=e.Row;
Label lbl = (Label)row.FindControl("lblStatus");
if (row.RowType == DataControlRowType.DataRow)
{
if(lbl.Text == "Pending")
{
lbl.ForeColor = Color.Red;
}
}
Gool运气。
Gool luck.
这篇关于如何获取标签的值并将颜色设置为gridview itemTemplate中的该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文