CheckBox改变GridView行的颜色(转载)
2007-07-08 22:46
399 查看
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>
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>
相关文章推荐
- CheckBox改变GridView行的颜色(1)
- 模板列中CheckBox改变GridView行的颜色(2) (.cs页面实现)
- GridView中点击CheckBox选中一行来改变此行的颜色
- GridView中点击CheckBox选中一行来改变此行的颜色
- Gridview中CheckBox列:赋值、改变值触发事件
- 根据绑定的值的不同改变gridview里某一单元格的字体或背景颜色
- Dev-Gridview中根据条件改变行颜色及单元颜色
- 操作gridview,选中行时改变背景色且checkbox被选中,隔行变色,全选变色
- Android GridView子元素item按击交互设计:背景颜色改变
- css中改变gridview中HyperLinkField字体颜色的方法
- aspx GridView的RowDataBound中改变单元格大小,颜色,汇总等
- ASPxGridview根据条件将符合条件的行颜色改变等
- ASPxGridview根据条件将符合条件的行颜色改变等
- GridView鼠标经过时颜色改变
- repeater单双行颜色不同,gridview repeater DataList 鼠标经过改变背景颜色
- IOS - 6\7下UINavigationBar的颜色的方法改变 ——转载http://www.th7.cn/Program/IOS/201310/155057.shtml
- android gridview点击子选项动态改变item背景颜色
- ASPxGridview根据条件将符合条件的行颜色改变
- net4:GridView中的重要操作(添加checkbox,以及鼠标动作,行颜色等)
- 鼠标停留在GridView某一行时行的颜色改变