Gridview应用技巧——如何为行添加事件
2009-05-14 12:06 观海看云 阅读(181) 评论(0) 编辑 收藏 举报下面的代码实现了如何为Gridview添加鼠标经过、离开、单击、双击的事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("BorderColor ", "#d2d2d2");
e.Row.Style.Add("Height","8px");
e.Row.Style.Add("FontSize","8");
e.Row.Style.Add("VerticalAlign", "Middle");
if (e.Row.RowType == DataControlRowType.DataRow)
{
string SelectID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();//((ClientList)(e.Row.DataItem)).ClientID;
if (!String.IsNullOrEmpty(SelectID))
{
//鼠标双击Row时的效果
e.Row.Attributes.Add("ondblclick", "javascript:window.location.href='"+(string)ViewState["Href"]+"?ID=" + SelectID+ "';");
//鼠标经过Row时的效果
e.Row.Attributes.Add("onmouseover", "this.classname=onColor2(this)");
//鼠标离开Row时的效果
e.Row.Attributes.Add("onmouseout", "this.classname=offColor2(this)");
//鼠标键Row时的效果
e.Row.Attributes.Add("onmousedown", "this.classname=downColor(this," + SelectID + ")");
}
}
}
下面是JavaScript方法
function onColor2(td)
{
// td.style.backgroundColor="#c0c0c0";
// td.style.fontWeight="bold";
// td.style.fontStyle="italic";
td.style.color="#0600FF";
}
function offColor2(td)
{
// td.style.backgroundColor="";
// td.style.fontWeight='';
// td.style.fontStyle='';
td.style.color='';
//td.style.backgroundColor='';
}
function downColor(td,a)
{
// td.style.backgroundColor="";
// td.style.fontWeight='';
// td.style.fontStyle='';
var rowindex = td.rowIndex;
for(var i=1;i <td.parentElement.rows.length;i++)
{
if(i!=rowindex)
{
//当选中其它行时,要把另外所有行的背景色去掉
td.parentElement.rows[i].style.color='';
td.parentElement.rows[i].style.backgroundColor = '';
}
}
document.getElementById("hid").value=a;
td.style.color='#0600FF';
td.style.backgroundColor='#FFDE59';
}
出处:http://www.cnblogs.com/zhangtao/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器