我正在使用转发器控件。我的一个项属性是布尔值。我知道我可以在Text属性中执行条件语句,例如:
Text='<%# Item.Boolean ? "Text 1" : "Text 2" %>
但是,如果我想要相同的文本但是根据布尔值不同的CSS样式怎么办?
以下代码是否可能?
CssClass=<%# Item.Boolean ? "CssClass1" : "CssClass2" %>
答案 0 :(得分:0)
你不能这样做。不是runat服务器类型的标记,因此它不能尝试在那里执行逻辑。相反,您需要在Page_PreRenderComplete中设置gridview的属性。
使用以下内容执行此操作:
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
this.FormatGridviewRows();
}
private void FormatGridviewRows()
{
foreach (GridViewRow row in this.GridView1.Rows)
{
// Don't attempt changes on header / select / etc. Only Datarow
if (row.RowType != DataControlRowType.DataRow) continue;
// At least make sure everything has the default class
row.CssClass = "gridViewRow";
// Don't affect the first row
if (row.DataItemIndex <= 0) continue;
if (row.RowState == DataControlRowState.Normal || row.RowState == (DataControlRowState.Normal ^ DataControlRowState.Edit))
{
row.CssClass = !this.cbForceOverride.Checked
? "gridViewRow"
: "gridViewRow gridViewRowDisabled";
}
if (row.RowState == DataControlRowState.Alternate || row.RowState == (DataControlRowState.Alternate ^ DataControlRowState.Edit))
{
row.CssClass = !this.cbForceOverride.Checked
? "gridViewAltRow"
: "gridViewAltRow gridViewAltRowDisabled";
}
}
}
然后在你的样式表中:
.gridViewRow {
background-color: #f2f2f2;
}
.gridViewAltRow {
background-color: #ffffff;
}
.gridViewRow, .gridViewAltRow {
color: #000000;
}
.gridViewRowDisabled, .gridViewAltRowDisabled {
color: #DDDDDD;
}