日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/8/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
GridView既強(qiáng)大又好用。為了讓它更強(qiáng)大、更好用,我們來寫一個(gè)繼承自GridView的控件。
[索引頁(yè)]
[源碼下載]


擴(kuò)展GridView控件(3) - 根據(jù)按鈕的CommandName設(shè)置其客戶端屬性


作者: webabcd


/*正式版的實(shí)現(xiàn)?開始*/
介紹
擴(kuò)展GridView控件:
根據(jù)按鈕的CommandName設(shè)置其客戶端屬性

使用方法(設(shè)置ClientButtons集合屬性):
BoundCommandName - 需要綁定的CommandName
AttributeKey - 屬性的名稱
AttributeValue - 屬性的值(兩個(gè)占位符:{0} - CommandArgument;{1} - Text)
Position - 屬性的值的位置


關(guān)鍵代碼
using?System;
using?System.Collections.Generic;
using?System.Text;

using?System.Web.UI.WebControls;
using?System.Web.UI;

namespace?YYControls.SmartGridViewFunction
{
????///?<summary>
????
///?擴(kuò)展功能:根據(jù)按鈕的CommandName設(shè)置其客戶端屬性
????
///?</summary>
????public?class?ClientButtonFunction?:?ExtendFunction
????{
????????///?<summary>
????????
///?構(gòu)造函數(shù)
????????
///?</summary>
????????public?ClientButtonFunction()
????????????:?base()
????????{

????????}

????????///?<summary>
????????
///?構(gòu)造函數(shù)
????????
///?</summary>
????????
///?<param?name="sgv">SmartGridView對(duì)象</param>
????????public?ClientButtonFunction(SmartGridView?sgv)
????????????:?base(sgv)
????????{

????????}

????????///?<summary>
????????
///?擴(kuò)展功能的實(shí)現(xiàn)
????????
///?</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里的每個(gè)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);
????????????????????????????
????????????????????????????//?設(shè)置按鈕的客戶端屬性
????????????????????????????YYControls.Helper.Common.SetAttribute(
????????????????????????????????(IAttributeAccessor)c,?
????????????????????????????????cb.AttributeKey,?
????????????????????????????????attributeValue,?
????????????????????????????????cb.Position);

????????????????????????????break;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????}
????}
}

/*正式版的實(shí)現(xiàn)?結(jié)束*/

/*測(cè)試版的實(shí)現(xiàn)?開始*/
介紹
給按鈕增加單擊彈出確認(rèn)框的功能是經(jīng)常要用到的,我們一般是通過在RowDataBound事件里編碼的方式實(shí)現(xiàn),麻煩,所以擴(kuò)展一下。


控件開發(fā)
1、新建一個(gè)繼承自GridView的類。
///?<summary>
///?繼承自GridView
///?</summary>
[ToolboxData(@"<{0}:SmartGridView?runat='server'></{0}:SmartGridView>")]
public?class?SmartGridView?:?GridView
{
}
2、新建一個(gè)ConfirmButton類,有兩個(gè)屬性
????///?<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>
????????
///?確認(rèn)框彈出的信息
????????
///?</summary>
????????public?string?ConfirmMessage
????????{
????????????get?{?return?this._confirmMessage;?}
????????????set?{?this._confirmMessage?=?value;?}
????????}
????}
3、新建一個(gè)繼承自CollectionBase的類ConfirmButtons
????///?<summary>
????
///?ProjectGroups?的摘要說明。
????
///?注意要繼承自CollectionBase
????
///?</summary>
????[
????ToolboxItem(false),
????ParseChildren(true)
????]
????public?class?ConfirmButtons?:?CollectionBase
????{
????????///?<summary>
????????
///?構(gòu)造函數(shù)
????????
///?</summary>
????????public?ConfirmButtons()
????????????:?base()
????????{
????????}

????????///?<summary>
????????
///?實(shí)現(xiàn)IList接口
????????
///?獲取或設(shè)置指定索引處的元素。
????????
///?</summary>
????????
///?<param?name="index">要獲得或設(shè)置的元素從零開始的索引</param>
????????
///?<returns></returns>
????????public?ConfirmButton?this[int?index]
????????{
????????????get
????????????{
????????????????return?(ConfirmButton)base.List[index];
????????????}
????????????set
????????????{
????????????????base.List[index]?=?(ConfirmButton)value;
????????????}
????????}

????????///?<summary>
????????
///?實(shí)現(xiàn)IList接口
????????
///?將某項(xiàng)添加到?System.Collections.IList?中。
????????
///?</summary>
????????
///?<param?name="item">要添加到?System.Collections.IList?的?System.Object。</param>
????????public?void?Add(ConfirmButton?item)
????????{
????????????base.List.Add(item);
????????}

????????///?<summary>
????????
///?實(shí)現(xiàn)IList接口
????????
///?從?System.Collections.IList?中移除特定對(duì)象的第一個(gè)匹配項(xiàng)。
????????
///?</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、新建一個(gè)繼承自ExpandableObjectConverter的類ConfirmButtonConverter
????///?<summary>
????
///?類型轉(zhuǎn)換器
????
///?</summary>
????public?class?ConfirmButtonConverter?:?ExpandableObjectConverter
????{
????????///?<summary>
????????
///?返回值能否將ConfirmButton類型轉(zhuǎn)換為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類型轉(zhuǎn)換為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(
????????????????????????"無(wú)效的ConfirmButton",?"value");
????????????????}
????????????}

????????????if?(destinationType.Equals(typeof(string)))
????????????{
????????????????if?(value?==?null)
????????????????{
????????????????????return?String.Empty;
????????????????}
????????????????return?"ConfirmButton";
????????????}
????????????return?base.ConvertTo(context,?culture,?value,?destinationType);
????????}
????}
5、在繼承自GridView的類中加一個(gè)復(fù)雜對(duì)象屬性,該復(fù)雜對(duì)象就是第3步創(chuàng)建的那個(gè)ConfirmButtons
????????private?ConfirmButtons?_confirmButtons;
????????///?<summary>
????????
///?確認(rèn)按鈕集合
????????
///?</summary>
????????[
????????PersistenceMode(PersistenceMode.InnerProperty),
????????DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
????????Description("確認(rèn)按鈕集合,確認(rèn)按鈕的CommandName和提示信息"),
????????Category("擴(kuò)展")
????????]
????????public?virtual?ConfirmButtons?ConfirmButtons
????????{
????????????get
????????????{
????????????????if?(_confirmButtons?==?null)
????????????????{
????????????????????_confirmButtons?=?new?ConfirmButtons();
????????????????}
????????????????return?_confirmButtons;
????????????}
????????}

6、重寫OnRowDataBound實(shí)現(xiàn)單擊命令按鈕彈出確認(rèn)框的功能。主要是給按鈕增加一個(gè)客戶端的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的每個(gè)TableCell
????????????????????foreach?(TableCell?tc?in?e.Row.Cells)
????????????????????{
????????????????????????//?TableCell里的每個(gè)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)
????????????????????????????????{
????????????????????????????????????//?如果發(fā)現(xiàn)的按鈕的CommandName在ConfirmButtons有定義的話
????????????????????????????????????if?(((IButtonControl)c).CommandName?==?cb.CommandName)
????????????????????????????????????{
????????????????????????????????????????//?增加確認(rèn)框?qū)傩?/span>
????????????????????????????????????????((IAttributeAccessor)c).SetAttribute("onclick",?"return?confirm('"?+?cb.ConfirmMessage?+?"')");
????????????????????????????????????????break;
????????????????????????????????????}
????????????????????????????????}
????????????????????????????}
????????????????????????}
????????????????????}
????????????????}
????????????}

????????????base.OnRowDataBound(e);
????????}


控件使用
添加這個(gè)控件到工具箱里,然后拖拽到webform上,設(shè)置其ConfirmButtons屬性即可。CommandName是命令按鈕的CommandName屬性;ConfirmMessage是彈出的確認(rèn)框所顯示的文字。
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:?在此處添加構(gòu)造函數(shù)邏輯
????????
//
????}

????[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>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
????<form?id="form1"?runat="server">
????????<div>
????????????&nbsp;
????????????<yyc:SmartGridView?ID="SmartGridView1"?runat="server"?AutoGenerateColumns="false"
????????????????DataSourceID
="ObjectDataSource1">
????????????????<Columns>
????????????????????<asp:BoundField?DataField="no"?HeaderText="序號(hào)"?SortExpression="no"?/>
????????????????????<asp:BoundField?DataField="name"?HeaderText="名稱"?SortExpression="name"?/>
????????????????????<asp:ButtonField?CommandName="ConfirmTest"?Text="確認(rèn)按鈕測(cè)試"?/>
????????????????</Columns>
????????????????<ConfirmButtons>
????????????????????<yyc:ConfirmButton?ConfirmMessage="確認(rèn)刪除嗎?"?CommandName="ConfirmTest"></yyc:ConfirmButton>
????????????????</ConfirmButtons>
????????????</yyc:SmartGridView>
????????????<asp:ObjectDataSource?ID="ObjectDataSource1"?runat="server"?SelectMethod="Select"
????????????????TypeName
="OjbData"></asp:ObjectDataSource>
????????</div>
????</form>
</body>
</html>

/*測(cè)試版的實(shí)現(xiàn)?結(jié)束*/

OK
[源碼下載]

總結(jié)

以上是生活随笔為你收集整理的扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。