楼主:
ashin42 (Rock'n roll style)
2019-07-24 10:02:37Hi 各位前辈好
我在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();
}