2013-05-08 109 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--; 
}