views:

229

answers:

1

I'm going to pre-populate my drop down lists on the server side. I only want the cascading drop-down to fire if the parent control changes.

A: 

Populate your parent ListBox from the code-behind, and set its "autopostback" attribute to true. Set OnSelectedIndexChanged="PopulateChildListBox"

Put the child ListBox in an UpdatePanel, and associate the parentListBox with the Updatepanel

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="lbParent" />
        </Triggers>
        <ContentTemplate>
            <asp:ListBox ID="lbChild" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>

In your code-behind, have the "PopulateChildListBox" method fill the child ListBox

protected void PopulateChildListBox(object sender, EventArgs e)
{
    // Get the data for the child listbox
    lbChildListBox.DataBind();
}

So, your child listbox only ever updates its content (via Asp.Net AJAX) when the parent ListBox changes.

Rafe Lavelle
Hmn... I had hoped there would be a less complicated way to do it.
Larsenal
Well, you need to put at least the child ListBox in an UpdatePanel, that's a given. You could put the parent ListBox in there too, but that would be pointless, and indeed wasteful to redraw it on postback. So that means you have to associate it with the UpdatePanel, hence the Triggers section. And you need to populate the child ListBox in the codebehind, as you said, hence 'PopulateChildListBox()'. So I can't see how much easier you could make this. Besides - none of this is in the least bit complicated :-)
Rafe Lavelle

related questions