上接扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性

简介:
4、新建一个继承自ExpandableObjectConverter的类ConfirmButtonConverter
/// <summary> 
         /// 类型转换器 
         /// </summary> 
         public  class ConfirmButtonConverter : ExpandableObjectConverter 
        { 
                 /// <summary> 
                 /// 返回值能否将ConfirmButton类型转换为String类型 
                 /// </summary> 
                 /// <param name="context"></param> 
                 /// <param name="destinationType"></param> 
                 /// <returns></returns> 
                 public  override  bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
                { 
                         if (destinationType ==  typeof( string)) 
                        { 
                                 return  true
                        } 
                         return  base.CanConvertTo(context, destinationType); 
                } 
 
                 /// <summary> 
                 /// 将ConfirmButton类型转换为String类型 
                 /// </summary> 
                 /// <param name="context"></param> 
                 /// <param name="culture"></param> 
                 /// <param name="value"></param> 
                 /// <param name="destinationType"></param> 
                 /// <returns></returns> 
                 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
private ConfirmButtons _confirmButtons; 
                 /// <summary> 
                 /// 确认按钮集合 
                 /// </summary> 
                [ 
                PersistenceMode(PersistenceMode.InnerProperty), 
                DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
                Description( "确认按钮集合,确认按钮的CommandName和提示信息"), 
                Category( "扩展"
                ] 
                 public  virtual ConfirmButtons ConfirmButtons 
                { 
                        get 
                        { 
                                 if (_confirmButtons ==  null
                                { 
                                        _confirmButtons =  new ConfirmButtons(); 
                                } 
                                 return _confirmButtons; 
                        } 
                }
 
6、重写OnRowDataBound实现单击命令按钮弹出确认框的功能。主要是给按钮增加一个客户端的onclick事件。
/// <summary> 
                 /// OnRowDataBound 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnRowDataBound(GridViewRowEventArgs e) 
                { 
                         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
                                                                        } 
                                                                } 
                                                        } 
                                                } 
                                        } 
                                } 
                        } 
 
                         base.OnRowDataBound(e); 
                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其ConfirmButtons属性即可。CommandName是命令按钮的CommandName属性;ConfirmMessage是弹出的确认框所显示的文字。
ObjData.cs
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; 
 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
public  class OjbData 

         public OjbData() 
        { 
                 // 
                 // TODO: 在此处添加构造函数逻辑 
                 // 
        } 
 
        [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
<%@ 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>无标题页</title> 
</head> 
<body> 
        <form id="form1" runat="server"> 
                <div> 
                          
                        <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="false" 
                                DataSourceID="ObjectDataSource1"> 
                                <Columns> 
                                        <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 
                                        <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 
                                        <asp:ButtonField CommandName="ConfirmTest" Text="确认按钮测试" /> 
                                </Columns> 
                                <ConfirmButtons> 
                                        <yyc:ConfirmButton ConfirmMessage="确认删除吗?" CommandName="ConfirmTest"></yyc:ConfirmButton> 
                                </ConfirmButtons> 
                        </yyc:SmartGridView> 
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 
                                TypeName="OjbData"></asp:ObjectDataSource> 
                </div> 
        </form> 
</body> 
</html>
 
/*测试版的实现 结束*/
 



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




相关文章
|
Android开发
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
|
C++
Qt动态添加控件并设置大小位置等属性
Qt动态添加控件并设置大小位置等属性
1063 0
|
Web App开发
艾伟:WinForm控件开发总结(七)-----为复杂属性的子属性提供编辑功能
前面的几篇文章中,我们给控件添加一个复杂的类型Scope,并且给它的类型提供的一个类型转换器,现在我们可以在属性浏览器中编辑它的值,并且它的值也被串行化的源代码里了。但是你有没有发现,在属性浏览器里编辑这个属性的值还是不太方便。
701 0
|
C# 数据安全/隐私保护
WPF 使用附加属性增加控件属性
原文:WPF 使用附加属性增加控件属性 使用附加属性增加控件属性,使得这个附加属性在使用的时候没有局限性,可以在任何的控件中使用它来增加所需要的属性,使得控件的属性使用起来非常灵活   一、自定义附加属性 1 2 3 4 5 6 7 8 9 10 11 ...
902 0
|
图形学
Unity3D 重写下拉菜单/Dropdown组件、开启每个按钮可用
Override Dropdown Component 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
2729 0