0

我认为这是一个 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 中逐步完成?

4

2 回答 2

1

Himadri 的代码让我想到(感谢您的努力!!!!)对于删除操作,我真的不需要重新绑定下拉列表,我有 SelectedValue .... 所以我可以从下拉列表中删除它。

DropDownList1.Items.Remove(selectedValue);

呃。

我在页面代码的另一半上执行插入类型操作(因此是更新面板),它调用相同的代码(Himadri 的 BindCombo)并遇到相同的问题,但我想我可以找到类似的解决方案。

我仍然想知道 VS 调试器是如何让这种情况发生的。

再次感谢希马德里!

于 2009-09-20T02:32:32.957 回答
0

如果它可以帮助你,请检查下面的代码。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCombo();

        }
    }
    public void BindCombo()
    {
        DropDownList1.Items.Clear();
        DropDownList1.AppendDataBoundItems = true;
        DropDownList1.Items.Insert(0, new ListItem("Select a Category", "select"));
        DropDownList1.SelectedValue = "select";
        DropDownList1.DataSourceID = "ds1";
        DropDownList1.DataBind();

    }

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //---- It can be placed in delete button's click event also ------//
        ds1.DeleteCommand = "Delete from Categories where CategoryID="+DropDownList1.SelectedValue;
        ds1.Delete();
        BindCombo();
        //---- It can be placed in delete button's click event also ------//
    }
于 2009-09-19T08:53:29.740 回答