Datalist使用itemdatabound事件问题更改行颜色

时间:2011-01-22 15:50:47

标签: .net datalist web-controls itemdatabound

我正在尝试更改DataList控件的行颜色...

        <asp:DataList ID="dlTrades" Width="100%" 
            RepeatDirection="Horizontal" 
            RepeatColumns="6" runat="server" 
            DataSourceID="objTrds" 
            OnItemDataBound="dlTrades_ItemDataBound">
            <ItemTemplate>
             <table>
                   <tr>
                    <td>
                      <b>
                      <%# DataBinder.Eval((System.Data.DataRowView)Container.DataItem, "Status") %>
                      </b>
                    </td>
                   </tr>
                 </table>
                 <table>
                   <tr>
                      <td><%# DataBinder.Eval((System.Data.DataRowView)Container.DataItem, "Hold") %>
                      </td>
                   </tr> 
                 </table>
            </ItemTemplate>
        </asp:DataList>

使用以下itemdatabound事件:

protected void dlTrades_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        System.Data.DataRowView drv = (System.Data.DataRowView)(e.Item.DataItem);

        string hld = (string)drv.Row["Hold"].ToString();

        if (hld == "Trade")
        {
            e.Item.BackColor = System.Drawing.Color.LightGreen;
            e.Item.ForeColor = System.Drawing.Color.White;
            e.Item.Font.Bold = true;
        }

        if (hld == "Hold")
        {
            e.Item.BackColor = System.Drawing.Color.LightGray;
            e.Item.ForeColor = System.Drawing.Color.White;
            e.Item.Font.Bold = true;
        }

        System.Data.DataRowView drv2 = (System.Data.DataRowView)(e.Item.DataItem);

        string stat = (string)drv2.Row["Status"].ToString();

        if (stat == "Open")
        {
            e.Item.BackColor = System.Drawing.Color.LightGreen;
            e.Item.ForeColor = System.Drawing.Color.White;
            e.Item.Font.Bold = true;
        }
        if (stat == "Filled")
        {
            e.Item.BackColor = System.Drawing.Color.Gold;
            e.Item.ForeColor = System.Drawing.Color.White;
            e.Item.Font.Bold = true;
        }

        if (stat == "Closed")
        {
            e.Item.BackColor = System.Drawing.Color.IndianRed;
            e.Item.ForeColor = System.Drawing.Color.White;
            e.Item.Font.Bold = true;
        }

    }
}

问题是无论最后一组背景颜色都是两行......我如何分隔行,使一行为'状态'颜色,一行为'保持'颜色??

我尝试使用div标签作为提到的帖子,但是divID.Attributes.Add(设置样式:颜色)没有编译...

谢谢,

1 个答案:

答案 0 :(得分:0)

使用Label Control并扩展它,100%的列宽适用于此。