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

Reading number is top 10 articles
原创:在ASP.NET2.0中实现主页嵌套_.net资料_编程技术
php5手动最简安装方法_php资料_编程技术
Discuz论坛2.5,升级到PHP,MySQL新版遇到的问题_php资料_编程技术
delphi高级vcl组件表头组件(THeaderControl)使用实例
静态和动态编译Apache+php+mysql全过程_php资料_编程技术
SQL Server 数据库文件存放在何处_[SQL Server教程]
ASP.NET,2.0,绑定高级技巧_[Asp.Net教程]
也谈,.NET2.0中避免分布式事务_.net资料_编程技术
Asp.Net中对Cookie的基本操作_.net资料_编程技术
PHP Shell的编写(改进版)_[PHP教程]
Reading number is top 10 pictures
The household of rural style is designed
Compared GDP and per capita income in China for 40 years
The sixties of the last century, China is such a kill pig
初吻给了谁?
你是左脑型还是右脑型
Sell the barbecue as says father du breul5
Soldier saw beauty after the reaction
Magnificent cloud2
Sora aoi on twitter3
寒流来了
Download software ranking
SP3 for SQL2000
Unix video tutorial2
VC++6.0简体中文版
Take off clothes to survival
Macromedia Dreamweaver 8
The Bermuda triangle2
美女写真3
小黑猫大战两米大花蛇
C语言教程TXT
I'm come from Beijing1
delv published in(发表于) 2014/1/16 9:33:35 Edit(编辑)
扩展GridView(三)——单击命令按钮弹出确认框_[Asp.Net教程]

扩展GridView(三)——单击命令按钮弹出确认框_[Asp.Net教程]

扩展GridView(三)——单击命令按钮弹出确认框_[Asp.Net教程]

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



扩展GridView(三)——单击命令按钮弹出确认框


介绍
给按钮增加单击弹出确认框的功能是经常要用到的,我们一般是通过在RowDataBound事件里编码的方式实现,麻烦,所以扩展一下。



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


/// 继承自GridView
///

[ToolboxData(@"<{0}:SmartGridView runat='server'>")]
public class SmartGridView : GridView
{
}
2、新建一个ConfirmButton类,有两个属性
复制C#代码保存代码///
/// ConfirmButton 的摘要说明。
///

[ToolboxItem(false)]
[TypeConverter(typeof(ConfirmButtonConverter))]
public class ConfirmButton
{
private string _commandName;


///


/// 按钮的CommandName
///

public string CommandName
{
get { return this._commandName; }
set { this._commandName = value; }
}


private string _confirmMessage;


///


/// 确认框弹出的信息
///

public string ConfirmMessage
{
get { return this._confirmMessage; }
set { this._confirmMessage = value; }
}
}
3、新建一个继承自CollectionBase的类ConfirmButtons
复制C#代码保存代码///
/// ProjectGroups 的摘要说明。
/// 注意要继承自CollectionBase
///

[
ToolboxItem(false),
ParseChildren(true)
]
public class ConfirmButtons : CollectionBase
{
///
/// 构造函数
///

public ConfirmButtons()
: base()
{
}


///


/// 实现IList接口
/// 获取或设置指定索引处的元素。
///

/// 要获得或设置的元素从零开始的索引
///
public ConfirmButton this[int index]
{
get
{
return (ConfirmButton) base.List[index];
}
set
{
base.List[index] = (ConfirmButton) value;
}
}


///


/// 实现IList接口
/// 将某项添加到 System.Collections.IList 中。
///

/// 要添加到 System.Collections.IList 的 System.Object。
public void Add(ConfirmButton item)
{
base.List.Add(item);
}


///


/// 实现IList接口
/// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
///

/// 要从 System.Collections.IList 移除的 System.Object
public void Remove(int index)
{
if (index > -1 && index < base.Count)
{
base.List.RemoveAt(index);
}
}
}
4、新建一个继承自ExpandableObjectConverter的类ConfirmButtonConverter
复制C#代码保存代码///
/// 类型转换器
///

public class ConfirmButtonConverter : ExpandableObjectConverter
{
///
/// 返回值能否将ConfirmButton类型转换为String类型
///

///
///
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}


///


/// 将ConfirmButton类型转换为String类型
///

///
///
///
///
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, Type destinationType)
{
if (value != null)
{
if (!(value is YYControls.SmartGridView.ConfirmButton))
{
throw new ArgumentException(
"无效的ConfirmButton", "value");
}
}


if (destinationType.Equals(typeof(string)))
{
if (value == null)
{
return String.Empty;
}
return "ConfirmButton";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
5、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第3步创建的那个ConfirmButtons
复制C#代码保存代码private ConfirmButtons _confirmButtons;


///


/// 确认按钮集合
///

[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Description("确认按钮集合,确认按钮的CommandName和提示信息"),
Category("扩展")
]
public virtual ConfirmButtons ConfirmButtons
{
get
{
if (_confirmButtons == null)
{
_confirmButtons = new ConfirmButtons();
}
return _confirmButtons;
}
}
6、在继承自GridView的那个类的构造函数内加一个RowDataBound事件的处理,在该事件内实现单击命令按钮弹出确认框的功能。
复制C#代码保存代码///
/// 构造函数
///

public SmartGridView()
: base()
{
// 新增“SmartGridView_RowDataBound”事件处理
this.RowDataBound += new GridViewRowEventHandler(SmartGridView_RowDataBound);
}


///


/// RowDataBound事件
///

/// sender
/// e
protected void SmartGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
SmartGridView sgv = (SmartGridView) sender;


if (e.Row.RowType == DataControlRowType.DataRow)
{
if (this._confirmButtons != null)
{
// 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)))
{
// 从用户定义的ConfirmButtons集合中分解出ConfirmButton
foreach (ConfirmButton cb in _confirmButtons)
{
// 如果发现的按钮的CommandName在ConfirmButtons有定义的话
if (((IButtonControl) c).CommandName == cb.CommandName)
{
// 增加确认框属性
((IAttributeAccessor) c).SetAttribute("onclick", "return confirm('" + cb.ConfirmMessage + "')");
break;
}
}
}
}
}
}
}
}


控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其ConfirmButtons属性即可。CommandName是命令按钮的CommandName属性;ConfirmMessage是弹出的确认框所显示的文字。
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" %>





无标题页






DataSourceID="ObjectDataSource1">









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.