views:

2186

answers:

2

I think this is an asp.net page life cycle question, but none the less, i can't quite figure it out.

I have a drop down list in an update panel that lists users and then shows details below it (update or delete). When the I click delete, the code behind goes and clears the ddl (to remove the deleted user), and then rebinds it. All fine. Somewhere between the end of the code behind and when the page updates, it then appends the list again.

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>

Note: srcUsers is a object data source

If I remove the ddl binding code from the button, then it doesn't update the ddl at all (with it in, it does it twice!)

Where is it binding the second time? Why isn't it binding if I remove the button code? How come I can't step through this in VS?

A: 

Check the code below if it can help you.

 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 ------//
    }
Himadri
Unless i am missing something, this is what i have now. The problem i have is that after BindCombo fires (it populates 20 items to the ddl) and the page goes thru it's lifecycle, it somewhere adds another 20 items (for 40).DDL.Items.Clear(); did not change this.I really want to know how to use the VS debugger better to find were it is adding adding the items the second time.
Plumbee
In my exampleple I have removed DropDownList1.Items.Clear(); from BindCombo() and replaced bindcombo(), delete statements only with DropDownList1.Items.RemoveAt(DropDownList1.SelectedIndex); and check the code. The combo doesn't contains data twice. If combo is populated with 20 records it always remains 20 records or less(after deleting some record by DropDownList1.Items.RemoveAt(DropDownList1.SelectedIndex);). Please check again. Thanks
Himadri
I really don't think it has anything to do with binding... i think it has to do with it posting back in the update panel. My struggle was in debugging. If the code is in a aspx page, it runs fine. Nest it in a update panel and somewhere, i am getting an extra binding... i just can't debug where. Items.remove(i); solved the immediate problem. Thanks for your time, Tim
Plumbee
In my post I have checked the code which I have posted, The dropdownlist is bound only one time. The binding has to be under if(!IsPostback) so that the dropdown will be bound when the page first loaded. Now I don't find the dropdown is bound anywhere else. Without using !IsPostBack the dropdown will be bound extra on each postback. Thanks for ur comment. I will try more.
Himadri
A: 

Himadri's code got me thinking (thanks for the effort!!!!) that for delete operations, i really didn't need to rebind the dropdownlist, i have the SelectedValue.... so i can just delete it from the dropdownlist.

DropDownList1.Items.Remove(selectedValue);

Duh.

I have on the other half of the page code that does an insert type operation (hence the update panel) that was calling the same code (Himadri's BindCombo) and having the same issue, but i think i can find a similar solution.

I still would like to know how the VS debugger lets that happen.

Thanks again Himadri!

Plumbee