上接扩展GridView控件(6) - 响应行的单击事件和双击事件

简介:
4、重写OnPreRender方法,注册上面那段客户端脚本
  /// <summary> 
                 /// OnPreRender 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnPreRender(EventArgs e) 
                { 
                         base.OnPreRender(e); 
 
                         if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 
                        { 
                                 if (!Page.ClientScript.IsClientScriptBlockRegistered( "jsClickAndDoubleClick")) 
                                { 
                                        Page.ClientScript.RegisterClientScriptBlock( 
                                                 this.GetType(), 
                                                 "jsClickAndDoubleClick", JavaScriptConstant.jsClickAndDoubleClick 
                                                ); 
                                } 
                        } 
                }
 
5、重写OnRowDataBound以实现数据行响应鼠标的单击和双击事件的功能。主要是给<tr>加上客户端代码,用来调用某个按钮的click事件

/// <summary> 
                 /// OnRowDataBound 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnRowDataBound(GridViewRowEventArgs e) 
                { 
                         if (e.Row.RowType == DataControlRowType.DataRow) 
                        { 
                                 if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 
                                { 
                                         // GridViewRow的每个TableCell 
                                         foreach (TableCell tc  in e.Row.Cells) 
                                        { 
                                                 // TableCell里的每个Control 
                                                 foreach (Control c  in tc.Controls) 
                                                { 
                                                         // 如果控件继承自接口IButtonControl 
                                                         if (c.GetType().GetInterface( "IButtonControl") !=  null && c.GetType().GetInterface( "IButtonControl").Equals( typeof(IButtonControl))) 
                                                        { 
                                                                 if (!String.IsNullOrEmpty(RowClickButtonID)) 
                                                                { 
                                                                         // 该按钮的ID等于单击行所对应的按钮ID 
                                                                         if (c.ID == RowClickButtonID) 
                                                                        { 
                                                                                 // 增加行的单击事件,调用客户端脚本,根据所对应按钮的ID执行所对应按钮的click事件 
                                                                                e.Row.Attributes.Add( "onclick""javascript:yy_RowClick('" + c.ClientID +  "')"); 
                                                                        } 
                                                                } 
 
                                                                 if (!String.IsNullOrEmpty(RowDoubleClickButtonID)) 
                                                                { 
                                                                         // 该按钮的ID等于双击行所对应的按钮ID 
                                                                         if (c.ID == RowDoubleClickButtonID) 
                                                                        { 
                                                                                 // 增加行的双击事件,调用客户端脚本,根据所对应按钮的ID执行所对应按钮的click事件 
                                                                                e.Row.Attributes.Add( "ondblclick""javascript:yy_RowDoubleClick('" + c.ClientID +  "')"); 
                                                                        } 
                                                                } 
                                                        } 
                                                } 
                                        } 
                                } 
                        } 
 
                         base.OnRowDataBound(e); 
                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,要实现行的单击事件则设置RowClickButtonID为行单击事件所对应的按钮的ID,要实现行的双击事件则设置RowDoubleClickButtonID为行双击事件所对应的按钮的ID。
ObjData.cs
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
 
using System.ComponentModel; 
 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
public  class OjbData 

         public OjbData() 
        { 
                 // 
                 // TODO: 在此处添加构造函数逻辑 
                 // 
        } 
 
        [DataObjectMethod(DataObjectMethodType.Select,  true)] 
         public DataTable Select() 
        { 
                DataTable dt =  new DataTable(); 
                dt.Columns.Add( "no"typeof( string)); 
                dt.Columns.Add( "name"typeof( string)); 
 
                 for ( int i = 0; i < 30; i++) 
                { 
                        DataRow dr = dt.NewRow(); 
                        dr[0] =  "no" + i.ToString().PadLeft(2, '0'); 
                        dr[1] =  "name" + i.ToString().PadLeft(2, '0'); 
 
                        dt.Rows.Add(dr); 
                } 
 
                 return dt; 
        } 
}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
        <title>SmartGridView测试</title> 
</head> 
<body> 
        <form id="form1" runat="server"> 
                <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False" 
                        DataSourceID="ObjectDataSource1" RowClickButtonID="btnTestRowClick" RowDoubleClickButtonID="btnTestRowDoubleClick"> 
                        <Columns> 
                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 
                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" /> 
                                <asp:TemplateField> 
                                        <footerstyle cssclass="hidden" /> 
                                        <headerstyle cssclass="hidden" /> 
                                        <itemstyle cssclass="hidden" /> 
                                        <itemtemplate> 
                                                <asp:Button id="btnTestRowClick" runat="server" CommandName="RowClick" CommandArgument='<%# Container.DataItemIndex %>' /> 
                                                <asp:Button id="btnTestRowDoubleClick" runat="server" CommandName="RowDoubleClick" CommandArgument='<%# Container.DataItemIndex %>' /> 
                                        </itemtemplate> 
                                </asp:TemplateField> 
                        </Columns> 
                </yyc:SmartGridView> 
                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 
                        TypeName="OjbData"></asp:ObjectDataSource> 
        </form> 
</body> 
</html>
 
/*测试版的实现 结束*/
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345522,如需转载请自行联系原作者


相关文章
|
C# Windows 容器
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
NotifyIcon控件表示系统右下角任务栏上的托盘图标,其ShowBalloonTip方法用于显示气球状提示框(Win10只有为本地Toast通知),ToolTip\oolTipText可以...
2045 0
C#或Winform中的消息通知之系统托盘的气泡提示窗口(系统toast通知)、ToolTip控件和ToolTipText属性
|
7月前
|
前端开发 JavaScript 定位技术
高德地图实现-自定义信息窗+窗体点击事件
高德地图实现-自定义信息窗+窗体点击事件
542 0
|
前端开发 C# Windows
WPF鼠标、键盘、拖拽事件、用行为封装事件
本文主要介绍了WPF中常用的鼠标事件、键盘事件以及注意事项,同时使用一个案例讲解了拓展事件。除此之外,本文还讲述如何用行为(Behavior)来封装事件。
362 0
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
如何解决WPF中 ScrollViewer 内包含 TreeView 或者 ListBox 等控件时滚轮事件被劫持的问题
|
XML Android开发 数据格式
【Android TV 开发】焦点处理 ( 父容器与子组件焦点获取关系处理 | 不同电视设备上的兼容问题 | 触摸获取焦点 | 按键获取焦点 )
【Android TV 开发】焦点处理 ( 父容器与子组件焦点获取关系处理 | 不同电视设备上的兼容问题 | 触摸获取焦点 | 按键获取焦点 )
539 0
|
C# 数据可视化 容器
WPF触屏Touch事件在嵌套控件中的响应问题
原文:WPF触屏Touch事件在嵌套控件中的响应问题 前几天遇到个touch事件的坑,记录下来以增强理解。 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出现滚动条压缩内容区域的,反而会将滚动区域转移到外面的list view(父listview),这个无可争议,但这个问题开始没留意,为待会的坑埋下伏笔。
1035 0
UWP 取消GridView、ListView鼠标选中、悬停效果
原文:UWP 取消GridView、ListView鼠标选中、悬停效果 因为经常碰到ListView或者ListBox之类的选中、鼠标悬停样式和自己设置的主题颜色不搭,这时就需要改变这些样式了. 而这里我通过ListView来说明,大致思路其实就是重新定义Item的Template。
1198 0
|
Web App开发 前端开发 JavaScript

热门文章

最新文章