[問題] POSTBACK更新DropDownList的問題

看板C_Sharp作者 (Second)時間4年前 (2019/07/24 10:02), 編輯推噓2(202)
留言4則, 2人參與, 4年前最新討論串1/1
Hi 各位前輩好 我在datasource Insert資料道資料庫後,Postback更新DropdownList資料的時候遇到了無 法更新的問題,煩請各位高手指導 首先,我在aspx定義了一個DataSource與一個DropDownList <asp:DropDownList ID="dlLot" runat="server" AutoPostBack="True"></asp:DropDownList> <asp:SqlDataSource ID="dsLot" runat="server" ConnectionString="<%$ ConnectionStrings:MY_SYS %>" SelectCommand= "SELECT distinct Lot FROM DEFINITION where 1=1 " InsertCommand="INSERT INTO DEFINITION (Lot) VALUES (@Lot) " OnInserted="dsLot_Inserted" > <InsertParameters> <asp:Parameter Type="String" Name="Lot" DefaultValue="" /> </InsertParameters> </asp:SqlDataSource> 在DataSource我定義了一個dsLot_Inserted function , 資料庫正確Insert資料後也的確 有進入這個函式 且程式也正確執行無任何錯誤 protected void dsLot_Inserted(object sender, SqlDataSourceStatusEventArgs e) { dlLot.DataValueField = "Lot"; dlLot.DataTextField = "Lot"; dlLot.DataSource = dsLot; dlLot.DataBind(); } 但DropDownList上的資料並沒有更新,但我在資料庫看資料在執行dlLot.DataSource = dsLot; 前就已經成功新增了 感覺有點像是POSTBACK執行元件更新之後才來執行dsLot_Inserted 我試著新增了一個 button和一個click事件做了一樣的事情, 執行完dsLot_Inserted去按 一下 , DropDownList dlLot就更新資料了....請教各位前輩要如何解決這個問題 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/> protected void Button1_Click(object sender, EventArgs e) { dlLot.DataValueField = "Lot"; dlLot.DataTextField = "Lot"; dlLot.DataSource = dsLot; dlLot.DataBind(); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.191.52 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1563933759.A.7D1.html

07/24 16:16, 4年前 , 1F
你是希望資料庫有新資料這下拉選單自動就更新?
07/24 16:16, 1F

07/29 00:48, 4年前 , 2F
這比較像初學......一般不會這樣做 你可以模擬listit
07/29 00:48, 2F

07/29 00:49, 4年前 , 3F
Listitem加入dropdownlist裡面
07/29 00:49, 3F

07/29 00:50, 4年前 , 4F
不然就是有個按鈕 按下去 更新畫面的下拉選單清單
07/29 00:50, 4F
文章代碼(AID): #1TDxm_VH (C_Sharp)