站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题在 Web DataGrid 中当鼠标移到某行与离开时行的颜色发生改变(结合javascript)

在 Web DataGrid 中当鼠标移到某行与离开时行的颜色发生改变(结合javascript)

摘要: 在 Web DataGrid 中当鼠标移到某行与离开时行的颜色发生改变(结合javascript)
<textarea readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible">

在head中添加javascript 代码如下:

<script lang=javascript>
function sel(i) // 鼠标移上去后执行
{
eval(i+".style.background='#CCCC66'"); // 更改行的颜色
eval(i+".style.cursor='hand'"); // 鼠标移上去后变为手形
}
function unsel(i) // 鼠标离开后执行
{
eval(i+".style.background=''");
}
function clicktr(i)
{
eval(i+".style.background=''");
window.open("Edit.aspx?param="+i,"修改","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no,left=50,top=50");
}
</script>

在DataGrid的 ItemDataBound (当数据绑定时发生)事件中:

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header)
{
string ID = e.Item.Cells[0].Text; // 这里的第一列为数据绑定中的ID值(为修改页中传递参数方便,若多参数,也可按需要增加!)
e.Item.Attributes.Add("id",ID);
e.Item.Attributes.Add("onmouseover","sel(" + ID+ ")");
e.Item.Attributes.Add("onmouseout", "unsel(" + ID+ ")");
e.Item.Attributes.Add("onclick", "clicktr(" + ID+")");
}
}

//**************************** 结束 **********************************************//

不过以上做法存在不便之处,如果在DataGrid中加个模板列,用于给用户提供选择操作(比如删除选中),
此时用上述方法就会造成每次在选择CheckBox的时候也弹出新窗口(激发了onclick事件)

比较差的解决办法:

将原先的基于行的 Attributes 改为基于列.除掉模板列外,所有列都添加属性.

比如模板列在第6列,可以这样修改 cs 文件

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header)
{
string bm = e.Item.Cells[0].Text;
for(int i=0;i<5;i++)
{
e.Item.Cells[i].Attributes.Add("id","a"+i.ToString()+bm);
e.Item.Cells[i].Attributes.Add("onmouseover","sel(" +i.ToString()+","+ bm + ")");
e.Item.Cells[i].Attributes.Add("onmouseout", "unsel(" +i.ToString()+","+ bm + ")");
e.Item.Cells[i].Attributes.Add("onclick", "clicktr(" + bm +")");
}
}
}

在 javascript 代码中:

function sel(i,ID)
{
for(var j=0;j<5;j++)
{ eval("a"+j.toString()+ID+".style.background='#CCCC66'"); eval("a"+j.toString()+ID+".style.cursor='hand'");
}
}
function unsel(i,ID)
{
for(var j=0;j<5;j++)
{ eval("a"+j.toString()+ID+".style.background=''");
}
}
function clicktr(i)
{
for(var j=0;j<5;j++)
{
eval("a"+j.toString()+i+".style.background=''");
window.open("Edit.aspx?param="+i,"修改","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no,left=50,top=50");
}

}

function sel(i) { eval("a"+i+".style.background='#CCCC66'"); eval("a"+i+".style.cursor='hand'"); } function unsel(i) { eval("a"+i+".style.background=''"); } function clicktr(i) { eval("a"+i+".style.background=''"); window.open("EditSB.aspx?sbbm="+i,"修改设备","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no,left=50,top=50"); }
</textarea>
↑返回目录
前一篇: JavaScript实用的一些技巧
后一篇: javaScript处理页面