Best control to use to populate a N number of tablse with rows of data in Asp.net dynamically

Kalpana 291 Reputation points
2021-08-29T02:11:08.007+00:00

Hi

I have got a requirement where I have to generate a N number of tables based a list collection. In each of this tables, there would be a N number of rows.
The columns are fixed for the tables.
Which control is suitable for this purpose, I do not need any paging or sorting as the data would be sorted before it gets displayed.
I tried repeater control, but I am facing difficulty in creating the item template for it.
Are there any guidance that I can look up ?

Thank you.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,181 Reputation points Microsoft External Staff
    2021-08-30T07:03:16.24+00:00

    Hi @Kalpana ,
    I recommended that you use a repeater, it is used to display a repeated list of items.

    I tried repeater control, but I am facing difficulty in creating the item template for it.

    You said you encountered difficulties when using the repeater. Can you describe it in detail?
    Other controls of ASP.NET data binding Web server, such as Gridview, you can refer to the following article:
    https://learn.microsoft.com/en-us/previous-versions/aspnet/ms247243(v=vs.100)
    I made a demo using a repeater, you can refer to it:
    The code:
    127532-test1.png


    127498-test2.png
    The Result:
    127408-test.png

    Best regards,
    Lan Huang


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Kalpana 291 Reputation points
    2021-08-30T17:49:25.253+00:00

    HI Lan Huang

    Yes, I am proceeding with a repeater control, the repeater control is created at the code behind, say if I have a collection of lists, for every list a method is run which creates the control and subsequently displays the data.
    Here is the code : -

      List<List<Bottleneckv11.SelectAllPU_Result>> groupedList = pulist.ToList()
                       .GroupBy(u => u.ProcessTypeName)
                       .Select(grp => grp.ToList())
                       .ToList();
    
    
                for(int i = 0; i < groupedList.Count; i++)
                {
                    CreateDynamicRepeater(groupedList[i]);
                }
    
      internal void CreateDynamicRepeater(List<Bottleneckv11.SelectAllPU_Result> sublist)
            {
                Repeater rpt = new Repeater();
    
                ITemplate header = LoadTemplate("rpthdr.ascx");
                rpt.HeaderTemplate = header;
                //rpt.HeaderTemplate = header;
    
    
                TemplateBuilder itemTemplate = new TemplateBuilder();
    
                for (int i = 0; i< sublist.Count; i++)
                {
    
                    itemTemplate.AppendLiteralString("<tr>");
                    itemTemplate.AppendLiteralString("<td>" + sublist[i].ProcessUnitID + "</td>");
                    itemTemplate.AppendLiteralString("<td>" + sublist[i].ProcessUnitName + "</td>");
                    itemTemplate.AppendLiteralString("<td>" + sublist[i].JobOrderName + "</td>");
                    itemTemplate.AppendLiteralString("<td>" + i + "</td>");
                    itemTemplate.AppendLiteralString("<td>" + sublist[i].Result + "</td>");
                    itemTemplate.AppendLiteralString("<td>" + sublist[i].TotalAvailable + "</td>");
                    itemTemplate.AppendLiteralString("</tr>");
    
                }
    
                rpt.ItemTemplate = itemTemplate;
    
    
                TemplateBuilder footer = new TemplateBuilder();
                footer.AppendLiteralString("<tr><td>End of Table</td></tr></table>");
                rpt.FooterTemplate = footer;
    
    
                rpt.DataSource = sublist;
                rpt.DataBind();
                this.Controls.Add(rpt);
    
    
            }
    

    Right now, the data is displayed but it is repeated and this is how it looks now. I do not want the data to be repeated. It gets repeated based on the count of list. I think I am doing something wrong with the template builder. Please see if you could help.


  3. AgaveJoe 30,041 Reputation points
    2021-08-30T18:53:05.54+00:00

    Again, I would use a GridView.

     public partial class _default : System.Web.UI.Page  
        {  
            public class ViewModelRecord  
            {  
                public int Id { get; set; }  
                public string Name { get; set; }  
            }  
            protected void Page_Load(object sender, EventArgs e)  
            {  
                PopulateGridViews();  
            }  
      
            protected void PopulateGridViews()  
            {  
                List<List<ViewModelRecord>> tables = PopluateViewModelCollections();  
                int i = 0;  
                foreach(var table in tables)  
                {  
                    GridView gv = new GridView();  
                    gv.ID = $"table{i++}";  
                    gv.DataSource = table;  
                    GridViewPlaceHolder.Controls.Add(gv);  
                    gv.DataBind();  
                }  
            }  
      
            protected List<List<ViewModelRecord>> PopluateViewModelCollections()  
            {  
                List<List<ViewModelRecord>> tables = new List<List<ViewModelRecord>>();  
                for (int i = 0; i < 5; i++)  
                {  
                    List<ViewModelRecord> table = new List<ViewModelRecord>();  
                    for (int j = 0; j < 5; j++)  
                    {  
                        table.Add(new ViewModelRecord() { Id = j, Name = $"item{i}-{j}" });      
                    }  
                    tables.Add(table);  
                }  
      
                return tables;  
            }  
        }  
    

    Markup

        <form id="form1" runat="server">  
            <div>  
                <asp:PlaceHolder ID="GridViewPlaceHolder" runat="server"></asp:PlaceHolder>  
            </div>  
        </form>  
    

    127686-capture.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.