ASP.NET 魔法學院
posts - 58, comments - 260, trackbacks - 14, articles - 0
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
扩展 CommandField 类别 - Header 加入新增钮
Posted on 2008-05-14 00:00
jeff377
阅读(1194)
评论(4)
编辑
收藏
所属分类:
服务器控件
摘要
延续前面「GridView+FormView 示范数据 新增/修改/删除(进阶篇:服务器控件)」的文章,文章后记有提及若要达到零程序代码要求,其中一个要件就是需要扩展 CommandField 类别,在 CommandField 的 Header 的部分加入「新增」钮,本文就是在说明如何扩展 CommandField 类别达到此需求。
扩展 CommandField 类别
我们的需求是在 CommandField 的 Header 加入新增钮,作法是继承 CommandField 下来命名为 TBCommandField,新增 ShowHeaderInsertButton 属性,来设定 Header 是否显示新增钮。TBCommandField 的程序代码如下,作法是覆写 InitializeCell 方法,若为 Header 且有设定 ShowHeaderInsertButton="True" 则利用 AddButtonToCell 私有方法来加入「新增」钮。
1
Imports
System
2
Imports
System.Collections.Generic
3
Imports
System.ComponentModel
4
Imports
System.Text
5
Imports
System.Web
6
Imports
System.Web.UI
7
Imports
System.Web.UI.WebControls
8
Imports
System.Globalization
9
10
Namespace WebControls
Namespace
WebControls
11
Public
Class TBCommandField
Class
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
103
End Namespace
使用 TBCommandField 类别
因为 GridView.Columns 的属性编辑器不支持我们改写 TBCommandField,故只能切换至 aspx 程序代码中手动加入。
<
bee:TBCommandField
ShowHeaderInsertButton
="True"
InsertText
="新增"
ShowEditButton
="True"
ShowDeleteButton
="True"
ButtonType
="Button"
/>
切换至设计画面,就可以看到 TBCommandField 的 Header 出现「新增」钮。
备注:若要让 GridView 的字段编辑器支持 TBCommandField,需自订 GridView.Columns 的属性编辑器。
Tag标签:
GridView
,
CommandField
Feedback
#1楼
回复
引用
查看
2008-05-14 09:37 by
Clark Zheng
不错
#2楼
回复
引用
查看
2008-05-14 12:05 by
1-2-3
收藏
#3楼
回复
引用
查看
2008-05-14 14:28 by
Scott Xu(南方小鬼)
有个问题,当记录条数为零的时候咋办呢?
#4楼
[
楼主
]
回复
引用
查看
2008-05-14 15:15 by
jeff377
@Scott Xu(南方小鬼)
記錄數為零時,GridView 是會由 EmptyTemplate 產生,所以不會去 Render Command 的 Header。
要讓記錄數為零時能 Render 所有 Columns 的 Header 也是有辨法,就是改寫 GridView。
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
另存
打印
所属分类的其他文章:
·
扩展 Calendar 控件 - DayCommand 事件
·
让 SqlDataSource 的 SelectCommand 属性值不会消失
·
ObjectDataSoruce 系结 BusinessObject 控件
·
GridView 自动编号字段 - TBSerialNumberField
·
扩展 CheckBoxList 控件 - 系结复选项目(2)
·
扩展 CheckBoxList 控件 - 系结复选项目
·
扩展 GridView 控件 - 无数据时显示标题列
·
扩展 CheckBoxField 类别 - 支持非布尔值的双向系结
·
扩展 GridView 控件 - 支持 Excel 及 Word 汇出
·
扩展 TextBox 控件 - 依 FormViewMode 来自行设定状态
最新IT新闻:
·
微软推新型搜索技术"BrowseRank"挑战谷歌
·
2008年7月26日IT博客精选
·
微软每年向Apache捐10万美元支持开源软件
·
AOL将关闭3个网站以降低成本 集中发力广告
·
谷歌网页索引数量突破1万亿个
博客园新闻频道
博客园首页
社区
Powered by:
博客园
Copyright © jeff377
日历
<
2008年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
公告
☆ 有些事現在不做,就一輩子都不會做了 ☆
与我互动
给我发短消息
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看私人留言
我参加的小组
jQuery
我的标签
GridView(12)
ServerControl(8)
SqlDataSource(4)
BasePage(3)
FormView(3)
Calendar(2)
RenderControl(2)
CheckBoxList(2)
DataControlField(1)
内框线(1)
更多
随笔分类
AJAX(3)
API(3)
ASP.NET 基础(25)
ASP.NET 进阶(1)
DataSource 控件(2)
FormView
GridView(2)
VB.NET(3)
動態建立控制項(1)
服务器控件(18)
随笔档案
2008年7月 (1)
2008年6月 (10)
2008年5月 (17)
2008年4月 (6)
2008年2月 (1)
2008年1月 (5)
2007年12月 (18)
部落格
ASP.NET 魔法學院(繁體)
topcat姍舞之間的極度凝聚
搜索
最新评论
1. re: GridView+FormView 示范数据 新增/修改/删除
xue xi.
--雪涛
2. re: SqlDataSource 執行資料篩選
非常感谢,你的方法帮了我大忙,对比起来你的方法最优,其他方法在换页或者查询条件较复杂的时侯都会有问题。Thanks a lot!!!
--keanuzhang
3. re: SqlDataSource 執行資料篩選
2楼,我只使用了sqldatasource.FilterExpression来设置过滤参数,但是如果筛选后的结果大于1页的话,在分页时还是会将全部数据带出来
--wythz
4. re: 利用 BasePage 实作窗体权限控管
有没有C#的版本啊。
--鹏鹏_Lovely
5. re: 十几行程序代码搞定 Master-Detail GridView(内含子 GridView)
@蜀山雪狼
Sorry. 没有注意到简繁混杂了。
--jeff377
阅读排行榜
1. 利用 BasePage 实作窗体权限控管(2521)
2. 十几行程序代码搞定 Master-Detail GridView(内含子 GridView)(2356)
3. 用属性封装 Session 及 VIewState 的存取(2199)
4. 扩展 GridView 控件 - 无数据时显示标题列(2120)
5. 维护 Panel 滚动条位置(2062)
评论排行榜
1. 扩展 GridView 控件 - 无数据时显示标题列(22)
2. 用属性封装 Session 及 VIewState 的存取(19)
3. 動態新增控制項與 ViewState(17)
4. 利用 BasePage 实作窗体权限控管(17)
5. 十几行程序代码搞定 Master-Detail GridView(内含子 GridView)(16)
60天内阅读排行
1. 利用 BasePage 实作窗体权限控管(2521)
2. 十几行程序代码搞定 Master-Detail GridView(内含子 GridView)(2356)
3. ObjectDataSoruce 系结 BusinessObject 控件(1779)
4. GridView 自动编号字段 - TBSerialNumberField(1539)
5. 扩展 Calendar 控件 - DayCommand 事件(1442)