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文件中加入全局变量

 

 

然后再行绑定事件中:

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"] = "单击选择行,双击打开详细页面"; } } }

你可能感兴趣的:(ASP.NET)