All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.net生成htm静态文件的两种途径_.net资料_编程技术
6种常用ASP.NET验证控件_[Asp.Net教程]
百分百的弹出窗口_JavaScript技术_编程技术
VBScript教程第三课,VBScript数据类型_JavaScript技术_编程技术
PHP网页编码问题:任意字符集正常显示网页的方法_php资料_编程技术
Asp.net,水晶报表之打印和导出格式_[Asp.Net教程]
技巧:ASP.NET技术获取IP与MAC地址的方法_.net资料_编程技术
GridView中添加CheckBox并返回选中行_[Asp.Net教程]
使用Delphi组件编程
PHP从映象(Reflection)类中读取属性信息_php资料_编程技术
Reading number is top 10 pictures
A man's favorite things16
Look for from human art net, is good1
Original author said, this is the Hengyang people
性感丰满身材火爆de美女1
战场废物1
Sora aoi mirror memorial classics1
狗狗与主人神同步2
支持判处贩卖儿童者死刑
Desktop Wallpapers1
China telecom 114 spokesman MeiYanXu1
Download software ranking
Boxer's Top ten classic battle6
星际争霸1.08硬盘免安装版
Boxer's Top ten classic battle3
Kung.Fu.Panda.2
Love the forty days
asp.net技术内幕
Professional killers2 data package
网页特效实例大全
Unix video tutorial3
Tram sex maniac 2 (H) rar bag2
delv published in(发表于) 2014/1/16 9:33:26 Edit(编辑)
扩展GridView控件(六)——数据行响应鼠标的单击和双击事件_[Asp.Net教程]

扩展GridView控件(六)——数据行响应鼠标的单击和双击事件_[Asp.Net教程]

扩展GridView控件(六)——数据行响应鼠标的单击和双击事件_[Asp.Net教程]























GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
http://files.cnblogs.com/webabcd/yycontrols.rar




介绍
为了让GridView的数据行可以响应鼠标的单击和双击事件,一般我们会在GridView的RowDataBound事件中给

加上客户端代码,为了简化这个步骤,我们来扩展一下它。




控件开发
1、新建一个继承自GridView的类。
复制C#代码保存代码///


/// 继承自GridView
///

[ToolboxData(@"<{0}:SmartGridView runat='server'>")]
public class SmartGridView : GridView
{
}
2、加两个属性,分别是单击行事件所对应的按钮的ID和双击行事件所对应的按钮的ID
复制C#代码保存代码private string _rowClickButtonID;
///
/// 单击行事件所对应的按钮的ID
///

[Description("单击行事件所对应的按钮的ID"), DefaultValue(""), Category("扩展")]
public virtual string RowClickButtonID
{
get { return _rowClickButtonID; }
set { _rowClickButtonID = value; }
}




private string _rowDoubleClickButtonID;
///


/// 双击行事件所对应的按钮的ID
///

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




namespace YYControls.SmartGridView
{
///


/// javascript
///

public class JavaScriptConstant
{
internal const string jsClickAndDoubleClick = @"";
}
}
4、重写OnPreRender方法,注册上面那段客户端脚本
复制C#代码保存代码///
/// OnPreRender
///

///
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以实现数据行响应鼠标的单击和双击事件的功能。主要是给

加上客户端代码,用来调用某个按钮的click事件
复制C#代码保存代码///


/// OnRowDataBound
///

///
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
复制C#代码保存代码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;




///


/// OjbData 的摘要说明
///

public class OjbData
{
public OjbData()
{
//
// TOD 在此处添加构造函数逻辑
//
}




[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
复制ASPX代码保存代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>







SmartGridView测试



DataSourceID="ObjectDataSource1" RowClickButtonID="btnTestRowClick" RowDoubleClickButtonID="btnTestRowDoubleClick">


















TypeName="OjbData">





转自【webabcd-.NET】













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.