2013-05-08 81 views
0

所以我幾乎有這個工作。我基於用戶選擇創建動態列表框。總是當用戶第一次加載頁面時,將只有一個顯示頂級類別的ListBox(沒有父級的類別)。我有與子類別的類別。這可以是許多子類,像這樣asp.net重新創建動態添加回傳列表框控件

貓1

貓2

  • 貓2.1

  • 貓2.2

    -- Cat 2.2.1 
    
         --- Cat 2.2.1.1 
    

等。

我遇到的問題是清除列表框,如果用戶從已顯示的列表框中選擇一個值。因此,如果顯示4個列表框,並且用戶從第一個列表框中獲得一個新值,該列表框顯示沒有父項的頂級眼淚類別,則所有列表框應該消失,並且應顯示新列表框。如果有4個列表框,並且用戶點擊ListbOx 3中的新項目,則第4個應該將子分類重新渲染到其選定的父項目。我希望我正確地解釋自己。

這是我的代碼迄今: 公共部分類WebForm2:System.Web.UI.Page { 私人的Int32 controlCount = 0; Panel _panel;

private Panel PanelPlaceholder 
    { 
     get 
     { 
      if (_panel == null && Master != null) 
       _panel = pnlContainer; 
      return _panel; 
     } 
    } 

    protected void Page_PreInit(Object sender, EventArgs e) 
    { 
     this.EnsureChildControls(); 

     if (IsPostBack) 
     { 
      // Re-create controls but not from datasource 
      // The controlCount value is output in the page as a hidden field during PreRender. 
      controlCount = Int32.Parse(Request.Form["controlCount"]); // assigns control count from persistence medium (hidden field) 
      for (Int32 i = 0; i < controlCount; i++) 
      { 
       CreateDynamicControlGroup(false); 
      } 
     } 
    } 
    protected void Page_Load(Object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 
      int cc = controlCount; 

      DataTable dt = null; 
      Dictionary<string, string> Params = new Dictionary<string, string>(); 
      dt = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetMainCategories, Params); 

      CreateDynamicControlGroup(true); 

      ListBox lb = (ListBox)PanelPlaceholder.Controls[controlCount - 1]; 

      lb.DataSource = dt; 
      lb.DataValueField = "ID"; 
      lb.DataTextField = "Name"; 
      lb.DataBind(); 
     } 
    } 


    protected void Page_PreRender(Object sender, EventArgs e) 
    { 
     // persist control count 
     ClientScript.RegisterHiddenField("controlCount", controlCount.ToString()); 
    } 


    private void ListBox_SelectedIndexChanged(Object sender, EventArgs e) 
    { 
     ListBox lb = sender as ListBox; 


     Dictionary<string, string> Params = new Dictionary<string, string>(); 
     Params.Add("parentID", lb.SelectedValue); 
     DataTable Categories = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetChildCategories, Params); 

     if (Categories.Rows.Count > 0) 
     { 
      CreateDynamicControlGroup(true); 

      ListBox newLb = (ListBox)PanelPlaceholder.Controls[controlCount - 1]; 

      newLb.DataSource = Categories; // use the same table 
      newLb.DataValueField = "ID"; 
      newLb.DataTextField = "Name"; 
      newLb.DataBind(); 
     } 
    } 


    private void CreateDynamicControlGroup(Boolean incrementCounter) 
    { 
     // Create one logical set of controls do not assign values! 
     ListBox lb = new ListBox(); 
     lb.AutoPostBack = true; 
     lb.CssClass = "panel"; 
     PanelPlaceholder.Controls.Add(lb); 

     // wire event delegate 
     lb.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged); 

     if (incrementCounter) 
     { 
      controlCount += 1; 
     } 
    } 
} 

這裏是我的標記:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<div class="Column12" id="Form_NewListing"> 
    <h2 class="h2row">Create Your Listing - Step 1 of 2)</h2> 
    <h3 class="h3row">Select a category</h3> 
    <div class="panel"> 
     <asp:Panel ID="pnlContainer" runat="server"></asp:Panel>   

    </div> 
</div> 

在此先感謝。

回答

0

有關添加

int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender); 
for (int i = index + 1; i < PanelPlaceholder.Controls.Count; i++) 
    PanelPlaceholder.Controls.RemoveAt(index + 1); 

ListBox_SelectedIndexChanged方法的開始呢?

+0

關閉幾個小調整它似乎工作。與您的代碼段相關的問題是,如果其他列表框存在更高的索引,則一旦刪除,索引超出範圍就會發生。 – 2013-05-09 00:30:25

+0

我編輯了答案。 – filipko 2013-05-09 03:53:38

0
int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender); 
for (int i = PanelPlaceholder.Controls.Count - 1; i > index; i--) 
{ 
    PanelPlaceholder.Controls.RemoveAt(i); 
    controlCount--; 
}