当DataGrid合并行是,怎么用JavaScript写行变色效果。

本文介绍了一种在DataGrid中实现行选中时颜色变化的方法,并提供了详细的C#与JavaScript代码实现。通过监听鼠标移入、移出及点击事件,可以改变DataGrid中被选中行的背景色。
ContractedBlock.gifExpandedBlockStart.gif全并行的DataGrid
 1None.gifprivate void grdSearchResults_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            ListItemType itemType = (ListItemType)e.Item.ItemType;
 4InBlock.gif
 5InBlock.gif            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 7InBlock.gif                
 8InBlock.gif                // Add attributes to the <td>.
 9InBlock.gif                string tableRowId = grdSearchResults.ClientID +  "_row" + e.Item.ItemIndex.ToString();
10InBlock.gif                e.Item.Attributes.Add("id", tableRowId);
11InBlock.gif                object checkBox = e.Item.FindControl("chkChoice");
12InBlock.gif                if(checkBox != null)
13ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
14InBlock.gif                    string clientId = ((CheckBox)checkBox).ClientID;
15InBlock.gif                    e.Item.Attributes.Add("onMouseMove""Javascript:chkSelect_OnMouseMove('" + tableRowId + "','" + clientId + "',"+e.Item.ItemIndex+")");
16InBlock.gif                    e.Item.Attributes.Add("onMouseOut""Javascript:chkSelect_OnMouseOut('" + tableRowId + "','" + clientId + "'," + e.Item.ItemIndex + ")");
17InBlock.gif                    ((CheckBox)checkBox).Attributes.Add("onClick""Javascript:chkSelect_OnClick('" + tableRowId + "','" + clientId + "'," + e.Item.ItemIndex + ")");
18ExpandedSubBlockEnd.gif                }

19ExpandedSubBlockEnd.gif            }

20ExpandedBlockEnd.gif        }
ContractedBlock.gifExpandedBlockStart.gif相应的JavaScript
  1ExpandedBlockStart.gifContractedBlock.gif /**//*
  2InBlock.gif            描述:鼠标移入事件
  3InBlock.gif            作者:南守拥
  4InBlock.gif            时间:2006年12月13日
  5InBlock.gif            参数:tableRow为TR的ID,checkBox此行的选择CheckBox,rowIndex行号。                                                    
  6ExpandedBlockEnd.gif            */

  7None.gif            function chkSelect_OnMouseMove(tableRow, checkBox,rowIndex)
  8ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
  9InBlock.gif                var tr = document.getElementById(tableRow);//等到当前行
 10InBlock.gif                var strTableRowBase = tableRow.substr(0,20);//当前行ID的不变部分    
 11InBlock.gif                var chbselect;
 12InBlock.gif                var bgColor;                                //选择CheckBox
 13InBlock.gif                var rowspan = tr.cells[0].rowSpan;            //当前行的RowSpan
 14InBlock.gif                while(rowspan == 1)                            //如果当前行不是合并行的起点
 15ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 16InBlock.gif                    rowIndex = rowIndex - 1;                //行序号少一
 17InBlock.gif                    var strTableRow = strTableRowBase + rowIndex;//上一行的行ID
 18InBlock.gif                    tr = document.getElementById(strTableRow);//得到上一行
 19InBlock.gif                    rowspan = tr.cells[0].rowSpan;//得到上一行的RowSpan
 20ExpandedSubBlockEnd.gif                }
//当找行的合并行起点时退出,当前行的信息有:tr、rowIndex、rowspan chbBase+rowIndex+2+chbLast
 21InBlock.gif                
 22InBlock.gif                //找到合并行起点的选择CheckBox
 23InBlock.gif                var chbBase = checkBox.substr(0,21);
 24InBlock.gif                var chbLast = checkBox.substr(22,32);
 25InBlock.gif                var num = rowIndex + 2;//这里很危险,是看Html直接定的。
 26InBlock.gif                var chbSelectID = chbBase + num + chbLast;
 27InBlock.gif                var chbselect = document.getElementById(chbSelectID);
 28InBlock.gif                if(chbselect != null)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 30InBlock.gif                    if(chbselect.checked == false)//如果没被选择则变色。
 31InBlock.gif                        bgColor = "#ffffcc";
 32ExpandedSubBlockEnd.gif                }

 33InBlock.gif                
 34InBlock.gif                for(i = rowIndex;i<rowIndex + rowspan;i++)//从起点行开始,给每行设置bgColor
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 36InBlock.gif                    var strTableRow = strTableRowBase + i;
 37InBlock.gif                    tr = document.getElementById(strTableRow);
 38InBlock.gif                    tr.style.backgroundColor = bgColor;
 39ExpandedSubBlockEnd.gif                }

 40ExpandedBlockEnd.gif            }
    
 41None.gif            
 42ExpandedBlockStart.gifContractedBlock.gif            /**//*
 43InBlock.gif            描述:鼠标移出事件
 44InBlock.gif            作者:南守拥
 45InBlock.gif            时间:2006年12月13日
 46InBlock.gif            参数:tableRow为TR的ID,checkBox此行的选择CheckBox,rowIndex行号。                                                    
 47ExpandedBlockEnd.gif            */

 48None.gif            function chkSelect_OnMouseOut(tableRow, checkBox, rowIndex)
 49ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
 50InBlock.gif                var tr = document.getElementById(tableRow);//等到当前行
 51InBlock.gif                var strTableRowBase = tableRow.substr(0,20);//当前行ID的不变部分    
 52InBlock.gif                var chbselect;
 53InBlock.gif                var bgColor;                                //选择CheckBox
 54InBlock.gif                var rowspan = tr.cells[0].rowSpan;            //当前行的RowSpan
 55InBlock.gif                while(rowspan == 1)                            //如果当前行不是合并行的起点
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 57InBlock.gif                    rowIndex = rowIndex - 1;                //行序号少一
 58InBlock.gif                    var strTableRow = strTableRowBase + rowIndex;//上一行的行ID
 59InBlock.gif                    tr = document.getElementById(strTableRow);//得到上一行
 60InBlock.gif                    rowspan = tr.cells[0].rowSpan;//得到上一行的RowSpan
 61ExpandedSubBlockEnd.gif                }
//当找行的合并行起点时退出,当前行的信息有:tr、rowIndex、rowspan chbBase+rowIndex+2+chbLast
 62InBlock.gif                
 63InBlock.gif                //找到合并行起点的选择CheckBox
 64InBlock.gif                var chbBase = checkBox.substr(0,21);
 65InBlock.gif                var chbLast = checkBox.substr(22,32);
 66InBlock.gif                var num = rowIndex + 2;//这里很危险,是看Html直接定的。
 67InBlock.gif                var chbSelectID = chbBase + num + chbLast;
 68InBlock.gif                var chbselect = document.getElementById(chbSelectID);
 69InBlock.gif                if(chbselect != null)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 71InBlock.gif                    if(chbselect.checked == false)//如果没被选择则变色。
 72InBlock.gif                            bgColor = "#ffffff";
 73ExpandedSubBlockEnd.gif                }

 74InBlock.gif                
 75InBlock.gif                for(i = rowIndex;i<rowIndex + rowspan;i++)//从起点行开始,给每行设置bgColor
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 77InBlock.gif                    var strTableRow = strTableRowBase + i;
 78InBlock.gif                    tr = document.getElementById(strTableRow);
 79InBlock.gif                    tr.style.backgroundColor = bgColor;
 80ExpandedSubBlockEnd.gif                }
                        
 81ExpandedBlockEnd.gif            }

 82None.gif            
 83None.gif            
 84ExpandedBlockStart.gifContractedBlock.gif            /**//*
 85InBlock.gif            描述:鼠标移出事件
 86InBlock.gif            作者:南守拥
 87InBlock.gif            时间:2006年12月13日
 88InBlock.gif            参数:tableRow为TR的ID,checkBox此行的选择CheckBox,rowIndex行号。                                                    
 89ExpandedBlockEnd.gif            */

 90None.gif            function chkSelect_OnClick(tableRow, checkBox, rowIndex)
 91ExpandedBlockStart.gifContractedBlock.gif            dot.gif{            
 92InBlock.gif                var chbselect = document.getElementById(checkBox);
 93InBlock.gif                var strTableRowBase = tableRow.substr(0,20);//当前行ID的不变部分    
 94InBlock.gif
 95InBlock.gif                if(chbselect != null)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 97InBlock.gif                    var bgColor;
 98InBlock.gif                    if(rowIndex%2 == 0)
 99InBlock.gif                        bgColor = "#ffffff";
100InBlock.gif                    else
101InBlock.gif                        bgColor = "#f5f5f5";
102InBlock.gif                    if(chbselect.checked == true)
103InBlock.gif                        bgColor = "#b0c4de";                    
104ExpandedSubBlockEnd.gif                }

105InBlock.gif                var tr = document.getElementById(tableRow);
106InBlock.gif                var rowspan = tr.cells[0].rowSpan;
107InBlock.gif                for(i = rowIndex;i<rowIndex + rowspan;i++)//从起点行开始,给每行设置bgColor
108ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
109InBlock.gif                    var strTableRow = strTableRowBase + i;
110InBlock.gif                    tr = document.getElementById(strTableRow);
111InBlock.gif                    tr.style.backgroundColor = bgColor;
112ExpandedSubBlockEnd.gif                }
                        
113ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/12/13/591303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值