先靜態加入一個子選項,
後續再從資料庫裡面撈資料,補充到 DropDownList裡面當成新的子選項。
相關的習題,可以參考以前的文章:
1) http://www.dotblogs.com.tw/mis2000lab/archive/2008/10/09/dropdownlist_items.add081009.aspx
2) http://www.dotblogs.com.tw/mis2000lab/archive/2008/12/01/listbox.item_add_remove.aspx
也可以參考 ASP.NET專題實務(文魁出版)的上冊,裡面的第三章 DropDownList。
===================================
有讀者詢問:
他想先靜態加入一個子選項,
後續再從資料庫裡面撈資料,補充到 DropDownList裡面當成新的子選項。
但發現一個問題 -- 每一次從資料庫撈出資料後,動態加入的子選項,
會覆蓋一開始 "靜態設定"的(自己動手寫的)子選項
執行畫面與步驟如下 :
------------------------------------------------------------------------------
如果我沒有會錯意的話,這題目並不難
(也可能我會錯意了)。
1. 首先,我們先幫 DropDownList動手加入一個靜態的子選項,例如:「歡迎挑選」這四個字
02 <form id="form1" runat="server">
03 <p>
04 <br />
05 <asp:Button ID="Button1" runat="server" Text="按下這個按鈕, 就會看見成果" />
06 </p>
07 <div>
08
09 <asp:DropDownList ID="DropDownList1" runat="server">
10 <asp:ListItem>~~~歡迎選擇~~~(自己動手寫入的、靜態加入)</asp:ListItem>
11 </asp:DropDownList>
12
13 </div>
14 </form>
15 </body>
2. 然後從資料庫裡面撈資料,以某一個欄位來當作(動態加入) DropDownList新的子選項
重點在於底下的 While dr.Read() ... End While迴圈
02

03

04

05

06

07 '----自己寫的----
08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

相關習題,請參考 [習題] DropDownList / ListBox連動,查詢菜單
今日值班正妹 , 李毓芬
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----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.主講 事先錄好的影片,並非上課側錄! 觀看時,有如「一對一」面對面講課。
