摘要:ADO.NET #9 FormView + SqlDataSource完全手寫、後置程式碼!
我的網站上有一篇文章 -- ADO.NET #3 (GridView + SqlDataSource)完全手寫、後置程式碼!
這篇文章有助於初學者,徹底瞭解 GridView背後是怎麼進行編輯、更新等等的動作。
但難度有點高..............
這篇文章是改用 FormView來作,
1). 原本我以為大同小異,但是在「改變畫面(模式)」的時候,我發現原本的作法不能運作。
以 GridView為例,只要把 Button的 CommandName設定為 Edit。一按下去就能進去編輯模式
但在 FormView我則作不到,
只好強制寫程式碼 -- FormView1.ChangeMode(FormViewMode.Edit) 來完成之。
2). 也因此,這個程式的按鈕,統統不在 FormView「內部」而在 FormView的外部。可惜啊~
3). 我只示範資料更新(Update)的部份而已,並非全部功能都寫完了。
...........................................................................................................................................................
HTML的畫面:
02 <head runat="server">
03 <title>(正確版)</title>
04 </head>
05 <body>
06 <form id="form1" runat="server">
07 <div>
08 <br />
09 100% 自己動手寫程式
10 <hr />
11 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
12 AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
13 DataSourceID="SqlDataSource1" PageSize="5">
14 <Columns>
15 <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
16 <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
17 ReadOnly="True" SortExpression="id" />
18 <asp:BoundField DataField="test_time" HeaderText="test_time"
19 SortExpression="test_time" />
20 <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
21 </Columns>
22 </asp:GridView>
23 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
24 ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
25 SelectCommand="SELECT [id], [test_time], [title] FROM [test]">
26 </asp:SqlDataSource>
27 <br />
28 <br />
29
30 <hr />
31 <br />
32 <asp:FormView ID="FormView1" runat="server" >
33 <EditItemTemplate>
34 文章編號:<asp:Label ID="Label_E_id" runat="server" Text='<%# Eval("id") %>'></asp:Label>
35 <br />
36 日期:<asp:TextBox ID="TextBox_E_Test_time" runat="server" Text='<%# Bind("test_time") %>'></asp:TextBox>
37 <br />
38 分類:<asp:TextBox ID="TextBox_E_class" runat="server" Text='<%# Bind("class") %>'></asp:TextBox>
39 <br />
40 <br />
41 標題:<asp:TextBox ID="TextBox_E_title" runat="server" Width="400px" Text='<%# Bind("title") %>'></asp:TextBox>
42 <br />
43 摘要:<asp:TextBox ID="TextBox_E_summary" runat="server" Height="50px" TextMode="MultiLine"
44 Width="400px" Text='<%# Bind("summary") %>'></asp:TextBox>
45 <br />
46 內容:<asp:TextBox ID="TextBox_E_article" runat="server" Height="200px"
47 TextMode="MultiLine" Width="400px" Text='<%# Bind("article") %>'></asp:TextBox>
48 <br />
49 作者:<asp:TextBox ID="TextBox_E_author" runat="server" Text='<%# Bind("author") %>'></asp:TextBox>
50 </EditItemTemplate>
51
52 <ItemTemplate>
53 文章編號:<asp:Label ID="Label_id" runat="server" Text='<%# Eval("id") %>'></asp:Label>
54 <br />
55 日期:<asp:Label ID="Label_test_time" runat="server" Text='<%# Eval("test_time") %>'></asp:Label>
56 <br />
57 分類:<asp:Label ID="Label_class" runat="server" Text='<%# Eval("class") %>'></asp:Label>
58 <br />
59 <br />
60 標題:<asp:Label ID="Label_title" runat="server" Text='<%# Eval("title") %>'></asp:Label>
61 <br />
62 摘要:<asp:Label ID="Label_summary" runat="server" Text='<%# Eval("summary") %>'></asp:Label>
63 <br />
64 內容:<asp:Label ID="Label_article" runat="server" Text='<%# Eval("article") %>'></asp:Label>
65 <br />
66 作者:<asp:Label ID="Label_author" runat="server" Text='<%# Eval("author") %>'></asp:Label>
67
68 <br />
69 </ItemTemplate>
70 </asp:FormView>
71 <hr />
72 <br />
73
74 <!-- '*** 自己設計 Button按鈕,修改 FormView的模式 ***************** -->
75 <asp:Button ID="ButtonEdit" runat="server" Text="Edit(編輯)" />
76 <asp:Button ID="ButtonUpdate" runat="server" Text="Update(更新)" visible=false/>
77 <asp:Button ID="ButtonCancle" runat="server" Text="Cancle(取消)" visible=false/>
78 </div>
79 </form>
80 </body>
81 </html>
千萬不要為SqlDataSource "動手" 寫程式
本文的範例,我只是寫給大家看,SqlDataSource其實骨子裡面就是「ADO.NET」
寫法與流程都一樣。
您千萬不要為了「SqlDataSource精靈」寫程式。讓他來服務你,你不用服務他!你才是主人!!!!
Code Behind 後置程式碼:
002

003

004

005

006

007 '----自己寫的----
008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

除了基本的 ADO.NET程式以外,
大概就是 FindControl()的用法,是初學者比較不清楚的,請看我幫各位寫好的範例:http://www.dotblogs.com.tw/mis2000lab/Tags/FindControl/default.aspx
===========================================================================
目前的書(ASP.NET專題實務 /文魁出版)只列出自己動手寫程式 100%控制 GridView的範例,
有讀者希望看見 ListView、FormView、DetailsView,也都可以自己動手打造。
到了這篇文章,算是把這四大天王的範例,都提供出來了。
ListView ----
FormView ----
GridView --
DetailsView --
===========================================================================
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson
線上課程教學,遠距教學 (Web Form 約 51hr) https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015
線上課程教學,遠距教學 (ASP.NET MVC 約 140hr) https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab
寫信給我,不要私訊 -- mis2000lab (at) yahoo.com.tw 或 school (at) mis2000lab.net
(1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A
(2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I
[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm 。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b
ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。
......... facebook社團 https://www.facebook.com/mis2000lab ......................
......... YouTube (ASP.NET) 線上教學影片 https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/
Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download
請看我們的「售後服務」範圍(嚴格認定)。
......................................................................................................................................................
ASP.NET MVC => .NET Core MVC 線上教學 ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講 事先錄好的影片,並非上課側錄! 觀看時,有如「一對一」面對面講課。
