[問題] POSTBACK更新DropDownList的問題

作者: ashin42 (Rock'n roll style)   2019-07-24 10:02:37
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();
}
作者: ian90911 (xopowo)   2019-07-24 16:16:00
你是希望資料庫有新資料這下拉選單自動就更新?
作者: mepowerlmay (用心,找對人)   2019-07-29 00:48:00
這比較像初學......一般不會這樣做 你可以模擬listitListitem加入dropdownlist裡面不然就是有個按鈕 按下去 更新畫面的下拉選單清單

Links booklink

Contact Us: admin [ a t ] ucptt.com