我可以通过使用背景颜色来设置所有颜色但是如何将颜色设置为文本,如下所示在网格视图中显示:成功=绿色,处理=红色,已验证=黄色 谢谢大家。
<asp:Label ID="Label1" BackColor="#006699" runat="server"
Text='<%#Eval("Status").ToString()=="S"?"Success":Eval("Status").ToString()=="V"?"Verified":Eval("Status").ToString()=="A"?"Approved":"Process" %>'></asp:Label>
答案 0 :(得分:1)
在
背后的代码上使用此功能protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["Label1"].ToString();
//format color of the as below
if(state == "Success")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Green;
if(state == "Process")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Rad;
if(state == "Verified")
(e.Row.FindControl("lbl1") as Label).BackColor = Color.Yellow;
}
}
答案 1 :(得分:1)
// Row Data Bound Event fires after gridview calls DataBind() method.
// So if you want to data or check certain conditions before displaying it to the user
// this may be correct place to do the changes.
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var status = (Label)e.Row.FindControl("Label1");
if(status == "Success")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Green;
if(status == "Process")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Rad;
if(status == "Verified")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Yellow;
}
}
或者将DataItem转换为适当的对象并获取状态值。
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var obj = (MyObject)e.Row.DataItem;
if(obj.Status == "Success")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Green;
if(obj.Status== "Process")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Rad;
if(obj.Status == "Verified")
(e.Row.FindControl("Label1") as Label).BackColor = Color.Yellow;
}
}