1、滑鼠移到GridView某一行時改變該行的背景色方法一:
:
做法:
雙擊GridView的OnRowDataBound事件;
在背景GridView1_RowDataBound()方法添加代**,最後代**如下所示:
前台代碼:
View Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="社會安全號碼**" DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" nRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="社會安全號碼**" HeaderText="社會安全號碼**" ReadOnly="True" SortExpression="社會安全號碼**" /> <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" /> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" /> <asp:BoundField DataField="郵政編**" HeaderText="郵政編**" SortExpression="郵政編**" /> </Columns> <footerstyle. backcolor="White" forecolor="#000066" /> <rowstyle. forecolor="#000066" /> <selectedrowstyle. backcolor="#669999" font-bold="True" forecolor="White" /> <pagerstyle. backcolor="White" forecolor="#000066" horizontalalign="Left" /> <headerstyle. backcolor="#006699" font-bold="True" forecolor="White" /> </asp:GridView>
後台代碼:
View Code
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"); } } }