CheckBox改变GridView行的颜色(转载)

本文介绍了一种在ASP.NET GridView中实现行高亮的方法,包括鼠标悬停、点击及使用复选框改变行背景色的技术细节。特别是针对带有交替颜色行的GridView,提供了JavaScript函数以确保即使在交替行的情况下也能正确实施高亮。

Highlighting GridView rows using onmouseover, onclick and using checkbox are all similar features. All are pretty simple to implement. The main problem rises when using the same technique on a GridView with alternate rows with different color. Let's check out how this can be done.

First, here is the HTML code for the GridView control.

<

asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">

 

<Columns>

 

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" onclick="changeColor(this)" />

</ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateField HeaderText="Name">

<ItemTemplate>

<asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>' />

</ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateField HeaderText="Description">

<ItemTemplate>

<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' />

</ItemTemplate>

</asp:TemplateField>

 

</Columns>

<FooterStyle BackColor="Tan" />

<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

<HeaderStyle BackColor="Tan" Font-Bold="True" />

<AlternatingRowStyle BackColor="PaleGoldenrod" />

 

</asp:GridView>

The ChangeColor function is fired whenever a checkbox is clicked.

One important point to note about Alternate rows is that there is no color assigned to them. They get the color from the style property of the table. In other words we only assign the style to the even numbered rows.

<

script language="javascript" type="text/javascript">

var

color = '';

function

changeColor(obj)

{

var rowObject = getParentRow(obj);

 

var parentTable = document.getElementById("gvCategories");

 

if(color == '')

{

color = getRowColor();

}

 

if(obj.checked)

{

rowObject.style.backgroundColor = 'Yellow';

}

else

{

 

rowObject.style.backgroundColor = color;

color = '';

}

 

// private method

function getRowColor()

{

if(rowObject.style.backgroundColor == '') return parentTable.style.backgroundColor;

else return rowObject.style.backgroundColor;

}

 

}

// This method returns the parent row of the object

function

getParentRow(obj)

{

do

{

obj = obj.parentElement;

}

while(obj.tagName != "TR")

 

return obj;

}

 

</

script>

转载于:https://www.cnblogs.com/ajaxworld/archive/2007/07/08/810571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值