欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net GridView和DataList实现鼠标移到行行变色

程序员文章站 2022-03-18 09:21:12
在gridview控件的rowdatabound事件里添加以下代码 if (e.row.rowtype == datacontrolrowtype.datarow) { /...
在gridview控件的rowdatabound事件里添加以下代码
if (e.row.rowtype == datacontrolrowtype.datarow)
{
//当鼠标移到行上时更改背景色
e.row.attributes.add("onmouseover", "c=this.style.backgroundcolor;this.style.backgroundcolor='#ee82ee'");
//当鼠标移开时还原背景色
e.row.attributes.add("onmouseout", "this.style.backgroundcolor=c");
}
在设计页面添加了datalist控件后,我在使用datalist绑定数据时是通过单元格来绑定的,因此鼠标效果就在源代码页面去实现,如下例所示
<asp:datalist id="datalist1" runat="server" borderwidth="1" >
<itemtemplate>
<tr onmouseover="this.style.backgroundcolor='#8ec26f'" onmouseout="this.style.backgroundcolor=''" >
<td>
<asp:label id="label1" runat="server" text='<%# databinder.eval(container, "dataitem.id") %>'></asp:label>
</td>
<td>
<asp:label id="label2" runat="server" text='<%# databinder.eval(container, "dataitem.area") %>'></asp:label>
</td>
</tr>
</itemtemplate>
<headertemplate>
header1</td>
<td>header2
</headertemplate>
</asp:datalist>