作者ashin42 (Second)
看板C_Sharp
标题[问题] POSTBACK更新DropDownList的问题
时间Wed Jul 24 10:02:37 2019
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://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1563933759.A.7D1.html
1F:推 ian90911: 你是希望资料库有新资料这下拉选单自动就更新? 07/24 16:16
2F:推 mepowerlmay: 这比较像初学......一般不会这样做 你可以模拟listit 07/29 00:48
3F:→ mepowerlmay: Listitem加入dropdownlist里面 07/29 00:49
4F:→ mepowerlmay: 不然就是有个按钮 按下去 更新画面的下拉选单清单 07/29 00:50