我有一个列表视图,它是 ItemTemplate,一个包含一些控件的 asp:panel。我需要对 asp:panel 应用三个不同的 css 类,具体取决于我的 asp:panel 中的一个控件的值。
这是我的代码:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
HiddenField type = (HiddenField)e.Item.FindControl("valueOfAd_type");
HiddenField paid = (HiddenField)e.Item.FindControl("valueOfPayment");
Panel ThePanel = (Panel)e.Item.FindControl("Panel1");
if ( paid.Value == "2")
{
if (type.Value == "1") //First condition
{
ThePanel.Attributes.Add("class", "whiteBackground");
}
else if (type.Value == "2") //Second condition
{
ThePanel.Attributes.Add("class", "redBackground");
}
}
else //third condition
{
ThePanel.Attributes.Add("class", "blueBackground");
}
}
}
问题是,无论发生哪种情况,最后一个 css 类总是适用于我所有的 asp:panels。现在我的问题是如何将不同的 css 类应用于不同 ItemTemplate 中的 asp:panels?