博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
上接扩展GridView控件(6) - 响应行的单击事件和双击事件
阅读量:6411 次
发布时间:2019-06-23

本文共 4432 字,大约阅读时间需要 14 分钟。

4、重写OnPreRender方法,注册上面那段客户端脚本
InBlock.gif 
/// <summary> 

InBlock.gif                
/// OnPreRender 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="e"></param> 

InBlock.gif                
protected 
override 
void OnPreRender(EventArgs e) 

InBlock.gif                { 

InBlock.gif                        
base.OnPreRender(e); 

InBlock.gif 

InBlock.gif                        
if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 

InBlock.gif                        { 

InBlock.gif                                
if (!Page.ClientScript.IsClientScriptBlockRegistered(
"jsClickAndDoubleClick")) 

InBlock.gif                                { 

InBlock.gif                                        Page.ClientScript.RegisterClientScriptBlock( 

InBlock.gif                                                
this.GetType(), 

InBlock.gif                                                
"jsClickAndDoubleClick", JavaScriptConstant.jsClickAndDoubleClick 

InBlock.gif                                                ); 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif                }
 
5、重写OnRowDataBound以实现数据行响应鼠标的单击和双击事件的功能。主要是给<tr>加上客户端代码,用来调用某个按钮的click事件
/// <summary> 

InBlock.gif                
/// OnRowDataBound 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="e"></param> 

InBlock.gif                
protected 
override 
void OnRowDataBound(GridViewRowEventArgs e) 

InBlock.gif                { 

InBlock.gif                        
if (e.Row.RowType == DataControlRowType.DataRow) 

InBlock.gif                        { 

InBlock.gif                                
if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 

InBlock.gif                                { 

InBlock.gif                                        
// GridViewRow的每个TableCell 

InBlock.gif                                        
foreach (TableCell tc 
in e.Row.Cells) 

InBlock.gif                                        { 

InBlock.gif                                                
// TableCell里的每个Control 

InBlock.gif                                                
foreach (Control c 
in tc.Controls) 

InBlock.gif                                                { 

InBlock.gif                                                        
// 如果控件继承自接口IButtonControl 

InBlock.gif                                                        
if (c.GetType().GetInterface(
"IButtonControl") != 
null && c.GetType().GetInterface(
"IButtonControl").Equals(
typeof(IButtonControl))) 

InBlock.gif                                                        { 

InBlock.gif                                                                
if (!String.IsNullOrEmpty(RowClickButtonID)) 

InBlock.gif                                                                { 

InBlock.gif                                                                        
// 该按钮的ID等于单击行所对应的按钮ID 

InBlock.gif                                                                        
if (c.ID == RowClickButtonID) 

InBlock.gif                                                                        { 

InBlock.gif                                                                                
// 增加行的单击事件,调用客户端脚本,根据所对应按钮的ID执行所对应按钮的click事件 

InBlock.gif                                                                                e.Row.Attributes.Add(
"onclick"
"javascript:yy_RowClick('" + c.ClientID + 
"')"); 

InBlock.gif                                                                        } 

InBlock.gif                                                                } 

InBlock.gif 

InBlock.gif                                                                
if (!String.IsNullOrEmpty(RowDoubleClickButtonID)) 

InBlock.gif                                                                { 

InBlock.gif                                                                        
// 该按钮的ID等于双击行所对应的按钮ID 

InBlock.gif                                                                        
if (c.ID == RowDoubleClickButtonID) 

InBlock.gif                                                                        { 

InBlock.gif                                                                                
// 增加行的双击事件,调用客户端脚本,根据所对应按钮的ID执行所对应按钮的click事件 

InBlock.gif                                                                                e.Row.Attributes.Add(
"ondblclick"
"javascript:yy_RowDoubleClick('" + c.ClientID + 
"')"); 

InBlock.gif                                                                        } 

InBlock.gif                                                                } 

InBlock.gif                                                        } 

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
base.OnRowDataBound(e); 

InBlock.gif                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,要实现行的单击事件则设置RowClickButtonID为行单击事件所对应的按钮的ID,要实现行的双击事件则设置RowDoubleClickButtonID为行双击事件所对应的按钮的ID。
ObjData.cs
InBlock.gif
using System; 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Configuration; 

InBlock.gif
using System.Web; 

InBlock.gif
using System.Web.Security; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI.WebControls.WebParts; 

InBlock.gif
using System.Web.UI.HtmlControls; 

InBlock.gif 

InBlock.gif
using System.ComponentModel; 

InBlock.gif 

/// <summary> 

/// OjbData 的摘要说明 

/// </summary> 

InBlock.gif
public 
class OjbData 

InBlock.gif

InBlock.gif        
public OjbData() 

InBlock.gif        { 

InBlock.gif                
// 

InBlock.gif                
// TODO: 在此处添加构造函数逻辑 

InBlock.gif                
// 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        [DataObjectMethod(DataObjectMethodType.Select, 
true)] 

InBlock.gif        
public DataTable Select() 

InBlock.gif        { 

InBlock.gif                DataTable dt = 
new DataTable(); 

InBlock.gif                dt.Columns.Add(
"no"
typeof(
string)); 

InBlock.gif                dt.Columns.Add(
"name"
typeof(
string)); 

InBlock.gif 

InBlock.gif                
for (
int i = 0; i < 30; i++) 

InBlock.gif                { 

InBlock.gif                        DataRow dr = dt.NewRow(); 

InBlock.gif                        dr[0] = 
"no" + i.ToString().PadLeft(2, '0'); 

InBlock.gif                        dr[1] = 
"name" + i.ToString().PadLeft(2, '0'); 

InBlock.gif 

InBlock.gif                        dt.Rows.Add(dr); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
return dt; 

InBlock.gif        } 

InBlock.gif}
 
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>
 
InBlock.gif
/*测试版的实现 结束*/
 

OK
 
     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345522,如需转载请自行联系原作者
你可能感兴趣的文章
咏南中间件支持手机客户端
查看>>
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>
Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
查看>>
topcoder srm 680 div1 -3
查看>>
具体数学第二版第四章习题(1)
查看>>
高效前端优化工具--Fiddler入门教程
查看>>
【翻译】我钟爱的HTML5和CSS3在线工具
查看>>
Java多线程学习(吐血超详细总结)
查看>>
css3 变形
查看>>
Win7 64bit 安装Mysql5 出错 无法启动服务。
查看>>
嵌入式 H264参数语法文档: SPS、PPS、IDR以及NALU编码规律
查看>>
初识Opserver,StackExchange的监控解决方案
查看>>
给大家讲解一下JavaScript与后台Java天衣无缝相结合
查看>>
探索HTML5之本地文件系统API - File System API
查看>>
javascript有用代码块(1)
查看>>
libevent 笔记
查看>>
PHP实现人人OAuth登录和API调用
查看>>
redis源码笔记 - initServer
查看>>