DetailsView 控件指定数据源控件,就可以使用单条方式显示记录数据,不仅如此,它同样可以使用分页来显示,如下所示。
<asp:AccessDataSource Id="Products" Runat="server"
DataFile="~/Products.mdb"
SelectCommand="SELECT ProductNo,ProductName, ProductPrice,InStock FROM Products"/>
<asp:DetailsView Id="detail" Runat="server"
DataSourceID="Products"
HeaderText="图书信息"
HeaderStyle-BackColor="#CC99FF"
AllowPaging="True"
PagerSettings-Position="Bottom"
RowStyle-VerticalAlign="Top"/>
上述DetailsView控件指定数据源控件Products,因为AutoGenerateColumns属性默认值为True,所以能够自动产生Field控件的字段,AllowPaging属性为 Ture,表示分页显示。完整ASP.NET程序范例是Ch10-5-1.aspx,其执行结果如图10-13所示。
图10-13 Ch10-5-1.aspx运行结果
10.5.2 DetailsView控件的Field控件
DetailsView 控件也支持 GridView 的 Field 控件,可以自行定义字段类型,如下所示。
<asp:DetailsView Id="detail" Runat="server"
DataSourceID="Products" Width="350px"
AutoGenerateRows="False"
……….
RowStyle-VerticalAlign="Top">
<Fields>
<asp:BoundField ………/>
………
<asp:CheckBoxField …….../>
<asp:ImageField ………/>
</Fields>
</asp:DetailsView>
上述 DatailsView 控件的 AutoGenerateRows 属性为 False,所以需要使用 Field 控件来定义显示字段,这些控件位于在 <Fields>标记。
1. ASP.NET 程序:Ch10-5-2.aspx
在 ASP.NET程序中使用 DetailsView 控件的 Field 控件,以单条方式显示数据表的记录数据,如下所示。
01: <html>
02: <head><title>Ch10-5-2</title></head>
03: <body>
04: <form Runat="server">
05: <asp:AccessDataSource Id="Products" Runat="server"
06: DataFile="~/Products.mdb"
07: SelectCommand="SELECT * FROM Products"/>
08: <asp:DetailsView Id="detail" Runat="server"
09: DataSourceID="Products" Width="350px"
10: AutoGenerateRows="False"
11: HeaderText="图书信息"
12: HeaderStyle-BackColor="#CC99FF"
13: HeaderStyle-HorizontalAlign="Center"
14: AllowPaging="True"
15: PagerSettings-Position="Bottom"
16: RowStyle-VerticalAlign="Top">
17: <Fields>
18: <asp:BoundField HeaderText="书号"
19: DataField="ProductNo" ReadOnly="True"
20: HeaderStyle-BackColor="#E0E0E0"
21: HeaderStyle-Font-Bold="True"/>
22: <asp:BoundField HeaderText="书名"
23: DataField="ProductName" NullDisplayText="没有书名"
24: HeaderStyle-BackColor="#E0E0E0"
25: HeaderStyle-Font-Bold="True"/>
26: <asp:BoundField HeaderText="书价" HtmlEncode="False"
27: DataField="ProductPrice" DataFormatString="{0:C}"
28: HeaderStyle-BackColor="#E0E0E0"
29: HeaderStyle-Font-Bold="True"/>
30: <asp:CheckBoxField HeaderText="库存" DataField="InStock"
31: HeaderStyle-BackColor="#E0E0E0"
32: HeaderStyle-Font-Bold="True"/>
33: <asp:ImageField HeaderText="封面"
34: DataImageUrlField="ProductNo"
35: DataImageUrlFormatString="~/{0}.jpg"
36: HeaderStyle-BackColor="#E0E0E0"
37: HeaderStyle-Font-Bold="True"/>
38: </Fields>
39: </asp:DetailsView>
40: </form>
41: </body>
42: </html>
2. 程序说明
第8~39行是DetailsView控件,AutoGenerateColumns属性值False,表示需要使用Field控件定义字段,这是位于在第17~38行的<Fields>标记,包含ButtonField、CheckBoxField 和 ImageField 控件。
3. 网页预览
将Ch10文件夹建立为虚拟目录后,启动浏览程序执行 ASP.NET 程序,可以看到 DetailsView 控件显示的单条记录数据,如图10-14所示。
图10-14 Ch10-5-2.aspx运行结果
10.5.3 DetailsView控件的编辑功能
DetailsView 控件提供数据表记录的编辑功能,不仅可以更新和删除记录,还可以在数据表中插入新记录。
1. DetailsView控件的基本编辑功能
DetailsView 控件的编辑功能可以自动建立,只需在数据源控件中建立所需的 SQL 命令,如下所示。
<asp:AccessDataSource Id="Products" Runat="server"
DataFile="~/Products.mdb"
SelectCommand="SELECT ProductNo,ProductName,
ProductPrice,InStock FROM Products"
UpdateCommand="UPDATE Products SET
ProductName=@ProductName,
ProductPrice=@ProductPrice,
InStock=@InStock
WHERE ProductNo=@ProductNo"
DeleteCommand="DELETE From Products
WHERE ProductNo=@ProductNo"
InsertCommand="INSERT INTO Products(ProductNo, ProductName,ProductPrice,Instock)
VALUES(@ProductNo,@ProductName, @ProductPrice,@Instock)"/>
上述 UpdateCommand、DeleteCommand 和 InsertCommand 属性分别指定更新、删除和插入记录的 SQL 命令,@ 开头是对应字段名称的参数。
现在只需在 DetailsView 控件中打开编辑功能,就可以使用上述 SQL 命令来编辑数据表的记录数据,如下所示。
<asp:DetailsView Id="detail" Runat="server"
DataSourceID="Products" Width="300pt"
………
AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True"
AutoGenerateInsertButton="True"
DataKeyNames="ProductNo"/>
上述 AutoGenerate…Button 属性依次创建编辑、删除和新建的超级链接,最后的 DataKeyNames 属性指定主键为 ProductNo。完整 ASP.NET 程序范例是 Ch10-5-3Auto.aspx,可以在表格中添加编辑行,如图10-15所示。
图10-15 Ch10-5-3Auto.aspx运行结果
2. CommandField控件
CommandField 控件可以在 DetailsView 控件建立所需样式的编辑行,只需将 AutoGenerate…Button 属性设为 False,就可以在 <Fields> 标记的字段区创建 CommandField 控件,如下所示。
<asp:CommandField
ButtonType="Button"
HeaderText="编辑"
HeaderStyle-BackColor="#E0E0E0"
HeaderStyle-Font-Bold="True"
ItemStyle-BackColor="#E0E0E0"
ShowHeader="True"
ShowInsertButton="True"
ShowEditButton="True"
ShowDeleteButton="True"/>
上述标记指定编辑行字段名称、样式和是否显示各编辑按钮。完整 ASP.NET 程序范例是 Ch10-5-3Command.aspx,可以在表格中创建自订样式的编辑行,如图10-16所示。
图10-16 Ch10-5-3Command.aspx运行结果
3. TemplateFiled控件
DetailsView 控件同样可以使用 TemplateField 控件来自订所需的数据输入字段,其使用方式和 GridView 控件相似,唯一差异是多了一个 Insert 按钮,如下所示。
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:Button Text="创建" CommandName="New"
Runat="server"/>
………
</ItemTemplate>
<EditItemTemplate>…</EditItemTemplate>
<InsertItemTemplate>
<asp:Button Text="插入" CommandName="Insert"
Runat="server"/>
<asp:Button Text="取消" CommandName="Cancel"
Runat="server"/>
</InsertItemTemplate>
</asp:TemplateField>
在上述 <ItemTemplate> 标记创建 CommandName 属性为 New 的 Button 控件,然后创建 <InsertItemTemplate> 标记的 Insert 和 Cancel 按钮。完整 ASP.NET 程序范例是 Ch10-5-3.aspx,其执行结果如图10-17所示。
图10-17 Ch10-5-3.aspx运行结果
4. 编辑事件与事件处理
DetailsView 控件的编辑事件与事件处理参数对象如表10-22所示。
表10-22 DetailsView 控件的编辑事件与事件处理参数对象
事 件
|
程序参数对象
|
说 明
|
ItemInserting
|
DetailsViewInsertEventArgs
|
插入但未写入数据源
|
ItemInserted
|
DetailsViewInsertedEventArgs
|
插入且写入数据源
|
ItemUpdating
|
DetailsViewUpdateEventArgs
|
更新但未写入数据源
|
ItemUpdated
|
DetailsViewUpdatedEventArgs
|
更新且写入数据源
|
ItemDeleting
|
DetailsViewDeleteEventArgs
|
删除但未写入数据源
|
ItemDeleted
|
DetailsViewDeletedEventArgs
|
删除且写入数据源
|
例如,ItemUpdating 事件处理程序可以进行数据验证,如下所示。
Sub validateData(Sender As Object, _
E As DetailsViewUpdateEventArgs)
If Not IsNumeric(E.NewValues("ProductPrice"))Then
E.Cancel = True
show.Text="书价需为数值........"
End If
End Sub
上述程序可以检查 NewValues 属性获取的新输入值是否为数值。完整 ASP.NET 程序范例是 Ch10-5-3Event.aspx。
detailview的控件基础相关推荐
- Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它
Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它 TFmxObject 增加了 TagObject.TagFloat.TagString, 算 ...
- Spread for WinForms 电子表格控件基础视频教程-王继飞-专题视频课程
Spread for WinForms 电子表格控件基础视频教程-23145人已学习 课程介绍 Spread for WinForms 电子表格控件基础视频教程 课程收益 讲 ...
- 常用MFC控件基础使用(Edit Button Check Combo Tree 显示位图 控件大小自适应、线程、右键菜单、控件调整移动、MFC背景)
常用MFC控件基础使用 1.Edit Control 2.Button控件 3.Check控件 4.Combo Box 5.Tree 控件 5.快捷键设置 6 显示缓冲区位图 7 窗口自适应大小 8 ...
- Delphi XE2 之 FireMonkey 入门(37) - 控件基础: TControl 概览
{ TControl } publicconstructor Create(...); override; //destructor Destroy; override; //procedure Ad ...
- C# chart控件基础使用
基本介绍:chart(图表) 功能:主要用来绘制折线图,柱状图与饼状图,也可达到动态效果(例如作示波器): 需要说明 一个chart可以包含多个chartArea. chartArea是具体的坐标区域 ...
- [00004]-[2015-07-16]-[00]-[VC++ 开发Activity控件基础]
Visual C++是开发ActiveX控件的强大工具,它的特点是开发周期短.便于使用,因此它已经成为开发ActiveX控件的主要工具之一.Visual C++集成开发环境,使用了微软自己的类库MFC ...
- [Winodows Phone 7控件详解]控件基础
Windows Phone7提供了丰富的silverlight控件,但是和silverlight又有一定的区别的,其中有很多控件都是不可用的,另外有些控件即使可以用,但有一些属性也是不可用的.后面将一 ...
- Win10系列:C#应用控件基础7
Slider控件 Slider控件包含一个滑动条.一个滑动块和一个取值范围,沿滑动条移动滑动块可以在取值范围内改变Slider控件的值.Slider控件的用途很广泛,例如可以使用Slider控件来设置 ...
- Win10系列:C#应用控件基础12
TextBlock控件 TextBlock控件是应用程序开发过程中经常使用的控件之一,它的主要功能是显示一段只读的文本内容.开发者可以使用TextBlock控件来显示提示信息,还可以根据需求将显示的提 ...
最新文章
- 业务脆弱性评估是业务持续性保障(BCM)的基础数据
- Detect combined string
- 图像恢复迭代算法的加速
- php memcache可存,php使用memcache共享存储session(二)
- 山师计算机试题答案,山师计算机应用技术试题及答案
- C++中如何定义动态数组
- [Leedcode][JAVA][第460题][LFU]
- 使用Numpy和Opencv完成图像的基本数据分析(Part III)
- springboot2.0 fastjson 日期格式化不生效解决
- (5)css样式表特征
- php define函数
- SM4算法详解(2021-12-8)
- 华为交换机eth口作用_华为5700交换机eth接口做什么用的?怎么使用它?
- 拍牌(沪牌)软件,开源全部代码。有精力可以自己研究然后自用拍牌
- 自动驾驶扎堆“重感知”路线:毫末智行如何从独行到领航?
- vue中使用svg图片
- [UOJ198]时空旅行
- 刚刚,优酷的模版引擎,竟然开源了!
- Windows电脑 添加 安卓或者苹果平板作为拓展屏(spacedesk )
- 高数 07.08 二重积分的计算
热门文章
- Eth-Trunk的配置
- 冰河的高并发电子书开源啦(文末免费领取)!!
- Liferay的学习
- Flink【优质】面试题摘录
- 逃离美国,跨越8000公里远程办公,他开发了世界上最强的虚拟机
- 发邮件的JAVA程序
- 磁盘挂载报错/dev/sdb1 is apparently in use by the system
- 2020最实用115个Java面试题及面向对象的能力汇总合集
- Filament加载并渲染glTF模型
- Matlab中meshgrid的用法