代码如下:
前台代码:
<table align="center" border="1" bordercolor="#66CCCC" cellpadding="4" cellspacing="0" style="border-collapse:collapse">
<asp:Repeater ID="rptStockpile" runat="server"
onitemdatabound="rptStockpile_ItemDataBound">
<HeaderTemplate>
<tr class="pagetitle" bgcolor="#009999">
<th width="80" scope="col">产品编号</th>
<th width="150" scope="col">产品名称</th>
<th width="150" scope="col">产品类型</th>
<th width="80" scope="col">产品数量</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class='change1 pagecontent' id="r">
<td align="center"><asp:Label ID="ID" runat=server text='<%# ((DataRowView)Container.DataItem)["stockID"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="Name" runat=server text='<%# ((DataRowView)Container.DataItem)["thingName"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="tyName" runat=server text='<%# ((DataRowView)Container.DataItem)["thingTypeName"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="Num" runat=server Text='<%# ((DataRowView)Container.DataItem)["stockNum"]%>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>后台代码:
protected void rptStockpile_ItemDataBound(object sender, RepeaterItemEventArgs e)
{ DataTable table = stockpilebll.GetAllStockpiles(); for (int i = 0; i < table.Rows.Count; i++)
{
if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"]))
{
for (int j = 0; j < rptStockpile.Items.Count; j++)
{
((Label)e.Item.FindControl("ID")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Name")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("tyName")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Num")).ForeColor = System.Drawing.Color.Red;
}
}
}
}
需要做一个报警的功能,当if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"])) 这个条件成立时将改变Repeater控件里面每项的颜色,
上面代码可以改变从第二行开始的颜色,如果数据库只放一行数据就不会判断,这是为什么放2条数据只会判断第二条开始。 放的数据都是 thingNum>stockNum 的 我是这样想的 哪个高手能指点下啊
前台代码:
<table align="center" border="1" bordercolor="#66CCCC" cellpadding="4" cellspacing="0" style="border-collapse:collapse">
<asp:Repeater ID="rptStockpile" runat="server"
onitemdatabound="rptStockpile_ItemDataBound">
<HeaderTemplate>
<tr class="pagetitle" bgcolor="#009999">
<th width="80" scope="col">产品编号</th>
<th width="150" scope="col">产品名称</th>
<th width="150" scope="col">产品类型</th>
<th width="80" scope="col">产品数量</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class='change1 pagecontent' id="r">
<td align="center"><asp:Label ID="ID" runat=server text='<%# ((DataRowView)Container.DataItem)["stockID"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="Name" runat=server text='<%# ((DataRowView)Container.DataItem)["thingName"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="tyName" runat=server text='<%# ((DataRowView)Container.DataItem)["thingTypeName"]%>'></asp:Label></td>
<td align="center"><asp:Label ID="Num" runat=server Text='<%# ((DataRowView)Container.DataItem)["stockNum"]%>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>后台代码:
protected void rptStockpile_ItemDataBound(object sender, RepeaterItemEventArgs e)
{ DataTable table = stockpilebll.GetAllStockpiles(); for (int i = 0; i < table.Rows.Count; i++)
{
if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"]))
{
for (int j = 0; j < rptStockpile.Items.Count; j++)
{
((Label)e.Item.FindControl("ID")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Name")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("tyName")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Num")).ForeColor = System.Drawing.Color.Red;
}
}
}
}
需要做一个报警的功能,当if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"])) 这个条件成立时将改变Repeater控件里面每项的颜色,
上面代码可以改变从第二行开始的颜色,如果数据库只放一行数据就不会判断,这是为什么放2条数据只会判断第二条开始。 放的数据都是 thingNum>stockNum 的 我是这样想的 哪个高手能指点下啊
{
DataTable table = GetAllStockPiles();
for (int i = 0; i < table.Rows.Count; i++)
{
if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"]))
{
//这里判断有误,如果只有一条数据的时候,你这个时候的rptStockpile.Items.Count为零
//有数据,也是从第二个开始的因为,第一次rptStockpile.Items.Count为零,第二次才为一
for (int j = 0; j < rptStockpile.Items.Count; j++)
{
((Label)e.Item.FindControl("ID")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Name")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("tyName")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Num")).ForeColor = System.Drawing.Color.Red;
}
}
}
不知道LZ加这个for循环做什么? 这加了和没有加有意义么?反而降低了性能... ...
半个小时,终于找到了有用的一点,让我自己也学习了,谢谢
ItemDataBound:在Repeater中的某项数据绑定之后,但在呈现于页面上之前发生;[b][/b]
我要判断的是如果if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"])){这个条件成立的话就要Repeater中显示的项显示红色 如果不成立就正常显示不变颜色
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataTable table = stockpilebll.GetAllStockpiles(); for (int i = 0; i < table.Rows.Count; i++)
{
if (Convert.ToInt32(table.Rows[i]["thingNum"]) > Convert.ToInt32(table.Rows[i]["stockNum"]))
{
((Label)e.Item.FindControl("ID")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Name")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("tyName")).ForeColor = System.Drawing.Color.Red;
((Label)e.Item.FindControl("Num")).ForeColor = System.Drawing.Color.Red;
}
}
}
}
刚刚换了用PreRender这个事件就可以了 代码没有错 是没加载所以才会出现那样的情况
谢谢大家的支持 给分咯