前台代码:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2"
RepeatDirection="Horizontal" Height="469px" Width="633px"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate> <table border="1"> <tr> <td style="width: 183px; height: 21px"> 楼层:<%#Eval("BuildFloor")%></td>
</tr> <tr> <td style="width: 183px; height: 21px"> 室号:<asp:Label ID="lbl" runat="server" Text='<%#Eval("RoomNumber")%>'></asp:Label></td>
</tr>
</table> </ItemTemplate></asp:DataList>
后台代码:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
buildID = Convert.ToInt32(Request["BuildID"].ToString());
DataTable dts = new DataTable();
dts = build.GetRoomByID(roomID, buildID);
for (int i = 0; i < dts.Rows.Count; i++)
{
string strName = dts.Rows[i]["State"].ToString();
if (strName == "小定")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Blue;
}
else if (strName == "大定")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Red;
}
else if (strName == "全额")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Yellow;
}
strName = null; } }
}
我想实现每个<ItemTemplate>对应数据库字段"state"显示不同的颜色,但是颜色全变成最后一个遍历的数据对应的颜色,有没有办法让它们颜色不同。
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2"
RepeatDirection="Horizontal" Height="469px" Width="633px"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate> <table border="1"> <tr> <td style="width: 183px; height: 21px"> 楼层:<%#Eval("BuildFloor")%></td>
</tr> <tr> <td style="width: 183px; height: 21px"> 室号:<asp:Label ID="lbl" runat="server" Text='<%#Eval("RoomNumber")%>'></asp:Label></td>
</tr>
</table> </ItemTemplate></asp:DataList>
后台代码:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
buildID = Convert.ToInt32(Request["BuildID"].ToString());
DataTable dts = new DataTable();
dts = build.GetRoomByID(roomID, buildID);
for (int i = 0; i < dts.Rows.Count; i++)
{
string strName = dts.Rows[i]["State"].ToString();
if (strName == "小定")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Blue;
}
else if (strName == "大定")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Red;
}
else if (strName == "全额")
{
Label lbl = (Label)e.Item.FindControl("lbl");
lbl.BackColor = System.Drawing.Color.Yellow;
}
strName = null; } }
}
我想实现每个<ItemTemplate>对应数据库字段"state"显示不同的颜色,但是颜色全变成最后一个遍历的数据对应的颜色,有没有办法让它们颜色不同。
for (int i = 0; i < dts.Rows.Count; i++){
string strName = dts.Rows[e.Item.ItemIndex]["State"].ToString();
}