转【GridView 和 DataList 内当鼠标移入记录行改变背景颜色问题】
一、GridView内改变记录行背景色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
GRIDVIEW里的数据行变色是这么写的
DATALIST里的就简单一些,不用也这么想
二、DataList内改变记录行的背景色
<asp:DataList runat="server" ID="dlItem" HeaderStyle-BackColor="#8fe45a" ShowHeader="true" GridLines="None" DataKeyField="id" OnItemCommand="dlItem_ItemCommand" OnDataBinding="dlItem_DataBind" OnItemDataBound="dlItem_ItemDataBound">
<HeaderTemplate>
<tr>
<td width="35" height="25" align="center" style="background-color:#8fe45a">编号
</td>
<td width="150" align="center" style="background-color:#8fe45a">国别</td>
<td align="center" style="background-color:#8fe45a; width:300px">投资机构</td>
<td width="100" align="center" style="background-color:#8fe45a">投资额度</td>
<td width="100" align="center" style="background-color:#8fe45a">修改</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr onmouseover="this.style.backgroundColor='#E6F5FA'" onmouseout="this.style.backgroundColor='' ">
<td width="35" height="25" align="center">
<%#DataBinder.Eval(Container.DataItem,"id") %>
</td>
<td width="150" align="center" ><%#DataBinder.Eval(Container.DataItem,"guobie") %></td>
<td align="center" ><%#DataBinder.Eval(Container.DataItem,"jigou") %></td>
<td width="100" align="center" ><%#DataBinder.Eval(Container.DataItem,"edu") %></td>
<td width="100" align="center" ><asp:LinkButton id="btnSelect" Text="删除" CommandName="Delete" runat="server" /> | <%--<asp:LinkButton runat="server" ID="chakan" CommandName="select" Text="查看"></asp:LinkButton>--%><asp:HyperLink ID="chakan" runat="server" Text="查看" Target="_blank" NavigateUrl='<%#"../xiangmu.aspx?id="+Eval("id") %>'></asp:HyperLink> | <asp:HyperLink ID="updata" NavigateUrl='<%#"../member.aspx?a=zhaoxm&idu="+Eval("id") %>' runat="server" Text="修改"></asp:HyperLink><%--|<asp:LinkButton runat="server" Text="修改1" OnClick="xiugai_click" ID="xiugai"></asp:LinkButton>--%></td>
</tr>
</ItemTemplate>
</asp:DataList>
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server"></asp:Label>页
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />