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

GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]


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


作者:webabcd

/*正式版的实现 开始*/
 
介绍
扩展GridView控件:
响应行的单击事件和双击事件,并在服务端处理

使用方法(设置属性):
BoundRowClickCommandName - 行的单击事件需要绑定的CommandName
BoundRowDoubleClickCommandName - 行的双击事件需要绑定的CommandName


关键代码
单击
using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI.WebControls;
using System.Web.UI;

namespace YYControls.SmartGridViewFunction
{
         /// <summary>
         /// 扩展功能:响应行的单击事件
         /// </summary>
         public class RowClickFunction : ExtendFunction
        {
                List< string> _rowClickButtonUniqueIdList = new List< string>();

                 /// <summary>
                 /// 构造函数
                 /// </summary>
                 public RowClickFunction()
                        : base()
                {

                }

                 /// <summary>
                 /// 构造函数
                 /// </summary>
                 /// <param name="sgv">SmartGridView对象</param>
                 public RowClickFunction(SmartGridView sgv)
                        : base(sgv)
                {

                }

                 /// <summary>
                 /// 扩展功能的实现
                 /// </summary>
                 protected override void Execute()
                {
                         this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
                         this._sgv.RenderBegin += new SmartGridView.RenderBeginHandler(_sgv_RenderBegin);
                }

                 /// <summary>
                 /// RowDataBoundCell
                 /// </summary>
                 /// <param name="sender"></param>
                 /// <param name="gvtc"></param>
                 void _sgv_RowDataBoundCell( object sender, GridViewTableCell gvtc)
                {
                        TableCell tc = gvtc.TableCell;

                         foreach (Control c in tc.Controls)
                        {
                                IButtonControl ibc = c as IButtonControl;

                                 if (ibc != null && this._sgv.BoundRowClickCommandName == ibc.CommandName)
                                {
                                         // 300毫秒后响应单击事件的脚本(避免和双击事件冲突)
                                         string js = this._sgv.Page.ClientScript.GetPostBackClientHyperlink(c, "");
                                        js = js.Insert(11, "setTimeout(\"");
                                        js += "\", 300)";

                                        GridViewRow gvr = tc.Parent as GridViewRow;
                                        Helper.Common.SetAttribute(gvr, "onclick", js, AttributeValuePosition.Last);

                                        _rowClickButtonUniqueIdList.Add(c.UniqueID);
                                }
                        }
                }

                 /// <summary>
                 /// RenderBegin
                 /// </summary>
                 /// <param name="sender"></param>
                 /// <param name="writer"></param>
                 void _sgv_RenderBegin( object sender, HtmlTextWriter writer)
                {
                         foreach ( string uniqueId in this._rowClickButtonUniqueIdList)
                        {
                                 // 注册回发或回调数据以进行验证
                                 this._sgv.Page.ClientScript.RegisterForEventValidation(uniqueId);
                        }
                }
        }
}
 
双击
using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI.WebControls;
using System.Web.UI;

namespace YYControls.SmartGridViewFunction
{
         /// <summary>
         /// 扩展功能:响应行的双击事件
         /// </summary>
         public class RowDoubleClickFunction : ExtendFunction
        {
                List< string> _rowDoubleClickButtonUniqueIdList = new List< string>();

                 /// <summary>
                 /// 构造函数
                 /// </summary>
                 public RowDoubleClickFunction()
                        : base()
                {

                }

                 /// <summary>
                 /// 构造函数
                 /// </summary>
                 /// <param name="sgv">SmartGridView对象</param>
                 public RowDoubleClickFunction(SmartGridView sgv)
                        : base(sgv)
                {

                }

                 /// <summary>
                 /// 扩展功能的实现
                 /// </summary>
                 protected override void Execute()
                {
                         this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
                         this._sgv.RenderBegin += new SmartGridView.RenderBeginHandler(_sgv_RenderBegin);
                }

                 /// <summary>
                 /// RowDataBoundCell
                 /// </summary>
                 /// <param name="sender"></param>
                 /// <param name="gvtc"></param>
                 void _sgv_RowDataBoundCell( object sender, GridViewTableCell gvtc)
                {
                        TableCell tc = gvtc.TableCell;

                         foreach (Control c in tc.Controls)
                        {
                                IButtonControl ibc = c as IButtonControl;

                                 if (ibc != null && this._sgv.BoundRowDoubleClickCommandName == ibc.CommandName)
                                {
                                         // 响应双击事件的脚本
                                         string js = this._sgv.Page.ClientScript.GetPostBackClientHyperlink(c, "");

                                        GridViewRow gvr = tc.Parent as GridViewRow;
                                        Helper.Common.SetAttribute(gvr, "ondblclick", js, AttributeValuePosition.Last);

                                        _rowDoubleClickButtonUniqueIdList.Add(c.UniqueID);
                                }
                        }
                }

                 /// <summary>
                 /// RenderBegin
                 /// </summary>
                 /// <param name="sender"></param>
                 /// <param name="writer"></param>
                 void _sgv_RenderBegin( object sender, HtmlTextWriter writer)
                {
                         foreach ( string uniqueId in this._rowDoubleClickButtonUniqueIdList)
                        {
                                 // 注册回发或回调数据以进行验证
                                 this._sgv.Page.ClientScript.RegisterForEventValidation(uniqueId);
                        }
                }
        }
}
 
/*正式版的实现 结束*/
 
/*测试版的实现 开始*/
 
介绍
为了让GridView的数据行可以响应鼠标的单击和双击事件,一般我们会在GridView的RowDataBound事件中给<tr>加上客户端代码,为了简化这个步骤,我们来扩展一下它。


控件开发
1、新建一个继承自GridView的类。
/// <summary>
/// 继承自GridView
/// </summary>
[ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]
public class SmartGridView : GridView
{
}
 
2、加两个属性,分别是单击行事件所对应的按钮的ID和双击行事件所对应的按钮的ID
private string _rowClickButtonID;
                 /// <summary>
                 /// 单击行事件所对应的按钮的ID
                 /// </summary>
                [Description( "单击行事件所对应的按钮的ID"), DefaultValue( ""), Category("扩展")]
                 public virtual string RowClickButtonID
                {
                        get { return _rowClickButtonID; }
                        set { _rowClickButtonID = value; }
                }

                 private string _rowDoubleClickButtonID;
                 /// <summary>
                 /// 双击行事件所对应的按钮的ID
                 /// </summary>
                [Description( "双击行事件所对应的按钮的ID"), DefaultValue( ""), Category("扩展")]
                 public virtual string RowDoubleClickButtonID
                {
                        get { return _rowDoubleClickButtonID; }
                        set { _rowDoubleClickButtonID = value; }
                }
 
3、新建一个JavaScriptConstant类,把我们要用到的javascript存在一个常量里
using System;
using System.Collections.Generic;
using System.Text;

namespace YYControls.SmartGridView
{
         /// <summary>
         /// javascript
         /// </summary>
         public class JavaScriptConstant
        {
                 internal const string jsClickAndDoubleClick = @"<script type=""text/javascript"">
                 //<![CDATA[
                var isDoubleClick = false;
                function yy_RowClick(id)
                {
                        setTimeout( ""yy_RowClickTimeout('""+id+ ""')"", 300);
                }
                function yy_RowClickTimeout(id)
                {
                         if (isDoubleClick == false)
                        {
                                 // 执行ID所指按钮的click事件
                                document.getElementById(id).click();
                        }
                        isDoubleClick = true;
                }
                function yy_RowDoubleClick(id)
                {
                         if (isDoubleClick == true)
                        {
                                 // 执行ID所指按钮的click事件
                                document.getElementById(id).click();
                        }
                        isDoubleClick = true;
                }
                 //]]>
                </script>";
        }
}
 
未完待续>>
 
 

你可能感兴趣的:(职场,GridView,控件,休闲,单击)