满分求解-关于GridView的行高亮问题 GRIDVIEW要实现的功能是单击:单击某一行 高亮显示,单击另一行 之前一行取消高亮,当前行高亮CTRL+单击:单击过的行都高亮显示SHIFT+单击:单击过的两行 之间的所有行高亮显示其实说穿了就是类似windows的文件操作那种的功能哪位高手指点下怎么做?当然有代码最好小弟拜谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='';this.style.color=''"); e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#50638D';this.style.color='#FFFFFF'");e.Row.Attributes.Add("onclick", "style.backgroundColor='red"); } }或 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);"); e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);"); } } var _oldColor; function SetNewColor(source) { _oldColor=source.style.backgroundColor; source.style.backgroundColor='#E8F4FF'; } function SetOldColor(source) { source.style.backgroundColor=_oldColor; } .Row.Attributes.Add("onclick", "tog(this,'#00A9FF'); var tgs; function tog(n,flags) { if(tgs) { tgs.style.background=''; } n.style.background=''; tgs=n; } protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='';this.style.color=''"); e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#50638D';this.style.color='#FFFFFF'"); 这个比较简单咯。。 也好理解 第一的个问题还是简单的,关键是后两个问题:CTRL+单击:单击过的行都高亮显示 SHIFT+单击:单击过的两行 之间的所有行高亮显示 这两个问题有难度,楼上答的都是第一个问题!!!! Jquery简单,定义好css只要几句话<script type="text/javascript"> $(function() { $("#tblName tr:gt(0):odd").attr("class", "odd"); $("#tblName tr:gt(0):even").attr("class", "even"); $("#tblName tr:gt(0)").hover(function() { $(this).addClass('mouseover'); }, function() { $(this).removeClass('mouseover'); }); }) </script> var currTR=this; function overTR(){ var trs = document.getElementById("setTbl").getElementsByTagName("tr"); for(var i in trs){ trs[i].onmouseover = function(){ var oldBgColor=this.bgColor; var oldFontColor=this.style.color; this.bgColor = "#0000FF"; this.style.color="#FFFFFF"; if(currTR != this&&currTR.tagName=="TR"){currTR.bgColor = oldBgColor; currTR.style.color=oldFontColor;} currTR = this; } } } window.onload = function(){overTR();} JS的行高亮CTRL+单击不是有个什么 ctrlkey&click 调用函数就可以了 呵呵 没有试过 我去试试 去吃了会饭,终于ok .hl,.hl a {background-color:#326496;color:#FFFFFF;}将下面的代码放在body后面,注意gridview的id(function() { var lastIndex=-1; var tb=document.getElementById("tb1");//表格的id可以写成"<%=GridView1.ClientID%>" for(var i=1;i<tb.rows.length;i++) {//从1开始,假设有列头 (function(row,i){ row.onclick=function(e) { e=window.event ||e; e.cancelBubble = true; if(e.shiftKey) { if(lastIndex==-1) { this.className="hl"; lastIndex=this.rowIndex; }else if(lastIndex==this.rowIndex) { this.className="";lastIndex=-1; } else { var start=Math.min(this.rowIndex,lastIndex); var end=Math.max(this.rowIndex,lastIndex); for(var i=start;i<end;i++) { tb.rows[i].className="hl"; } lastIndex=this.rowIndex; } }else if(e.ctrlKey) { this.className="hl"; } else { for(var i=0;i<tb.rows.length;i++) { tb.rows[i].className=""; } this.className="hl"; lastIndex=this.rowIndex; } } })(tb.rows[i],i); } })(); window.onload=function() { var lastIndex=-1; var tb=document.getElementById("setTbl"); for(var i=1;i<tb.rows.length;i++) {//从1开始,假设有列头 (function(row,i){ row.onclick=function(e) { e=window.event ||e; e.cancelBubble = true; if(e.shiftKey) { if(lastIndex==-1) { this.className="hl"; lastIndex=this.rowIndex; }else if(lastIndex==this.rowIndex) { this.className="";lastIndex=-1; } else { var start=Math.min(this.rowIndex,lastIndex); var end=Math.max(this.rowIndex,lastIndex); for(var i=start;i<end;i++) { tb.rows[i].className="hl"; } lastIndex=this.rowIndex; } }else if(e.ctrlKey) { this.className="hl"; } else { for(var i=0;i<tb.rows.length;i++) { tb.rows[i].className=""; } this.className="hl"; lastIndex=this.rowIndex; } } })(tb.rows[i],i); } }呵呵我这样调用? sohighthesky前辈的JS的确是能实现我要的功能的但是稍稍两点不明白 由于我这边是两个GV如图我如何让两个GV的行同时都具有变色功能?还有一点是 选中的高亮行将来客户可能需要做一个删除的操作 如何捕捉已经高亮行的ID? 1.把那个写到一个方法里,传入gridview的id2.遍历tb,获取className为hl的 再 onclick 事件 的 写,,,取到所有高亮行的 ID!!!protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onclick", "row_click(this)"); } } function row_click(Row){ 这ROW是被你点击后 高亮的行! row.cells[0].innerHTML //取 主键}这里还有一问题,你必须 编辑 GRIDVIEW的所有行,判断 是否高亮!(你用SHIFT拉的高亮)高亮就取值,最后把值 串起来,赋值给隐藏控件,后台取值 删除就好! 1,写到一个方法里,那应该只能分别高亮的吧 由于两个gridview是放在一起的v,看起来好像是一个的,所以之前我也没注意到- -2,我再试下 简单的,进来看看,直接结贴。 三层简单问题 在ASP.NET中应用AJAX的简单问题 vs2003 如何播放asf视频 DataGrid asp.net 2.0问题!100分! 如何取得当前datagrid中第一行第二列的值? DataGrid的HyperLinkColumn字段问题? 调用其他项目用户控件失败 好消息: 有偿兼职开发网站流量统计或进销存系统(C#)! 如果有兴趣跟我联系? 怎么样能做到本机电脑上的QQ号 <meta content="<%=webTitle %>"/> 运行后变成 content="<%=webTitle %>" 求解
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='';this.style.color=''");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#50638D';this.style.color='#FFFFFF'");
e.Row.Attributes.Add("onclick", "style.backgroundColor='red"); } }或
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");
e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");
}
}
var _oldColor;
function SetNewColor(source)
{
_oldColor=source.style.backgroundColor;
source.style.backgroundColor='#E8F4FF';
}
function SetOldColor(source)
{
source.style.backgroundColor=_oldColor;
} .Row.Attributes.Add("onclick", "tog(this,'#00A9FF');
var tgs;
function tog(n,flags)
{
if(tgs)
{
tgs.style.background='';
}
n.style.background='';
tgs=n;
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='';this.style.color=''");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#50638D';this.style.color='#FFFFFF'"); 这个比较简单咯。。 也好理解
SHIFT+单击:单击过的两行 之间的所有行高亮显示 这两个问题有难度,楼上答的都是第一个问题!!!!
<script type="text/javascript">
$(function() {
$("#tblName tr:gt(0):odd").attr("class", "odd");
$("#tblName tr:gt(0):even").attr("class", "even");
$("#tblName tr:gt(0)").hover(function() {
$(this).addClass('mouseover');
}, function() {
$(this).removeClass('mouseover');
});
})
</script>
function overTR(){
var trs = document.getElementById("setTbl").getElementsByTagName("tr");
for(var i in trs){
trs[i].onmouseover = function(){
var oldBgColor=this.bgColor;
var oldFontColor=this.style.color;
this.bgColor = "#0000FF";
this.style.color="#FFFFFF";
if(currTR != this&&currTR.tagName=="TR"){currTR.bgColor = oldBgColor; currTR.style.color=oldFontColor;}
currTR = this;
}
}
}
window.onload = function(){overTR();} JS的行高亮
CTRL+单击
不是有个什么 ctrlkey&click 调用函数就可以了 呵呵 没有试过 我去试试
var lastIndex=-1;
var tb=document.getElementById("tb1");//表格的id可以写成"<%=GridView1.ClientID%>"
for(var i=1;i<tb.rows.length;i++) {//从1开始,假设有列头
(function(row,i){
row.onclick=function(e) {
e=window.event ||e;
e.cancelBubble = true;
if(e.shiftKey) {
if(lastIndex==-1) {
this.className="hl";
lastIndex=this.rowIndex;
}else if(lastIndex==this.rowIndex) {
this.className="";lastIndex=-1;
} else {
var start=Math.min(this.rowIndex,lastIndex);
var end=Math.max(this.rowIndex,lastIndex);
for(var i=start;i<end;i++) {
tb.rows[i].className="hl";
}
lastIndex=this.rowIndex;
}
}else if(e.ctrlKey) {
this.className="hl";
} else {
for(var i=0;i<tb.rows.length;i++) {
tb.rows[i].className="";
}
this.className="hl";
lastIndex=this.rowIndex;
}
}
})(tb.rows[i],i);
}
})();
var lastIndex=-1;
var tb=document.getElementById("setTbl");
for(var i=1;i<tb.rows.length;i++) {//从1开始,假设有列头
(function(row,i){
row.onclick=function(e) {
e=window.event ||e;
e.cancelBubble = true;
if(e.shiftKey) {
if(lastIndex==-1) {
this.className="hl";
lastIndex=this.rowIndex;
}else if(lastIndex==this.rowIndex) {
this.className="";lastIndex=-1;
} else {
var start=Math.min(this.rowIndex,lastIndex);
var end=Math.max(this.rowIndex,lastIndex);
for(var i=start;i<end;i++) {
tb.rows[i].className="hl";
}
lastIndex=this.rowIndex;
}
}else if(e.ctrlKey) {
this.className="hl";
} else {
for(var i=0;i<tb.rows.length;i++) {
tb.rows[i].className="";
}
this.className="hl";
lastIndex=this.rowIndex;
}
}
})(tb.rows[i],i);
}
}呵呵我这样调用?
我如何让两个GV的行同时都具有变色功能?还有一点是 选中的高亮行将来客户可能需要做一个删除的操作 如何捕捉已经高亮行的ID?
2.遍历tb,获取className为hl的
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "row_click(this)"); } } function row_click(Row)
{
这ROW是被你点击后 高亮的行! row.cells[0].innerHTML //取 主键
}
这里还有一问题,你必须 编辑 GRIDVIEW的所有行,判断 是否高亮!(你用SHIFT拉的高亮)
高亮就取值,最后把值 串起来,赋值给隐藏控件,后台取值 删除就好!