[習題] FindControl 簡單練習--FormView/DetailsView,自己修改樣板裡面的控制項後,資料無法新增?

我不太有把握,不清楚怎麼形容這個問題。

有一個網友發問了這個問題----
他在 FormView與 DetailsView裡面要新增資料,
但預設都是 TextBox控制項,

他打算把幾個欄位做成 DropDownList,直接從資料表裡面抓資料,避免使用者自己輸入文字,而造成錯誤。
但發現了一些問題





我不太有把握,不清楚怎麼形容這個問題。

 

有一個網友發問了這個問題----

他在 FormView與 DetailsView裡面要新增資料,

但預設都是 TextBox控制項,

他打算把幾個欄位做成 DropDownList,直接從資料表裡面抓資料,避免使用者自己輸入文字,而造成錯誤。

但發現了一些問題:

 

我用 Northwind資料庫的 Order資料表來解說好了。

我要新增一筆訂單,客戶資料、員工編號、船運公司這些資料,不能放任 User自由輸入文字(很容易輸入錯誤)

必須從相關的資料表去撈取資料。

於是他在 新增資料的樣版(InsertItemTemplate)裡面,

把這三個欄位改成 DropDownList並且搭配SqlDataSource

從其他資料表抓取資料。

     

然後,他想要採用我這個方法 -- ADO.NET #5,自己設定畫面,讓SqlDataSource幫我們撈(呈現)資料 ...

讓 SqlDataSource幫他完成資料新增,可以少寫一點程式。

     

 

問題來了,

他在 Insert的指令裡面,發現自己修改的三個欄位(改用 DropDownList搭配 相關資料表)

卻無法點選......請看下圖。

     

 

=================================================================================

這情況的確很難形容,所以雞同鴨講了半天。

我只能說:「不要過份地依賴開發工具那些精靈」

  • 做得到,是我們賺到。算他厲害!
  • 做不到也是應該的。   工具做不到,大不了自己寫程式來解決,有什麼好大驚小怪的?

 

如果工具提供的精靈都能做到,那麼會寫程式的人,可能跟會用 Word的人,都是一樣的薪資。

(只是打比喻。  我 "不是"看不起 Word這套軟體,我知道它功能很強,80%的功能或許我自己也不善用)

===========================================================================

我自己的解決方法有兩種:

 

第一,既然要自己設計「資料新增」的畫面,那幹嘛還用現成的 FormView與 DetailsView呢?

      沒事搞一些「混搭」、「混用」,往往會有更多怪異情況。

      以我這個例子來說(ADO.NET #5,自己設定畫面,讓SqlDataSource幫我們撈(呈現)資料 ...

      畫面上全都是用最基礎的控制項來做, TextBox / Button /DropDownList.... 問題就解了。

 

 

第二,要堅持這名網友原本的作法(上面的「混搭」作法)也行。

      但要學會 FindControl()方法,然後自己寫後置程式碼來做。請看下面文章的說明:

      [習題]GridView裡面,樣版(Template)內的控制項,怎麼抓取?使用FindControl就對啦~

 

      既然您堅持要用 FormView,那麼各樣版裡面的「子控制項」,您就要有能力去抓住他。

      抓住他以後,就能控制他。       程式請自己參考,都是重複的。

        當你抓到 FormView裡面的某一個「DropDownList控制項以後」

        Dim myCustomerID As DropDownList = FormView1.FindControl("DropDownList1_CustomerID")

        你就能使用它的屬性啦!很簡單!
        SqlDataSource1.InsertParameters.Add("CustomerID", myCustomerID.SelectedValue)

 

01 Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
02         Dim SqlDataSource1 As New SqlDataSource
03         SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
04
05         '*****************************************************************
06         '***  透過「參數」的方法,來做 SqlDataSource資料新增。
07         '***
08         Dim myCustomerID As DropDownList = FormView1.FindControl("DropDownList1_CustomerID")
09         '== FindControl -- 抓取大型控制項的「樣版(Template)裡面」的子控制項 ==
10         SqlDataSource1.InsertParameters.Add("CustomerID", myCustomerID.SelectedValue)
11
12         Dim myEmployeeID As DropDownList = FormView1.FindControl("DropDownList2_EmployeeID")
13         SqlDataSource1.InsertParameters.Add("EmployeeID", myEmployeeID.SelectedValue)
14
15         Dim myOrderDate As TextBox = FormView1.FindControl("OrderDateTextBox")
16         SqlDataSource1.InsertParameters.Add("OrderDate", myOrderDate.Text)
17
18         Dim myRequiredDate As TextBox = FormView1.FindControl("RequiredDateTextBox")
19         SqlDataSource1.InsertParameters.Add("RequiredDate", myRequiredDate.Text)
20
21         Dim myShippedDate As TextBox = FormView1.FindControl("ShippedDateTextBox")
22         SqlDataSource1.InsertParameters.Add("ShippedDate", myShippedDate.Text)
23
24         Dim myShipVia As DropDownList = FormView1.FindControl("DropDownList3_ShipVia")
25         SqlDataSource1.InsertParameters.Add("ShipVia", myShipVia.SelectedValue)
26
27         Dim myFreight As TextBox = FormView1.FindControl("FreightTextBox")
28         SqlDataSource1.InsertParameters.Add("Freight", myFreight.Text)
29
30         Dim myShipName As TextBox = FormView1.FindControl("ShipNameTextBox")
31         SqlDataSource1.InsertParameters.Add("ShipName", myShipName.Text)
32
33         Dim myShipAddress As TextBox = FormView1.FindControl("ShipAddressTextBox")
34         SqlDataSource1.InsertParameters.Add("ShipAddress", myShipAddress.Text)
35
36         Dim myShipCity As TextBox = FormView1.FindControl("ShipCityTextBox")
37         SqlDataSource1.InsertParameters.Add("ShipCity", myShipCity.Text)
38
39         Dim myShipRegion As TextBox = FormView1.FindControl("ShipRegionTextBox")
40         SqlDataSource1.InsertParameters.Add("ShipRegion", myShipRegion.Text)
41
42         Dim myShipPostalCode As TextBox = FormView1.FindControl("ShipPostalCodeTextBox")
43         SqlDataSource1.InsertParameters.Add("ShipPostalCode", myShipPostalCode.Text)
44
45         Dim myShipCountry As TextBox = FormView1.FindControl("ShipCountryTextBox")
46         SqlDataSource1.InsertParameters.Add("ShipCountry", myShipCountry.Text)
47         '*****************************************************************
48         SqlDataSource1.InsertCommand = "INSERT INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight], [ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry]) VALUES (@CustomerID, @EmployeeID, @OrderDate, @RequiredDate, @ShippedDate, @ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry)"
49
50         Dim affectRows As Integer = SqlDataSource1.Insert()  '--執行 Insert()的動作。
51         If affectRows > 0 Then
52             Response.Write("<h2>資料新增,成功!!</h2>")
53         End If
54
55 End Sub

 

 

 

或許我要自己寫程式來解,不是好方法。

說不定真的有更好的方法,可以拖拉一下,從設定上著手就能搞定。

 

但我還是建議:寫程式還是要多依賴 "鍵盤" 打字、思考。

只會用 "滑鼠" 拖拉現成的控制項,那只是騙騙初學者入門而已。

一般人可以這樣學,

但要靠"寫程式過生活"的話,別想依賴這一招過一輩子。

 

 

 

 

 

 

 

 

 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

線上課程教學,遠距教學 (Web Form 約 51hr)  https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程教學,遠距教學 (ASP.NET MVC 約 135hr)  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.主講   事先錄好的影片,並非上課側錄!   觀看時,有如「一對一」面對面講課