How can i get DataSource property & Databind method of HTML Table.
Do i have to make a new control for it? if yes then how can i do this.

Dani AI

Generated

Short answer: an HtmlTable does not provide a DataSource property the way WebForms data controls do, so you either bind it yourself or use a lightweight data control. ’s point about using a data-bound control is valid; was right that you can’t just “grab” a DataSource off an HtmlTable. For ’s scenario (100 tables), avoid rendering 100 heavy GridViews at once — consider paging, lazy loading into placeholders, client-side rendering, or a single aggregated table.

A quick, practical approach is a small server-side binder you call after fetching the IEnumerable (no custom control required):

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI.HtmlControls;

public static class HtmlTableBinder
{
    public static void Bind(HtmlTable table, IEnumerable data, bool includeHeader = true)
    {
        if (table == null) throw new ArgumentNullException("table");
        table.Rows.Clear();
        var items = new List<object>();
        foreach (var x in data) items.Add(x);
        if (items.Count == 0) return;

        var props = items[0].GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        if (includeHeader)
        {
            var header = new HtmlTableRow();
            foreach (var p in props)
            {
                var th = new HtmlTableCell { InnerText = p.Name };
                header.Cells.Add(th);
            }
            table.Rows.Add(header);
        }

        foreach (var item in items)
        {
            var row = new HtmlTableRow();
            foreach (var p in props)
            {
                var cell = new HtmlTableCell { InnerText = (p.GetValue(item, null) ?? String.Empty).ToString() };
                row.Cells.Add(cell);
            }
            table.Rows.Add(row);
        }
    }
}

If you want a reusable server control with DataSource and DataBind, create a small custom control that extends HtmlTable, exposes an object DataSource property and overrides DataBind() to populate rows (bind in PreRender or when you explicitly call DataBind). Note: DataSource is not persisted across postbacks by default — either reassign it each request or store minimal state (IDs/page) in ViewState/Session.

Performance tips: do not output 100 full tables on initial page render. Use paging, placeholders + dynamic loading, or client-side templates (JSON + JS) to keep both server CPU and browser memory reasonable.

Recommended Answers

All 4 Replies

Any particular reason you're using an HTML table?

Why don't you use a gridview to display the results from a datasource?

>How can i get DataSource property & Databind method of HTML Table.

No!

Hey CrappyCoder thanx for advice.
But I have to bind more than 100 grids & it will create so much load on page as Gridview control is a heavy control.
So i am thinking of light control and that is HTML table.

Bind more than 100 Grids - Are you displaying these all simultaneously (which would be highly inadvisable and downright madness)

Why not use a couple of placeholders and load the datagrid dynamically as and when the need arises.

Better still, let us know what you're trying to achieve and we may be able to point you in the right direction

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.