GridView行的单击/双击事件

为GridView的行添加单击/双击事件:

一:单击/双击事件一般情况下是不能同时使用的,点加行事件有几种方式:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { for (int i = 0; i < GridView1.Rows.Count + 1; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标经过改变行颜色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFCC66';this.style.cursor= 'hand ';"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); //单双击事件这里不能同时使用,在使用母版页时使用UniqueID e.Row.Attributes.Add("onClick", "javascript:__doPostBack('" + GridView1.UniqueID + "','Select$" + e.Row.RowIndex + "');"); //e.Row.Attributes.Add("ondblclick", "javascript:__doPostBack('" + GridView1.UniqueID + "','Select$" + e.Row.RowIndex + "');"); //另一种方式 //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(GridView1, "select$" + e.Row.RowIndex); } } }

二:单击/双击事件同时使用

首先在asp.net文件中加入全局变量

<mce:script type="text/javascript"><!-- // 辅助全局变量,指示是否双击 var dbl_click = false; //--> </mce:script>

然后再行绑定事件中:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { for (int i = 0; i < GridView1.Rows.Count + 1; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;b=this.style.ForeColor;this.style.backgroundColor='#FFCC66';this.style.cursor= 'hand ';"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); //先判断是否双击了,是则置为否,设置两秒反映时间 e.Row.Attributes["onclick"] = String.Format("javascript:setTimeout(/"if(dbl_click){{dbl_click=false;}}else{{{0}}};/", 1000*0.2);", ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString(), true)); //双击实现打开新页面 e.Row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.open('PropertyAdd.aspx?Number=" + e.Row.Cells[0].Text + "&Status=" + e.Row.Cells[5].Text + "&Type=" + Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value.ToString()) + "','物业区域','height=400,width=750,left=320,top=60');", GridView1.DataKeys[e.Row.RowIndex].Value.ToString()); e.Row.Attributes["style"] = "cursor:pointer"; e.Row.Attributes["Tooltip"] = "单击选择行,双击打开详细页面"; } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值