GridView/DataGrid行单击和双击事件实现代码_.Net教程
功能: 单击选中行,双击打开详细页面
说明:单击事件(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教程的更多相关文章
- 支持行单击、双击事件的GridView和DataList控件(译)
支持行单击.双击事件的GridView和DataList控件(译) 让GridView 和 DataList 控件响应鼠标单击.双击事件.并且,使用 ClientScript.Regi ...
- GridView 行单击或双击事件绑定
protected void gvTeacherTaskList_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.Comma ...
- [转] Ext Grid (ExtJs)上的单击以及双击事件
例1: 1.双击 var cb = new Ext.grid.RowSelectionModel({ singleSelect:true //如果值是false,表明可以选择多行:否则只能选择一行 } ...
- 禁用CMFCRibbonApplicationButton的单击和双击事件
为了禁用CMFCRibbonApplicationButton的单击和双击事件,我重载了CMFCRibbonApplicationButton如下: 1. MyRibbonApplicationBut ...
- jquery处理单击和双击事件
今天做div点击时,需要用到同一div的单击和双击事件,出现问题如下 例子: Html <body> <div id="div_1">单击双击我</d ...
- Android 自定义View实现单击和双击事件
自定义View, 1. 自定义一个Runnable线程TouchEventCountThread , 用来统计500ms内的点击次数 2. 在MyView中的 onTouchEvent 中调用 上面 ...
- [Javasript] 同时实现单击和双击事件
在同一个元素上同时绑定单击和双击事件: JavaScript <script type="text/javascript"> var timer = 0; var de ...
- JS - 解决鼠标单击、双击事件冲突问题(原生js实现)
由于鼠标双击时每一次触发双击事件都会引起两次单击事件和一次单击事件,原生的js不提供专门的双击事件. 因为业务原因,双击和单机都绑定了不同的业务,在双击的时候又触发了单机,影响了页面的正常显示 出现问 ...
- unity3D 游戏物体同时绑定单击、双击事件
前言 在unity中我们常用的获取鼠标点击的方法有 在3D场景中,一般用在Update方法中,每一帧调用 void Update(){ )){ Debug.log("鼠标左键点击" ...
随机推荐
- 流程开发Activiti 与SpringMVC整合实例
流程(Activiti) 流程是完成一系列有序动作的概述.每一个节点动作的结果将对后面的具体操作步骤产生影响.信息化系统中流程的功能完全等同于纸上办公的层级审批,尤其在oa系统中各类电子流提现较为明显 ...
- PHP代码优化
1 代码优化 1 尽量静态化 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和 ...
- 开发者的利器:Docker 理解与使用
困扰写代码的机器难免会被我们安装上各种各样的开发工具.语言运行环境和引用库等一大堆的东西,长久以来不仅机器乱七八糟,而且有些相同的软件还有可能会安装不同的版本,这样又会导致一个项目正常运行了,却不小心 ...
- python 数据类型 -- 元组
元组其实是一种只读列表, 不能增,改, 只可以查询 对于不可变的信息将使用元组:例如数据连接配置 元组的两个方法: index, count >>> r = (1,1,2,3) &g ...
- 使用Git Bash远程添加分支和简单部署你的静态页面
新建一个分支:git branch mybranch(mybranch你的分支名字) 切换到你的新分支: git checkout mybranch 将新分支发布在github上: git push ...
- ELK分析IIS日志
LogStash.conf input { file { type => "iis_log" path => ["C:/inetpub/logs/LogF ...
- Spring mvc @initBinder 类型转化器的使用
一.单日期格式 因为是用注解完完成的后台访问,所以必须在大配置中配置包扫描器: 1.applicactionContext.xml <?xml version="1.0" e ...
- win7,Mindmanager2012使用模板时弹出Runtime error R6025解决方法
Mindjet.MindManager2012.v10.0在应用个别模板时提示"参数错误",然后自动关闭. 解决办法: 如果是win7系统,可以进入C:\Users\(用户名)\A ...
- ASP.NET Aries JSAPI 文档说明:AR.DataGrid
AR.DataGrid 文档 用法: <body> <table id="dg"></table> </body> </htm ...
- 【腾讯Bugly干货分享】微信iOS SQLite源码优化实践
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b58022433221be01499480 作者:张三华 前言 随着微信iO ...