日期:2014-05-17  浏览次数:20386 次

急急急!!datalist控件如何读取数据库中的标示显示不同的颜色
前台代码:
 <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();
。。。。。。
}