ASP.NET dynamically load AJAX toolkit accordion panes

I love the Accordion pane from the AJAX toolkit, but I've found myself needing to dynamically add accordion panes to it. To do this you need to some work on the back end. The following code loops through a DataSet and dynamically creates a Label control for the header and content. It then adds those controls to a new AccordionPane and then add it to your Accordion.

 for ( var i = 0; i < ds.Tables[10].Rows.Count; i++ )  
           Label lblContent = new Label();  
           lblContent.ID = Guid.NewGuid().ToString();  
           Label lblTitle = new Label();  
           lblTitle.ID = Guid.NewGuid().ToString();  
           lblTitle.Text = ds.Tables[2].Rows[i + 1][1].ToString();  
           lblContent.Text = ds.Tables[10].Rows[i][1].ToString();  
           AjaxControlToolkit.AccordionPane pane = new AjaxControlToolkit.AccordionPane();  
           pane.ID = Guid.NewGuid().ToString();  
           pane.HeaderContainer.Controls.Add( lblTitle );  
           pane.ContentContainer.Controls.Add( lblContent );  
           this.Accordion1.Panes.Add( pane );  


Popular Posts