我认为这是一个 asp.net 页面生命周期问题,但仍然无法弄清楚。
我在更新面板中有一个下拉列表,其中列出了用户,然后在其下方显示详细信息(更新或删除)。当我单击删除时,后面的代码会清除 ddl(以删除已删除的用户),然后重新绑定它。一切都好。在后面的代码结束和页面更新之间的某个地方,它会再次附加列表。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.AppendDataBoundItems = true;
DropDownList1.Items.Insert(0, new ListItem("Select a User", "select"));
DropDownList1.SelectedValue = "select";
DropDownList1.DataSourceID = "srcUsers";
DropDownList1.DataBind();
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
DropDownList1.AppendDataBoundItems = false;
DropDownList1.DataSourceID = "srcUsers";
DropDownList1.DataBind();
DropDownList1.AppendDataBoundItems = true;
DropDownList1.Items.Insert(0, new ListItem("Select a User", "select"));
DropDownList1.SelectedValue = "select";
}//at this point, the quick watch shows the correct count for the ddl
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" Width="150" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
注:srcUsers
是对象数据源
如果我从按钮中删除 ddl 绑定代码,那么它根本不会更新 ddl(在它里面,它会执行两次!)
第二次绑定在哪里?如果我删除按钮代码,为什么它不绑定?为什么我无法在 VS 中逐步完成?