扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
生活随笔
收集整理的這篇文章主要介紹了
扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GridView既強大又好用。為了讓它更強大、更好用,我們來寫一個繼承自GridView的控件。
[索引頁]
[源碼下載]
擴展GridView控件(3) - 根據按鈕的CommandName設置其客戶端屬性
作者: webabcd
/*正式版的實現?開始*/
介紹
擴展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;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????}
????}
}
/*正式版的實現?結束*/
/*測試版的實現?開始*/
介紹
給按鈕增加單擊彈出確認框的功能是經常要用到的,我們一般是通過在RowDataBound事件里編碼的方式實現,麻煩,所以擴展一下。
控件開發
1、新建一個繼承自GridView的類。
///?<summary>
///?繼承自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;?}
????????}
????}
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);
????????????}
????????}
????}
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>
/*測試版的實現?結束*/
OK
[源碼下載]
[索引頁]
[源碼下載]
擴展GridView控件(3) - 根據按鈕的CommandName設置其客戶端屬性
作者: webabcd
/*正式版的實現?開始*/
介紹
擴展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;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????}
????}
}
/*正式版的實現?結束*/
/*測試版的實現?開始*/
介紹
給按鈕增加單擊彈出確認框的功能是經常要用到的,我們一般是通過在RowDataBound事件里編碼的方式實現,麻煩,所以擴展一下。
控件開發
1、新建一個繼承自GridView的類。
///?<summary>
///?繼承自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;?}
????????}
????}
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);
????????????}
????????}
????}
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>
/*測試版的實現?結束*/
OK
[源碼下載]
總結
以上是生活随笔為你收集整理的扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CommandName 和 Comman
- 下一篇: 安装vc2005运行库错误1935c处理