views:

149

answers:

5

Much like when you are looking for support on a website or whatever, you choose Product from the first listbox (say, Hard Disk), which triggers and populates a second listbox with more options (say, "Solid State", "SATA"), and so on...

The problem I am having is that when you select something from the second listbox that should populate the third one, the postback triggers the first listbox too, which then repopulates the 2nd one back to its default value.

Eg.

[Dropdown 1] (contains A B C D E)

[DropDown 2] (A in dropdown 1 has options X Y Z)

[Dropdown 3] ...

If you choose A, then dropdown 2 populates with XYZ. You choose Z, and it should update dropdown3, but the postback also triggers dropdown 1 again, which replaces Dropdown 2's contents and resets the value back to X.

I am looking for an elegent solution. I had one that said only repopulate dropdown 2 if dropdown 1 has changed, but it means keeping track of what dropdown 1 was before the page posted back.

Each dropdown is in an updatepanel and set to autopostback=true, and each updatepanel has the previous listbox in its triggers.

A: 

I would simply put all the drop downs in the same update panel. It might not be as efficient, but it will probably bee good enough and certainly require less lines of code.

Jan Aagaard
Unfortunately in my case the drop downs are in totally different places on the page so I can't really do that unless I wrap pretty much the entire page in one.
SLC
Ah, okay. Then I would use Gabriel's answer.
Jan Aagaard
A: 

Why don't you try AjaxControl Toolkit CascadingDropDown. It seems to suit your needs.

Also, this will improve performance, as postback won't be sending all page information (as does with update panel).

MaLKaV_eS
It looks interesting, I am not just populating drop downs though, I am updating all kinds of information with the final dropdown (You can imagine the last dropdown in my example would populate another part with specific details like make, price etc.)It boils down to the fact that when I select something in a dropdown in an updatepanel, the updatepanel it is in triggers the load event.
SLC
Then maybe you could handle the last dropdown and use it as an async trigger for the up. That way you'll have only one update panel, and the drop downs will not be affected be the round trip to the server.
MaLKaV_eS
I might try it but it uses webservices, whereas I populate my lists in the code behind
SLC
A: 

I figured it out - I can use ScriptManager1.AsyncPostBackSourceElementID to check what caused the trigger, and make it refresh only for postback and the first dropdown box.

SLC
A: 

Maybe I'm thinking too simplified, but why wouldn't you just be handling all these events ... as events? Populate DDL1 only on initial Page_Load. Populate DDL2 only on DDL1.SelectedIndexChanged. Populate DDL3 only on DDL2.SelectedIndexChanged...

JustLoren
+2  A: 

First Check if the second TextBox isn't a trigger to the first UpdatePanel, if it is then remove it from the first update panel's Triggers Collection.

Here's the Key Concept:

If you have two separate tags you should place a trigger tag in the second update panel and insert a AsyncPostBackTrigger with the controlID of the first dropdownlist, here's a simple example:

<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="TxtBox1" />
</ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="TxtBox2" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TxtBox1" />
</Triggers>
</asp:UpdatePanel>
Gabriel Guimarães