Datalist使用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(set style: color) 没有编译...
谢谢,
I am trying to change the row color of a DataList Control...
<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>
with the following itemdatabound event:
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;
}
}
}
The problem is that whatever is the last set of background colors does both rows.. how can I separate the rows so that one row is 'Status' color and one row is 'Hold' Color??
I tried using div tags as a prev post mentioned but divID.Attributes.Add(set style: color) did not compile...
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用标签控件并将其扩展为列宽的 100% 对此效果很好。
Using a Label Control and expanding it 100% of column width works well for this.