功能: 单击选中行,双击打开详细页面 
说明:单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间 ;当双击时,通过全局变量 dbl_click 来取消单击事件的响应 
常见处理行方式会选择在 RowDataBound/ItemDataBound 中处理,这里我选择 Page.Render 中处理,至少基于以下考虑 
1、RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发 假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中 
2、并且我们希望使用 ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法 进行安全脚本的注册,而后者需要在页的 Render 阶段中才能处理 .aspx(直接运行)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %> <%--http://community.csdn.net/Expert/TopicView3.asp?id=5767096--%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadGridViewProductData();
LoadDataGridProductData();
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
/*
当然可以在这里进行客户端脚本绑定,
但是,我选择在重载页的 Render 方法中处理,因为
1. RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发
假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中
2. 并且我们希望使用
ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法
进行安全脚本的注册,而后者需要在页的 Render 阶段中才能处理
*/
} protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// 隐藏辅助按钮列
int cellIndex = 0;
e.Item.Cells[cellIndex].Attributes["style"] = "display:none";
} void LoadGridViewProductData()
{
DataTable dt = CreateSampleProductData(); GridView1.DataSource = dt;
GridView1.DataBind();
} void LoadDataGridProductData()
{
DataTable dt = CreateSampleProductData(); DataGrid1.DataSource = dt;
DataGrid1.DataBind();
} #region sample data static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable("Products"); tbl.Columns.Add("ProductID", typeof(int));
tbl.Columns.Add("ProductName", typeof(string));
tbl.Columns.Add("UnitPrice", typeof(decimal));
tbl.Columns.Add("CategoryID", typeof(int)); tbl.Rows.Add(1, "Chai", 18, 1);
tbl.Rows.Add(2, "Chang", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
tbl.Rows.Add(4, "Chef Anton’s Cajun Seasoning", 22, 2);
tbl.Rows.Add(5, "Chef Anton’s Gumbo Mix", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
tbl.Rows.Add(48, "Chocolade", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku", 20, 3); return tbl;
} #endregion protected override void Render(HtmlTextWriter writer)
{
// GridView
foreach (GridViewRow row in GridView1.Rows) {
if (row.RowState == DataControlRowState.Edit) { // 编辑状态
row.Attributes.Remove("onclick");
row.Attributes.Remove("ondblclick");
row.Attributes.Remove("style");
row.Attributes["title"] = "编辑行";
continue;
}
if (row.RowType == DataControlRowType.DataRow) {
// 单击事件,为了响应双击事件,需要延迟单击响应,根据需要可能需要增加延迟
// 获取ASP.NET内置回发脚本函数,返回 __doPostBack(<<EventTarget>>, <<EventArgument>>)
// 可直接硬编码写入脚本,不推荐
row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString(), true));
// 双击,设置 dbl_click=true,以取消单击响应
row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.open(’DummyProductDetail.aspx?productid={0}’);", GridView1.DataKeys[row.RowIndex].Value.ToString());
//
row.Attributes["style"] = "cursor:pointer";
row.Attributes["title"] = "单击选择行,双击打开详细页面";
}
} // DataGrid
foreach (DataGridItem item in DataGrid1.Items) {
if (item.ItemType == ListItemType.EditItem) {
item.Attributes.Remove("onclick");
item.Attributes.Remove("ondblclick");
item.Attributes.Remove("style");
item.Attributes["title"] = "编辑行";
continue;
}
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) {
//单击事件,为了响应双击事件,延迟 1 s,根据需要可能需要增加延迟
// 获取辅助的支持回发按钮
// 相对而言, GridView 支持直接将 CommandName 作为 <<EventArgument>> 故不需要辅助按钮
Button btnHiddenPostButton = item.FindControl("btnHiddenPostButton") as Button;
item.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(btnHiddenPostButton, null));
// 双击
// 双击,设置 dbl_click=true,以取消单击响应
item.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.open(’DummyProductDetail.aspx?productid={0}’);", DataGrid1.DataKeys[item.ItemIndex].ToString()); //
item.Attributes["style"] = "cursor:pointer";
item.Attributes["title"] = "单击选择行,双击打开详细页面";
}
} base.Render(writer);
}
</script> <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ASP.NET DEMO15: GridView 行单击与双击事件2</title>
<script>
// 辅助全局变量,指示是否双击
var dbl_click = false;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>功能:</h3>
<li>单击选中行</li>
<li>双击打开详细页面</li>
<h3>说明:</h3>
<ul>
<li>这是<a href="GridView/DataGrid http://www.cnblogs.com/Jinglecat/archive/2007/09/20/900645.html"> ASP.NET DEMO 15: 同时支持行单击和双击事件</a>的改进版本</li>
<li>单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间</li>
<li>当双击时,通过全局变量 dbl_click 来取消单击事件的响应</li>
<li>常见处理行方式会选择在 RowDataBound/ItemDataBound 中处理,这里我选择 Page.Render 中处理,至少基于以下考虑
<li style="padding-left:20px; list-style-type:square">RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发
假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中</li>
<li style="padding-left:20px; list-style-type:square">并且我们希望使用
ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法
进行安全脚本的注册,而后者需要在页的 Render 阶段中才能处理</li>
</li>
<li>关于“DataGrid中采取的辅助按钮支持回发”见<a href="http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818394.html">ASP.NET DEMO8: 为 GridView 每行添加服务器事件</a>
</ul>
<br />
<input type="button" id="Button1" value="Rebind" onclick="location.href=location.href;" />
<div style="float:left">
<h3>GridView Version</h3>
<asp:GridView ID="GridView1" DataKeyNames="ProductID" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<SelectedRowStyle BackColor="CadetBlue" />
<Columns>
<asp:TemplateField HeaderText="ProductName" >
<ItemTemplate>
<%# Eval("ProductName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProductName" runat="server" Text=’<%# Bind("ProductName") %>’ />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
</Columns>
</asp:GridView></div>
<div style="float:left;padding-left:100px;">
<h3>DataGrid Version</h3>
<asp:DataGrid ID="DataGrid1" DataKeyField="ProductID" runat="server" AutoGenerateColumns="False" OnItemDataBound="DataGrid1_ItemDataBound">
<SelectedItemStyle BackColor="CadetBlue" />
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="btnHiddenPostButton" CommandName="Select" runat="server" Text="HiddenPostButton" style="display:none" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ProductName" HeaderText="ProductName" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="UnitPrice" />
</Columns>
</asp:DataGrid></div>
</li>
</div>
</form>
</body>
</html>

  

GridView/DataGrid行单击和双击事件实现代码_.Net教程的更多相关文章

  1. 支持行单击、双击事件的GridView和DataList控件(译)

    支持行单击.双击事件的GridView和DataList控件(译)         让GridView 和 DataList 控件响应鼠标单击.双击事件.并且,使用 ClientScript.Regi ...

  2. GridView 行单击或双击事件绑定

    protected void gvTeacherTaskList_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.Comma ...

  3. [转] Ext Grid (ExtJs)上的单击以及双击事件

    例1: 1.双击 var cb = new Ext.grid.RowSelectionModel({ singleSelect:true //如果值是false,表明可以选择多行:否则只能选择一行 } ...

  4. 禁用CMFCRibbonApplicationButton的单击和双击事件

    为了禁用CMFCRibbonApplicationButton的单击和双击事件,我重载了CMFCRibbonApplicationButton如下: 1. MyRibbonApplicationBut ...

  5. jquery处理单击和双击事件

    今天做div点击时,需要用到同一div的单击和双击事件,出现问题如下 例子: Html <body> <div id="div_1">单击双击我</d ...

  6. Android 自定义View实现单击和双击事件

    自定义View, 1. 自定义一个Runnable线程TouchEventCountThread ,  用来统计500ms内的点击次数 2. 在MyView中的 onTouchEvent 中调用 上面 ...

  7. [Javasript] 同时实现单击和双击事件

    在同一个元素上同时绑定单击和双击事件: JavaScript <script type="text/javascript"> var timer = 0; var de ...

  8. JS - 解决鼠标单击、双击事件冲突问题(原生js实现)

    由于鼠标双击时每一次触发双击事件都会引起两次单击事件和一次单击事件,原生的js不提供专门的双击事件. 因为业务原因,双击和单机都绑定了不同的业务,在双击的时候又触发了单机,影响了页面的正常显示 出现问 ...

  9. unity3D 游戏物体同时绑定单击、双击事件

    前言 在unity中我们常用的获取鼠标点击的方法有 在3D场景中,一般用在Update方法中,每一帧调用 void Update(){ )){ Debug.log("鼠标左键点击" ...

随机推荐

  1. Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)

    概要 这篇博客是在上一篇博客Phantomjs+Nodejs+Mysql数据抓取(1.抓取数据) http://blog.csdn.net/jokerkon/article/details/50868 ...

  2. JavaScript基础知识总结(二)

    JavaScript语法 二.数据类型 程序把这些量.值分为几大类,每一类分别叫什么名称,有什么特点,就叫数据类型. 1.字符串(string) 字符串由零个或多个字符构成,字符包括字母,数字,标点符 ...

  3. winform 窗体圆角设计

    网上看到的很多winform窗体圆角设计代码都比较累赘,这里分享一个少量代码就可以实现的圆角.主要运用了System.Drawing.Drawing2D. 效果图 代码如下. private void ...

  4. Java 教程整理:基础、项目全都有

    Java 在编程语言排行榜中一直位列前排,可知 Java 语言的受欢迎程度了. 网上有很多 Java 教程,无论是基础入门还是开发小项目的教程都比比皆是,可是系统的很少,对于Java 学习者来说找到系 ...

  5. maven 快照

    大型应用软件一般由多个模块组成,一般它是多个团队开发同一个应用程序的不同模块,这是比较常见的场景.例如,一个团队正在对应用程序的应用程序,用户界面项目(app-ui.jar:1.0) 的前端进行开发, ...

  6. How to accept Track changes in Microsoft Word 2010?

    "Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...

  7. VisualStudio 2015 开启IIS Express可以调试X64项目

    现在项目开发时总有时需要在X64下开发,这样我们就需要IIS Express中调试.不要总是放在IIS中,在Attach这样好慢.   如果不设置直接调试X64的程序,我们有可能会受到以下类似的错误 ...

  8. Concurrency

    <Concurrency>:http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html <Java ...

  9. .NET面试题系列[3] - C# 基础知识(1)

    1 类型基础 面试出现频率:基本上肯定出现 重要程度:10/10,身家性命般重要.通常这也是各种招聘工作的第一个要求,即“熟悉C#”的一部分.连这部分都不清楚的人,可以说根本不知道自己每天都在干什么. ...

  10. Atitit各种SDM 软件开发过程SDP sdm的ddd tdd bdd设计

    Atitit各种SDM 软件开发过程SDP sdm的ddd tdd bdd设计 1.1. software development methodology (also known as SDM 1 1 ...