GridView控件——单击命令按钮弹出确认框

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


控件开发
1、新建一个继承自GridView的类。
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 继承自GridView
ExpandedBlockEnd.gif
/// </summary>

None.gif [ToolboxData( @" <{0}:SmartGridView runat='server'></{0}:SmartGridView> " )]
None.gif
public   class  SmartGridView : GridView
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

2、新建一个ConfirmButton类,有两个属性
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// ConfirmButton 的摘要说明。
ExpandedBlockEnd.gif    
/// </summary>

None.gif     [ToolboxItem( false )]
None.gif    [TypeConverter(
typeof (ConfirmButtonConverter))]
None.gif    
public   class  ConfirmButton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string _commandName;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按钮的CommandName
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string CommandName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._commandName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._commandName = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _confirmMessage;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 确认框弹出的信息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string ConfirmMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this._confirmMessage; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis._confirmMessage = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

3、新建一个继承自CollectionBase的类ConfirmButtons
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// ProjectGroups 的摘要说明。
InBlock.gif    
/// 注意要继承自CollectionBase
ExpandedBlockEnd.gif    
/// </summary>

None.gif     [
None.gif    ToolboxItem(
false ),
None.gif    ParseChildren(
true )
None.gif    ]
None.gif    
public   class  ConfirmButtons : CollectionBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ConfirmButtons()
InBlock.gif            : 
base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 实现IList接口
InBlock.gif        
/// 获取或设置指定索引处的元素。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="index">要获得或设置的元素从零开始的索引</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public ConfirmButton this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (ConfirmButton)base.List[index];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.List[index] = (ConfirmButton)value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 实现IList接口
InBlock.gif        
/// 将某项添加到 System.Collections.IList 中。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="item">要添加到 System.Collections.IList 的 System.Object。</param>

InBlock.gif        public void Add(ConfirmButton item)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.List.Add(item);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 实现IList接口
InBlock.gif        
/// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="index">要从 System.Collections.IList 移除的 System.Object</param>

InBlock.gif        public void Remove(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (index > -1 && index < base.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.List.RemoveAt(index);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

4、新建一个继承自ExpandableObjectConverter的类ConfirmButtonConverter
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 类型转换器
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  ConfirmButtonConverter : ExpandableObjectConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回值能否将ConfirmButton类型转换为String类型
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="context"></param>
InBlock.gif        
/// <param name="destinationType"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (destinationType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.CanConvertTo(context, destinationType);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将ConfirmButton类型转换为String类型
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="context"></param>
InBlock.gif        
/// <param name="culture"></param>
InBlock.gif        
/// <param name="value"></param>
InBlock.gif        
/// <param name="destinationType"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

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

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (destinationType.Equals(typeof(string)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return String.Empty;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return "ConfirmButton";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.ConvertTo(context, culture, value, destinationType);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

5、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第3步创建的那个ConfirmButtons
None.gif          private  ConfirmButtons _confirmButtons;
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 确认按钮集合
ExpandedBlockEnd.gif        
/// </summary>

None.gif         [
None.gif        PersistenceMode(PersistenceMode.InnerProperty),
None.gif        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
None.gif        Description(
" 确认按钮集合,确认按钮的CommandName和提示信息 " ),
None.gif        Category(
" 扩展 " )
None.gif        ]
None.gif        
public   virtual  ConfirmButtons ConfirmButtons
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_confirmButtons == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _confirmButtons 
= new ConfirmButtons();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _confirmButtons;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

6、重写OnRowDataBound实现单击命令按钮弹出确认框的功能。主要是给按钮增加一个客户端的onclick事件。
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// OnRowDataBound
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="e"></param>

None.gif          protected   override   void  OnRowDataBound(GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (e.Row.RowType == DataControlRowType.DataRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this._confirmButtons != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// GridViewRow的每个TableCell
InBlock.gif
                    foreach (TableCell tc in e.Row.Cells)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
// TableCell里的每个Control
InBlock.gif
                        foreach (Control c in tc.Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
// 如果控件继承自接口IButtonControl
InBlock.gif
                            if (c.GetType().GetInterface("IButtonControl"!= null && c.GetType().GetInterface("IButtonControl").Equals(typeof(IButtonControl)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
// 从用户定义的ConfirmButtons集合中分解出ConfirmButton
InBlock.gif
                                foreach (ConfirmButton cb in _confirmButtons)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    
// 如果发现的按钮的CommandName在ConfirmButtons有定义的话
InBlock.gif
                                    if (((IButtonControl)c).CommandName == cb.CommandName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
dot.gif{
InBlock.gif                                        
// 增加确认框属性
InBlock.gif
                                        ((IAttributeAccessor)c).SetAttribute("onclick""return confirm('" + cb.ConfirmMessage + "')");
InBlock.gif                                        
break;
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.OnRowDataBound(e);
ExpandedBlockEnd.gif        }

None.gif


控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其ConfirmButtons属性即可。CommandName是命令按钮的CommandName属性;ConfirmMessage是弹出的确认框所显示的文字。
ObjData.cs
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  System.ComponentModel;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// OjbData 的摘要说明
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  OjbData
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public OjbData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//
InBlock.gif        
// TODO: 在此处添加构造函数逻辑
InBlock.gif        
//
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    [DataObjectMethod(DataObjectMethodType.Select, 
true)]
InBlock.gif    
public DataTable Select()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return dt;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Default.aspx
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  %>
None.gif
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml" >
None.gif
< head  runat ="server" >
None.gif    
< title > 无标题页 </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif        
< div >
None.gif            
&nbsp;
None.gif            
< yyc:SmartGridView  ID ="SmartGridView1"  runat ="server"  AutoGenerateColumns ="false"
None.gif                DataSourceID
="ObjectDataSource1" >
None.gif                
< Columns >
None.gif                    
< asp:BoundField  DataField ="no"  HeaderText ="序号"  SortExpression ="no"   />
None.gif                    
< asp:BoundField  DataField ="name"  HeaderText ="名称"  SortExpression ="name"   />
None.gif                    
< asp:ButtonField  CommandName ="ConfirmTest"  Text ="确认按钮测试"   />
None.gif                
</ Columns >
None.gif                
< ConfirmButtons >
None.gif                    
< yyc:ConfirmButton  ConfirmMessage ="确认删除吗?"  CommandName ="ConfirmTest" ></ yyc:ConfirmButton >
None.gif                
</ ConfirmButtons >
None.gif            
</ yyc:SmartGridView >
None.gif            
< asp:ObjectDataSource  ID ="ObjectDataSource1"  runat ="server"  SelectMethod ="Select"
None.gif                TypeName
="OjbData" ></ asp:ObjectDataSource >
None.gif        
</ div >
None.gifNone.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif
诸葛依驰

转载于:https://www.cnblogs.com/dingdayi/archive/2007/05/18/750833.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值