特点: 一次只显示一条,而且分页也是跳到下一条数据.显示数据的格式与GridView 差不多,表格显示,很规范,很难任意布局
FormView 与 DataList 差不多,可以任意布局, 可以说随心所欲吧.^_^
DetailsView 主要是显示某一条记录的时候,显示详细数据.
下面有几个它的知识点:
1: 它的分页, 其实不是一页内多条记录的分页,而是一页内显示的就一条记录的相关字段,
确切的说是指 由同一个查询条件所得的 一些记录,比如:这里的查询条件是: state ,要将
GridView 的 DataKeyNames 设置为state, 这里它的默认值为: au_id
2: 在一列中按照需要组合显示多个数据字段
实现方法是: 先转换为模板列,数据模板中添加一个Label,再绑定数据,编辑模板中也要添加一个TextBox,然 后绑定数据,不用写处理代码,它自动可以更新的.
特点: 一次只显示一条,而且分页也是跳到下一条数据.显示数据的格式与GridView 差不多,表格显示,很规范,很难任意布局
FormView 与 DataList 差不多,可以任意布局, 可以说随心所欲吧.^_^
DetailsView 主要是显示某一条记录的时候,显示详细数据.
下面有几个它的知识点:
1: 它的分页, 其实不是一页内多条记录的分页,而是一页内显示的就一条记录的相关字段,
确切的说是指 由同一个查询条件所得的 一些记录,比如:这里的查询条件是: state ,要将
GridView 的 DataKeyNames 设置为state, 这里它的默认值为: au_id
2: 在一列中按照需要组合显示多个数据字段
实现方法是: 先转换为模板列,数据模板中添加一个Label,再绑定数据,编辑模板中也要添加一个TextBox,然 后绑定数据,不用写处理代码,它自动可以更新的.
===================
前台页面 DetailsView.aspx 注意:这个事例没有写后台代码
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsView.aspx.cs" Inherits="DetailsView" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
13 SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [city], [state] FROM [authors]">
14 </asp:SqlDataSource>
15
16 </div>
17 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
18 BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
19 CellPadding="4" DataKeyNames="state" DataSourceID="SqlDataSource1" PageSize="5">
20 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
21 <Columns>
22 <asp:CommandField ShowSelectButton="True" />
23 <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
24 <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
25 <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
26 <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
27 <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
28 <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
29 </Columns>
30 <RowStyle BackColor="White" ForeColor="#330099" />
31 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
32 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
33 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
34 </asp:GridView>
35 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
36 DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id" InsertCommand="INSERT INTO [authors] ([au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract]) VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip, @contract)"
37 SelectCommand="SELECT * FROM [authors] WHERE ([state] = @state)" UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zip] = @zip, [contract] = @contract WHERE [au_id] = @au_id">
38 <DeleteParameters>
39 <asp:Parameter Name="au_id" Type="String" />
40 </DeleteParameters>
41 <UpdateParameters>
42 <asp:Parameter Name="au_lname" Type="String" />
43 <asp:Parameter Name="au_fname" Type="String" />
44 <asp:Parameter Name="phone" Type="String" />
45 <asp:Parameter Name="address" Type="String" />
46 <asp:Parameter Name="city" Type="String" />
47 <asp:Parameter Name="state" Type="String" />
48 <asp:Parameter Name="zip" Type="String" />
49 <asp:Parameter Name="contract" Type="Boolean" />
50 <asp:Parameter Name="au_id" Type="String" />
51 </UpdateParameters>
52 <SelectParameters>
53 <asp:ControlParameter ControlID="GridView1" Name="state" PropertyName="SelectedValue"
54 Type="String" />
55 </SelectParameters>
56 <InsertParameters>
57 <asp:Parameter Name="au_id" Type="String" />
58 <asp:Parameter Name="au_lname" Type="String" />
59 <asp:Parameter Name="au_fname" Type="String" />
60 <asp:Parameter Name="phone" Type="String" />
61 <asp:Parameter Name="address" Type="String" />
62 <asp:Parameter Name="city" Type="String" />
63 <asp:Parameter Name="state" Type="String" />
64 <asp:Parameter Name="zip" Type="String" />
65 <asp:Parameter Name="contract" Type="Boolean" />
66 </InsertParameters>
67 </asp:SqlDataSource>
68 <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
69 BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
70 CellPadding="3" DataKeyNames="au_id" DataSourceID="SqlDataSource2" ForeColor="Black"
71 GridLines="Vertical" Height="50px" Width="314px">
72 <FooterStyle BackColor="#CCCCCC" />
73 <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
74 <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
75 <Fields>
76 <asp:BoundField DataField="au_id" HeaderText="ID" ReadOnly="True" SortExpression="au_id" />
77 <asp:TemplateField HeaderText="姓名" SortExpression="au_lname">
78 <EditItemTemplate>
79 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_lname") %>' Width="69px"></asp:TextBox>
80 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("au_fname") %>' Width="86px"></asp:TextBox>
81 </EditItemTemplate>
82 <InsertItemTemplate>
83 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_lname") %>'></asp:TextBox>
84 </InsertItemTemplate>
85 <ItemTemplate>
86 <asp:Label ID="Label1" runat="server" Text='<%# Bind("au_lname") %>'></asp:Label>
87 <asp:Label ID="Label2" runat="server" Text='<%# Eval("au_fname") %>'></asp:Label>
88 </ItemTemplate>
89 </asp:TemplateField>
90 <asp:BoundField DataField="phone" HeaderText="电话" SortExpression="phone" />
91 <asp:BoundField DataField="address" HeaderText="地址" SortExpression="address" />
92 <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
93 <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
94 <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />
95 <asp:CheckBoxField DataField="contract" HeaderText="contract" SortExpression="contract" />
96 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
97 </Fields>
98 <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
99 <AlternatingRowStyle BackColor="#CCCCCC" />
100 </asp:DetailsView>
101 </form>
102</body>
103</html>
104
FormView 与 DataList 差不多,可以任意布局, 可以说随心所欲吧.^_^
DetailsView 主要是显示某一条记录的时候,显示详细数据.
下面有几个它的知识点:
1: 它的分页, 其实不是一页内多条记录的分页,而是一页内显示的就一条记录的相关字段,
确切的说是指 由同一个查询条件所得的 一些记录,比如:这里的查询条件是: state ,要将
GridView 的 DataKeyNames 设置为state, 这里它的默认值为: au_id
2: 在一列中按照需要组合显示多个数据字段
实现方法是: 先转换为模板列,数据模板中添加一个Label,再绑定数据,编辑模板中也要添加一个TextBox,然 后绑定数据,不用写处理代码,它自动可以更新的.
===================
前台页面 DetailsView.aspx 注意:这个事例没有写后台代码
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsView.aspx.cs" Inherits="DetailsView" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
13 SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [city], [state] FROM [authors]">
14 </asp:SqlDataSource>
15
16 </div>
17 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
18 BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
19 CellPadding="4" DataKeyNames="state" DataSourceID="SqlDataSource1" PageSize="5">
20 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
21 <Columns>
22 <asp:CommandField ShowSelectButton="True" />
23 <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
24 <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
25 <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
26 <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
27 <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
28 <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
29 </Columns>
30 <RowStyle BackColor="White" ForeColor="#330099" />
31 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
32 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
33 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
34 </asp:GridView>
35 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
36 DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id" InsertCommand="INSERT INTO [authors] ([au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract]) VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip, @contract)"
37 SelectCommand="SELECT * FROM [authors] WHERE ([state] = @state)" UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zip] = @zip, [contract] = @contract WHERE [au_id] = @au_id">
38 <DeleteParameters>
39 <asp:Parameter Name="au_id" Type="String" />
40 </DeleteParameters>
41 <UpdateParameters>
42 <asp:Parameter Name="au_lname" Type="String" />
43 <asp:Parameter Name="au_fname" Type="String" />
44 <asp:Parameter Name="phone" Type="String" />
45 <asp:Parameter Name="address" Type="String" />
46 <asp:Parameter Name="city" Type="String" />
47 <asp:Parameter Name="state" Type="String" />
48 <asp:Parameter Name="zip" Type="String" />
49 <asp:Parameter Name="contract" Type="Boolean" />
50 <asp:Parameter Name="au_id" Type="String" />
51 </UpdateParameters>
52 <SelectParameters>
53 <asp:ControlParameter ControlID="GridView1" Name="state" PropertyName="SelectedValue"
54 Type="String" />
55 </SelectParameters>
56 <InsertParameters>
57 <asp:Parameter Name="au_id" Type="String" />
58 <asp:Parameter Name="au_lname" Type="String" />
59 <asp:Parameter Name="au_fname" Type="String" />
60 <asp:Parameter Name="phone" Type="String" />
61 <asp:Parameter Name="address" Type="String" />
62 <asp:Parameter Name="city" Type="String" />
63 <asp:Parameter Name="state" Type="String" />
64 <asp:Parameter Name="zip" Type="String" />
65 <asp:Parameter Name="contract" Type="Boolean" />
66 </InsertParameters>
67 </asp:SqlDataSource>
68 <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
69 BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
70 CellPadding="3" DataKeyNames="au_id" DataSourceID="SqlDataSource2" ForeColor="Black"
71 GridLines="Vertical" Height="50px" Width="314px">
72 <FooterStyle BackColor="#CCCCCC" />
73 <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
74 <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
75 <Fields>
76 <asp:BoundField DataField="au_id" HeaderText="ID" ReadOnly="True" SortExpression="au_id" />
77 <asp:TemplateField HeaderText="姓名" SortExpression="au_lname">
78 <EditItemTemplate>
79 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_lname") %>' Width="69px"></asp:TextBox>
80 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("au_fname") %>' Width="86px"></asp:TextBox>
81 </EditItemTemplate>
82 <InsertItemTemplate>
83 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_lname") %>'></asp:TextBox>
84 </InsertItemTemplate>
85 <ItemTemplate>
86 <asp:Label ID="Label1" runat="server" Text='<%# Bind("au_lname") %>'></asp:Label>
87 <asp:Label ID="Label2" runat="server" Text='<%# Eval("au_fname") %>'></asp:Label>
88 </ItemTemplate>
89 </asp:TemplateField>
90 <asp:BoundField DataField="phone" HeaderText="电话" SortExpression="phone" />
91 <asp:BoundField DataField="address" HeaderText="地址" SortExpression="address" />
92 <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
93 <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
94 <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />
95 <asp:CheckBoxField DataField="contract" HeaderText="contract" SortExpression="contract" />
96 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
97 </Fields>
98 <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
99 <AlternatingRowStyle BackColor="#CCCCCC" />
100 </asp:DetailsView>
101 </form>
102</body>
103</html>
104