問題
使用 ListView.ItemTemplate將ListView裡將某一行置換顏色
在aspx註記tr為runat="server"並且賦予一個id,當listview的資料符合置換顏色的條件時,藉以取得該行(tr裡的所有td)變更顏色
解決方法
.aspx
<asp:ListView ID="ListView1" runat="server" EnableModelValidation="True" OnItemDataBound="ListView1_ItemDataBound"> <LayoutTemplate> <table id="Table1" runat="server"> <tr> <td> <table id="itemPlaceholderContainer" runat="server" border="1" style="margin-left: 0px; border-collapse: collapse; border-color: #696969;"> <tr style="color:#FFFFFF;background-color:#0000FF"> <th>Product</th> <th>Material</th> </tr> </table> </td> </tr> </LayoutTemplate> <ItemTemplate> <tr style="background-color:#FFFFCC" id="trRow" runat="server"> <td> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Product") %>' /> </td> <td> <asp:Label ID="lblMaterial" runat="server" Text='<%# Eval("Material") %>' /> </td> </tr> </ItemTemplate> </asp:ListView>.cs
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { string type = ((Label)e.Item.FindControl("lblMaterial")).Text; if (type == "SUM") { var dataItem = e.Item as ListViewDataItem; if (dataItem != null) { var innerTR = dataItem.FindControl("trRow"); if (innerTR != null) { ((HtmlTableRow)innerTR).Attributes["style"] = "background-color: #C0C0C0"; } } } }
沒有留言:
張貼留言