私はリストビューを持っており、それはItemTemplateで、いくつかのコントロールを含むasp:panelです。asp:panel のいずれかのコントロールの値に応じて、3 つの異なる 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 に適用する方法です。