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

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




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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值