支持行单击、双击事件的GridView和DataList控件(译)
支持行单击、双击事件的GridView和DataList控件(译)
介绍
背景
GridView中的单击和双击
创建一个包含默认页的web应用程序。然后,在页面的Page指令中添加EnableEventValidation ="false"。稍后我们会将其删除。添加 GridView 并绑定一些数据。添加两个使用 GridView 编辑列选项的asp: ButtonField 控件。给这两个ButtonField的CommandName分别命名为SingleClick、DoubleClick 。(选择按钮也可以用于 SingleClick 事件但我已经决定两个都用 asp: ButtonField)。
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" />
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" />
</Columns>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender; // Get the selected index and the command name
int _selectedIndex = int.Parse(e.CommandArgument.ToString());
string _commandName = e.CommandName; switch (_commandName)
{
case ("SingleClick"):
_gridView.SelectedIndex = _selectedIndex;
this.Message.Text += "Single clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
case ("DoubleClick"):
this.Message.Text += "Double clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
}
}
创建一个RowDataBound 事件去修改绑定的每一行。现在,我们需要得到 SingleClick 按钮用于回发的客户端脚本并将其分配到整行。if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsSingle =
ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this JavaScript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
如果我们同时实现了上面两个事件,在功能上只会响应单击事件。这是因为当用户双击时,他的第一次点击被识别为单击然后页面在双击事件响应前就回传了。JavaScript的setTimeOut方法可以用来给单击事件设置一个延迟,从而给用户一个机会去完成双击操作。通过查看页面源码,我们可以发现OnClick事件在tr标签内:
onclick="javascript:__doPostBack('GridView1$ctl02$ctl00','')"
添加一个setTimeout方法,并为其设置300毫秒延迟:
onclick="javascript:setTimeout("__doPostBack('GridView1$ctl02$ctl00','')",300)"
于是完整的RowDataBound代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsSingle =
ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// To prevent the first click from posting back immediately
// (therefore giving the user a chance to double click)
// pause the postbackfor 300 milliseconds by
// wrapping the postback command in a setTimeout
_jsSingle = _jsSingle.Insert(, "setTimeout(\"");
_jsSingle += "\", 300)";
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle; // Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this javascript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
这两个按钮也无需再显示,设置其Visible="false":
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="false" />
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
</Columns>
现在RowCommand代码就能正确捕获和处理行单击和双击了。
注册用于验证的回发、回调数据
一切工作正常,但请记住,我们一开始在页面指令中添加了 EnableEventValidation ="flase"。这并不是最安全的选项,所以我们应该将其删除。但这将导致行单击或双击时触发"回发、回调参数无效"错误。错误提示告诉我们可以通过ClientScriptManager.RegisterForEventValidation来注册用于验证的回发、回调数据。(至于EventValidation 的目的已在别的文章里写得很详尽了本文不予赘述)。
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
base.Render(writer);
}
至此,我们将不会再遇到"Invalid postback or callback argument" 错误。
其它控件
在源代码中,我还演示了如何在DataList和ListBox中实现此功能。代码基本相同所以我就不贴在这里了。
DataList中实现行单击、双击
ListBox中实现行单击、双击
在ListBox中,它本身是没有Row或Item事件的,但onclick 和ondblclick 属性可以添加到控件中去。在SelectedIndexChanged事件处理程序中,通过Request.Form["__EventArgument"] 获取命令名。
结论
通过查看由 ASP.NET 生成的 javascript 代码,我们可以扩展 GridView 和 DataList 控件的功能。为了保护你的应用程序安全,您应该尽量避免使用 EnableEventValidation ="false"。一旦你理解了 EventValidation 的工作原理,你可以通过 ClientScript.RegisterForEventValidation 来确保您的代码按预期方式工作。
源码下载地址
https://yunpan.cn/cYTKyZFjQuS2c 访问密码 036e (不嫌麻烦的话,最好前往原文地址下载)
Clickable and Double Clickable Rows with GridView and DataList Controls (ASP.NET 2.0)
Introduction
There are several online sources describing how to make a GridView row clickable anywhere on the row. There are also a few which describe how to make a row double-clickable. I haven't come across any which handle both events or deal with event validation errors in ASP.NET 2.0.
Here I will describe how to handle single and double clicks with the GridView, DataList and ListBox controls. I will also show how to deal with event validation without disabling it.
Background
While developing an ASP.NET application to replace an old Windows application, I was asked to keep the user experience relatively close to the original. The original application let a user select a row and perform an action on that row from a toolbar. By double clicking on a row the user could open another form with a details view of that row. This type of functionality could easily be achieved with two separate buttons on each row of a GridView but I needed something better than that.
Single and Double Clicking with a GridView
Create an new web application with a default page. For now, add EnableEventValidation="false" to the page directive. We will remove it later. Add a GridView and bind some data to it. Add two asp:ButtonField controls using the edit columns option on the GridView. Give these button fields command names of SingleClick and DoubleClick. (A select button could also be used for the SingleClick event but I've decided to use asp:ButtonField for both.)
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" />
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" />
</Columns>
Create a RowCommand event for the GridView with a switch block to capture the separate events.
For the demo, this code will write out a history of the events fired. The SingleClick command will also set the SelectedIndex of the row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender; // Get the selected index and the command name
int _selectedIndex = int.Parse(e.CommandArgument.ToString());
string _commandName = e.CommandName; switch (_commandName)
{
case ("SingleClick"):
_gridView.SelectedIndex = _selectedIndex;
this.Message.Text += "Single clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
case ("DoubleClick"):
this.Message.Text += "Double clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
}
}
create a RowDataBound event to modify each row as it is bound. Now we need to take the client script which is used by the SingleClick button for postback and assign it to the entire row.
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsSingle =
ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
}
DoubleClick button, however both of these will not work together.if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this JavaScript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
If we implement both these events together we will only get the functionality of the single click.
This is because when the user starts a double click, the first click is taken as a single click and the page is posted back before the second click can happen.
The JavaScript setTimeout method can be used to set a timeout on the first click, therefore giving the user a chance to complete a double click.
Looking at the source of the page, we can see the onclick event on the <tr> tags:
onclick="javascript:__doPostBack('GridView1$ctl02$ctl00','')"
Add a setTimeout method and give it a timeout of 300 milliseconds:
onclick="javascript:setTimeout("__doPostBack('GridView1$ctl02$ctl00','')", 300)"
So the entire RowDataBound code looks like:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsSingle =
ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// To prevent the first click from posting back immediately
// (therefore giving the user a chance to double click)
// pause the postbackfor 300 milliseconds by
// wrapping the postback command in a setTimeout
_jsSingle = _jsSingle.Insert(, "setTimeout(\"");
_jsSingle += "\", 300)";
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[].Controls[];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this javascript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
The buttons no longer need to be visible so hide them by adding Visible="false":
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="false" />
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
</Columns>
We can now single click and double click on a row and the appropriate action will be captured in the RowCommand code.
Register the postback or callback data for validation
Everything is working fine, but remember that we added EnableEventValidation="false" to the page directive. This is not the most secure option so we should remove it. This will cause an "Invalid postback or callback argument" error when a row is clicked or double clicked. The error tells us to use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. (The purpose of EventValidation is well documented elsewhere and beyond the scope of this article.)
The ClientScriptManager.RegisterForEventValidation can be called by overriding the Render method. The trick here is to register the unique id of both the buttons for each row of the GridView. The UniqueID of the row is returned by GridViewRow.UniqueID. The UniqueID of the first button can be generated by appending "$ctl00" to the row's UniqueID and for the second button append "$ctl01".
The overridden Render method is as follows:
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
base.Render(writer);
}
Now we will not get any "Invalid postback or callback argument" errors.
Other Controls
In the source code, I also demonstrate how to achieve this functionality with a DataList and ListBox controls. The code is quite similar so I won't show it here.
Single and Double Clicking with a DataList
With the DataList, the contents of the ItemTemplate are wrapped in an asp:Panel, giving something to assign the onclick and ondblclick events to.
Single and Double Clicking with a ListBox
With the ListBox, there are no Row or Item events but the onclick and ondblclick attributes can be added to the control.
In the SelectedIndexChanged event handler, Request.Form["__EventArgument"] will return the command name.
Conclusion
By looking closely at the JavaScript which is written out by ASP.NET we can extend the functionality of the GridView and DataList controls. To keep your applications secure, you should not use EnableEventValidation="false" if at all possible. Once you understand how EventValidation works, you can use ClientScript.RegisterForEventValidation to ensure your code works as intended.
History
- v1.0 - 23rd Sep 2006
- v1.1 - 25th Sep 2006
- v1.2 - 12th Nov 2006
Addition of aListBoxcontrol to the demo - v1.3 - 20th Dec 2006
VB.NET version of demo added to download
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
支持行单击、双击事件的GridView和DataList控件(译)的更多相关文章
- .net dataGridView当鼠标经过时当前行背景色变色;然后【给GridView增加单击行事件,并获取单击行的数据填充到页面中的控件中】
1.首先在前台dataGridview属性中增加onRowDataBound属性事件 2.然后在后台Observing_RowDataBound事件中增加代码 protected void Obser ...
- GridView控件 Reapter控件 DataList控件 的区别和用法
ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...
- java笔记--使用事件分配线程更新Swing控件
使用事件分配线程更新Swing控件: Swing并不是线程安全的,如果在多个线程中更新Swing控件,则很可能造成程序崩溃. 为了避免这种问题,可以使用时间分配线程来更新Swing控件. EventQ ...
- Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录
一.问题描述 记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!! 在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...
- MVC中使用Echart后台加载数据 实现饼图、折线图、全国地图数据,单击双击事件等
@{ Layout = null; } @if (false) { <script src="~/Js/jquery-easyui-1.5/jquery.min.js"> ...
- electron 系统托盘 单击 双击事件冲突解决方法
部分代码 // 任务栏点击事件 let timeCount = 0 tray.on('click', function (Event) { setTimeout(() => { if (time ...
- Android入门之GridView(表格控件)
GridView是一个表格控件,可以在每个单元格中显示自定义的View或者字符串.在这里我们要实现一个图标下方有文字的效果. 1.首先我们应自定义布局文件image_text.xml.代码如下: &l ...
- gridview里找到控件
; i < gvIncomeYG.Rows.Count; i++) { Label lblYG_DYYGSR_BHS = ((Label)gvIncomeYG.Rows[i].Cells[].F ...
- 安卓中的事件分发机制之View控件
前言:Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent e ...
随机推荐
- Markdown 安装图解(破解汉化教程)
http://blog.csdn.net/taokai_110/article/details/72934818 终于解决了问题
- python数据分析工具包(1)——Numpy(一)
在本科阶段,我们常用的科学计算工具是MATLAB.下面介绍python的一个非常好用而且功能强大的科学计算库--Numpy. a powerful N-dimensional array object ...
- 一个客户端一键安装环境和服务的shell脚本
#!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...
- PHP中的浮点精度和类型
PHP中的浮点数 精度 在PHP中,浮点数的字长和平台相关,通常最大值是 1.8e308 并具有 14 位十进制数字的精度(64 位 IEEE 格式). 浮点数的精度有限.尽管取决于系统,PHP 通常 ...
- wordpress安装五步法
原文链接: 下载并解压缩WordPress程序安装包 在你的网页服务器上为WordPress创建一个数据库, 并且创建一个MySQL 拥有所有权限可以进入和修改的用户 重命名 wp-config-sa ...
- myeclispe中向mysql中插入中文数据出现??问题解决办法
或许很多人会出现??这种令人头痛的mysql的中文乱码问题:解决如下: 1.先对于新建的数据库要设置默认的字符集为UTF-8 create database mydb default characte ...
- Asp.Net Core 2.1+的视图缓存(响应缓存)
响应缓存Razor 页与 ASP.NET 核心 2.0 中不支持. 此功能将支持ASP.NET 核心 2.1 版本. 在老的版本的MVC里面,有一种可以缓存视图的特性(OutputCache),可以保 ...
- hihoCoder1310 岛屿 (dfs)
思路:首先dfs求得所有联通块,再搜索的同时把每个联通块的坐标都保存下来,然后把每个联通块处理一下–首先得到某个联通块的最小横坐标和纵坐标,然后让每个坐标去减去这个横坐标和纵坐标.相当于使得所有联通块 ...
- scrapy回调函数传递参数
scrapy.Request 的callback传参的两种方式 1.使用 lambda方式传递参数 def parse(self, response): for sel in response.xpa ...
- Nginx + uwsgi + django + websocket(dwebsocket)环境部署
1.安装nginx(/export/servers/nginx/) 保证/export/servers/nginx/是nginx的安装目录 /export/servers/nginx/conf/dom ...