GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[×××]
作者: webabcd
[索引页]
[×××]
扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
作者: webabcd
/*正式版的实现 开始*/
介绍
扩展GridView控件:
根据按钮的CommandName设置其客户端属性
使用方法(设置ClientButtons集合属性):
BoundCommandName - 需要绑定的CommandName
AttributeKey - 属性的名称
AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text)
Position - 属性的值的位置
关键代码
扩展GridView控件:
根据按钮的CommandName设置其客户端属性
使用方法(设置ClientButtons集合属性):
BoundCommandName - 需要绑定的CommandName
AttributeKey - 属性的名称
AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text)
Position - 属性的值的位置
关键代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace YYControls.SmartGridViewFunction
{
/// <summary>
/// 扩展功能:根据按钮的CommandName设置其客户端属性
/// </summary>
public class ClientButtonFunction : ExtendFunction
{
/// <summary>
/// 构造函数
/// </summary>
public ClientButtonFunction()
: base()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sgv">SmartGridView对象</param>
public ClientButtonFunction(SmartGridView sgv)
: base(sgv)
{
}
/// <summary>
/// 扩展功能的实现
/// </summary>
protected override void Execute()
{
this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
}
/// <summary>
/// SmartGridView的RowDataBoundCell事件
/// </summary>
/// <param name="sender"></param>
/// <param name="gvtc"></param>
void _sgv_RowDataBoundCell( object sender, GridViewTableCell gvtc)
{
TableCell tc = gvtc.TableCell;
// TableCell里的每个Control
foreach (Control c in tc.Controls)
{
// 如果控件继承自接口IButtonControl
if (c is IButtonControl)
{
// 从用户定义的ClientButtons集合中分解出ClientButton
foreach (ClientButton cb in this._sgv.ClientButtons)
{
// 如果在ClientButtons中绑定了该按钮的CommandName
if (((IButtonControl)c).CommandName == cb.BoundCommandName)
{
// 替换占位符{0}-CommandArgument;{1}-Text
string attributeValue =
String.Format(
cb.AttributeValue,
((IButtonControl)c).CommandArgument,
((IButtonControl)c).Text);
// 设置按钮的客户端属性
YYControls.Helper.Common.SetAttribute(
(IAttributeAccessor)c,
cb.AttributeKey,
attributeValue,
cb.Position);
break;
}
}
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace YYControls.SmartGridViewFunction
{
/// <summary>
/// 扩展功能:根据按钮的CommandName设置其客户端属性
/// </summary>
public class ClientButtonFunction : ExtendFunction
{
/// <summary>
/// 构造函数
/// </summary>
public ClientButtonFunction()
: base()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sgv">SmartGridView对象</param>
public ClientButtonFunction(SmartGridView sgv)
: base(sgv)
{
}
/// <summary>
/// 扩展功能的实现
/// </summary>
protected override void Execute()
{
this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
}
/// <summary>
/// SmartGridView的RowDataBoundCell事件
/// </summary>
/// <param name="sender"></param>
/// <param name="gvtc"></param>
void _sgv_RowDataBoundCell( object sender, GridViewTableCell gvtc)
{
TableCell tc = gvtc.TableCell;
// TableCell里的每个Control
foreach (Control c in tc.Controls)
{
// 如果控件继承自接口IButtonControl
if (c is IButtonControl)
{
// 从用户定义的ClientButtons集合中分解出ClientButton
foreach (ClientButton cb in this._sgv.ClientButtons)
{
// 如果在ClientButtons中绑定了该按钮的CommandName
if (((IButtonControl)c).CommandName == cb.BoundCommandName)
{
// 替换占位符{0}-CommandArgument;{1}-Text
string attributeValue =
String.Format(
cb.AttributeValue,
((IButtonControl)c).CommandArgument,
((IButtonControl)c).Text);
// 设置按钮的客户端属性
YYControls.Helper.Common.SetAttribute(
(IAttributeAccessor)c,
cb.AttributeKey,
attributeValue,
cb.Position);
break;
}
}
}
}
}
}
}
/*正式版的实现 结束*/
/*测试版的实现 开始*/
介绍
给按钮增加单击弹出确认框的功能是经常要用到的,我们一般是通过在RowDataBound事件里编码的方式实现,麻烦,所以扩展一下。
控件开发
1、新建一个继承自GridView的类。
给按钮增加单击弹出确认框的功能是经常要用到的,我们一般是通过在RowDataBound事件里编码的方式实现,麻烦,所以扩展一下。
控件开发
1、新建一个继承自GridView的类。
/// <summary>
/// 继承自GridView
/// </summary>
[ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]
public class SmartGridView : GridView
{
}
/// 继承自GridView
/// </summary>
[ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]
public class SmartGridView : GridView
{
}
2、新建一个ConfirmButton类,有两个属性
/// <summary>
/// ConfirmButton 的摘要说明。
/// </summary>
[ToolboxItem( false)]
[TypeConverter( typeof(ConfirmButtonConverter))]
public class ConfirmButton
{
private string _commandName;
/// <summary>
/// 按钮的CommandName
/// </summary>
public string CommandName
{
get { return this._commandName; }
set { this._commandName = value; }
}
private string _confirmMessage;
/// <summary>
/// 确认框弹出的信息
/// </summary>
public string ConfirmMessage
{
get { return this._confirmMessage; }
set { this._confirmMessage = value; }
}
}
/// ConfirmButton 的摘要说明。
/// </summary>
[ToolboxItem( false)]
[TypeConverter( typeof(ConfirmButtonConverter))]
public class ConfirmButton
{
private string _commandName;
/// <summary>
/// 按钮的CommandName
/// </summary>
public string CommandName
{
get { return this._commandName; }
set { this._commandName = value; }
}
private string _confirmMessage;
/// <summary>
/// 确认框弹出的信息
/// </summary>
public string ConfirmMessage
{
get { return this._confirmMessage; }
set { this._confirmMessage = value; }
}
}
3、新建一个继承自CollectionBase的类ConfirmButtons
/// <summary>
/// ProjectGroups 的摘要说明。
/// 注意要继承自CollectionBase
/// </summary>
[
ToolboxItem( false),
ParseChildren( true)
]
public class ConfirmButtons : CollectionBase
{
/// <summary>
/// 构造函数
/// </summary>
public ConfirmButtons()
: base()
{
}
/// <summary>
/// 实现IList接口
/// 获取或设置指定索引处的元素。
/// </summary>
/// <param name="index">要获得或设置的元素从零开始的索引</param>
/// <returns></returns>
public ConfirmButton this[ int index]
{
get
{
return (ConfirmButton) base.List[index];
}
set
{
base.List[index] = (ConfirmButton)value;
}
}
/// <summary>
/// 实现IList接口
/// 将某项添加到 System.Collections.IList 中。
/// </summary>
/// <param name="item">要添加到 System.Collections.IList 的 System.Object。</param>
public void Add(ConfirmButton item)
{
base.List.Add(item);
}
/// <summary>
/// 实现IList接口
/// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
/// </summary>
/// <param name="index">要从 System.Collections.IList 移除的 System.Object</param>
public void Remove( int index)
{
if (index > -1 && index < base.Count)
{
base.List.RemoveAt(index);
}
}
}
/// ProjectGroups 的摘要说明。
/// 注意要继承自CollectionBase
/// </summary>
[
ToolboxItem( false),
ParseChildren( true)
]
public class ConfirmButtons : CollectionBase
{
/// <summary>
/// 构造函数
/// </summary>
public ConfirmButtons()
: base()
{
}
/// <summary>
/// 实现IList接口
/// 获取或设置指定索引处的元素。
/// </summary>
/// <param name="index">要获得或设置的元素从零开始的索引</param>
/// <returns></returns>
public ConfirmButton this[ int index]
{
get
{
return (ConfirmButton) base.List[index];
}
set
{
base.List[index] = (ConfirmButton)value;
}
}
/// <summary>
/// 实现IList接口
/// 将某项添加到 System.Collections.IList 中。
/// </summary>
/// <param name="item">要添加到 System.Collections.IList 的 System.Object。</param>
public void Add(ConfirmButton item)
{
base.List.Add(item);
}
/// <summary>
/// 实现IList接口
/// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
/// </summary>
/// <param name="index">要从 System.Collections.IList 移除的 System.Object</param>
public void Remove( int index)
{
if (index > -1 && index < base.Count)
{
base.List.RemoveAt(index);
}
}
}
转载于:https://blog.51cto.com/webabcd/345450