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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

扩展 CommandField 类别 - Header 加入新增钮

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 扩展 CommandField 类别 - Header 加入新增钮 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要
延續前面「GridView+FormView 示范數據 新增/修改/刪除(進階篇:服務器控件)」的文章,文章后記有提及若要達到零程序代碼要求,其中一個要件就是需要擴展 CommandField 類別,在 CommandField 的 Header 的部分加入「新增」鈕,本文就是在說明如何擴展 CommandField 類別達到此需求。

擴展 CommandField 類別
我們的需求是在 CommandField 的 Header 加入新增鈕,作法是繼承 CommandField 下來命名為 TBCommandField,新增 ShowHeaderInsertButton 屬性,來設定 Header 是否顯示新增鈕。TBCommandField 的程序代碼如下,作法是覆寫 InitializeCell 方法,若為 Header 且有設定 ShowHeaderInsertButton="True" 則利用 AddButtonToCell 私有方法來加入「新增」鈕。

??1Imports?System
??2Imports?System.Collections.Generic
??3Imports?System.ComponentModel
??4Imports?System.Text
??5Imports?System.Web
??6Imports?System.Web.UI
??7Imports?System.Web.UI.WebControls
??8Imports?System.Globalization
??9
?10Namespace?WebControlsNamespace?WebControls
?11????Public?Class?TBCommandFieldClass?TBCommandField
?12????????Inherits?CommandField
?13
?14????????Private?FShowHeaderInsertButton?As?Boolean?=?False
?15
?16????????/**/'''?<summary>
?17????????'''?初始化儲存格。
?18????????'''?</summary>
?19????????'''?<param?name="cell">要初始化的儲存格。</param>
?20????????'''?<param?name="cellType">儲存格類型。</param>
?21????????'''?<param?name="rowState">儲存格狀態。</param>
?22????????'''?<param?name="rowIndex">數據列之以零起始的索引。</param>

?23????????Public?Overrides?Sub?InitializeCell()Sub?InitializeCell(ByVal?cell?As?DataControlFieldCell,?ByVal?cellType?As?DataControlCellType,?ByVal?rowState?As?DataControlRowState,?ByVal?rowIndex?As?Integer)
?24????????????MyBase.InitializeCell(cell,?cellType,?rowState,?rowIndex)
?25
?26????????????If?Me.ShowHeaderInsertButton?AndAlso?(cellType?=?DataControlCellType.Header)?Then
?27????????????????'標題加入新增鈕
?28????????????????AddButtonToCell(cell,?"New",?Me.InsertText,?Me.CausesValidation,?Me.ValidationGroup,?rowIndex,?Me.InsertImageUrl)
?29????????????End?If
?30????????End?Sub

?31
?32????????/**/'''?<summary>
?33????????'''?標題儲存格是否顯示新增鈕。
?34????????'''?</summary>

?35????????<?_
?36????????Description("標題儲存格是否顯示新增鈕"),?_
?37????????DefaultValue(False)?_
?38????????>?_
?39????????Public?Property?ShowHeaderInsertButton()Property?ShowHeaderInsertButton()?As?Boolean
?40????????????Get
?41????????????????Return?FShowHeaderInsertButton
?42????????????End?Get
?43????????????Set(ByVal?value?As?Boolean)
?44????????????????If?FShowHeaderInsertButton?<>?value?Then
?45????????????????????FShowHeaderInsertButton?=?value
?46????????????????????Me.OnFieldChanged()
?47????????????????End?If
?48????????????End?Set
?49????????End?Property

?50
?51????????/**/'''?<summary>
?52????????'''?儲存格加入按鈕。
?53????????'''?</summary>
?54????????'''?<param?name="Cell">儲存格。</param>
?55????????'''?<param?name="CommandName">按鈕命令。</param>
?56????????'''?<param?name="ButtonText">按鈕文字。</param>
?57????????'''?<param?name="CausesValidation">是否執行驗證。</param>
?58????????'''?<param?name="ValidationGroup">驗證控件所屬之驗證群組的名稱。</param>
?59????????'''?<param?name="RowIndex">列索引。</param>
?60????????'''?<param?name="ImageUrl">影像網址。</param>

?61????????Private?Sub?AddButtonToCell()Sub?AddButtonToCell(ByVal?Cell?As?DataControlFieldCell,?ByVal?CommandName?As?String,?ByVal?ButtonText?As?String,?ByVal?CausesValidation?As?Boolean,?ByVal?ValidationGroup?As?String,?ByVal?RowIndex?As?Integer,?ByVal?ImageUrl?As?String)
?62????????????Dim?oButtonControl?As?IButtonControl
?63
?64????????????Select?Case?Me.ButtonType
?65????????????????Case?ButtonType.Button
?66????????????????????oButtonControl?=?New?Button()
?67????????????????????Exit?Select
?68????????????????Case?ButtonType.Link
?69????????????????????oButtonControl?=?New?LinkButton()
?70????????????????Case?Else
?71????????????????????oButtonControl?=?New?ImageButton()
?72????????????????????DirectCast(oButtonControl,?ImageButton).ImageUrl?=?ImageUrl
?73????????????????????Exit?Select
?74????????????End?Select
?75????????????oButtonControl.Text?=?ButtonText
?76????????????oButtonControl.CommandName?=?CommandName
?77????????????oButtonControl.CommandArgument?=?RowIndex.ToString(CultureInfo.InvariantCulture)
?78????????????oButtonControl.CausesValidation?=?CausesValidation
?79????????????oButtonControl.ValidationGroup?=?ValidationGroup
?80????????????Cell.Controls.Add(DirectCast(oButtonControl,?WebControl))
?81????????End?Sub

?82
?83????????/**/'''?<summary>
?84????????'''?建立新的?TBCommandField?對象。?
?85????????'''?</summary>

?86????????Protected?Overrides?Function?CreateField()Function?CreateField()?As?DataControlField
?87????????????Return?New?TBCommandField()
?88????????End?Function

?89
?90????????/**/'''?<summary>
?91????????'''?將目前?TBCommandField?對象的屬性復制到指定之?DataControlField?對象。
?92????????'''?</summary>
?93????????'''?<param?name="newField">目的?DataControlField?對象。</param>

?94????????Protected?Overrides?Sub?CopyProperties()Sub?CopyProperties(ByVal?NewField?As?DataControlField)
?95????????????Dim?oNewField?As?TBCommandField
?96
?97????????????oNewField?=?DirectCast(NewField,?TBCommandField)
?98????????????oNewField.ShowHeaderInsertButton?=?Me.ShowHeaderInsertButton
?99????????????MyBase.CopyProperties(NewField)
100????????End?Sub

101
102????End?Class

103End?Namespace

使用 TBCommandField 類別
因為 GridView.Columns 的屬性編輯器不支持我們改寫 TBCommandField,故只能切換至 aspx 程序代碼中手動加入。

<bee:TBCommandField?ShowHeaderInsertButton="True"?InsertText="新增"?ShowEditButton="True"?ShowDeleteButton="True"?ButtonType="Button"??/>
切換至設計畫面,就可以看到 TBCommandField 的 Header 出現「新增」鈕。




備注:若要讓 GridView 的字段編輯器支持 TBCommandField,需自訂 GridView.Columns 的屬性編輯器。

轉載于:https://www.cnblogs.com/jeff377/archive/2008/05/14/1196005.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的扩展 CommandField 类别 - Header 加入新增钮的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。