2009-10-14 85 views

回答

0

從代碼隱藏中填充父代ListBox,並將其「autopostback」屬性設置爲true。設置OnSelectedIndexChanged="PopulateChildListBox"

把孩子列表框在一個UpdatePanel,和parentListBox與UpdatePanel的

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

相關聯。在您的代碼隱藏,有 「PopulateChildListBox」 方法填補了孩子的ListBox

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

因此,當父列表框更改時,您的子列表框只會更新其內容(通過Asp.Net AJAX)。

+0

Hmn ......我原本希望能有一種不那麼複雜的方式來做到這一點。 – Larsenal 2009-10-19 20:44:21

+0

那麼,你需要在UpdatePanel中至少放置子ListBox,這是給定的。你也可以把父列表框放在那裏,但是這將毫無意義,而且在回發時重繪它確實很浪費。所以這意味着你必須將它與UpdatePanel關聯,因此觸發器部分。如你所說,你需要在代碼隱藏中填充子ListBox,因此'PopulateChildListBox()'。所以我看不出有多容易做到這一點。此外 - 這些都沒有一點複雜:-) – 2009-10-19 23:17:46