Show list in html table with asp.net

0

I want to know if you can print the data from a list < > in a html table or if it is necessary to print them in a gridview

namespace tts
{
    public class tabla_conexion
    {
        public DataTable connect()
        {
            string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";

            DataTable SBTable = new DataTable();

            OleDbConnection connection = new OleDbConnection();
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataSet dataset = new DataSet();

            try
            {
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
                bool ok = System.IO.File.Exists(myConnectionString);
                String qry = "SELECT * FROM information_tool_gen";
                command.Connection = connection;
                command.CommandText = qry;

                adapter.SelectCommand = command;

                command.Connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                List<tabla> Resultado = new List<tabla>();

                while (reader.Read())
                {
                    Resultado.Add(new tabla(
                        int.Parse(reader["id_global"].ToString()),
                        int.Parse(reader["torque"].ToString()),
                        int.Parse(reader["tol"].ToString()),
                        reader["um"].ToString(),
                        reader["module"].ToString(),
                        reader["batteries"].ToString(),
                        reader["torquemeter"].ToString(),
                        int.Parse(reader["frequency"].ToString()),
                        DateTime.Parse(reader["maint_date"].ToString()),
                        int.Parse(reader["status_id"].ToString()),
                        DateTime.Parse(reader["reg_date"].ToString())
                        ));
                }
                if (!reader.IsClosed)
                {
                    reader.Close();
                }
                return SBTable;
            }
            catch (Exception)
            {
            }
            return SBTable;
        }
    }
}

I have a class where I do the get and the set of data

namespace tts
{
    public class tabla
    {
        public int ID_GLOBAL { get; set; }
        public int TORQUE { get; set; }
        public int TOL { get; set; }
        public String UM { get; set; }
        public String MODULE { get; set; }
        public String BATTERIES { get; set; }
        public String TORQUEMETER { get; set; }
        public int FREQUENCY { get; set; }
        public DateTime MAINT_DATE { get; set; }
        public int STATUS_ID { get; set; }
        public DateTime REG_DATE { get; set; }

        public tabla(
            int id_global,
            int torque,
            int tol,
            String um,
            String module,
            String batteries,
            String torquemeter,
            int frequency,
            DateTime maint_date,
            int status_id,
            DateTime reg_date
            )
        {
            this.ID_GLOBAL = id_global;
            this.TORQUE = torque;
            this.TOL = tol;
            this.UM = um;
            this.MODULE = module;
            this.BATTERIES = batteries;
            this.TORQUEMETER = torquemeter;
            this.FREQUENCY = frequency;
            this.MAINT_DATE = maint_date;
            this.STATUS_ID = status_id;
            this.REG_DATE = reg_date;
        }
        private tabla() { }
    }
}
    
asked by Cesar Gutierrez Davalos 03.05.2017 в 22:55
source

2 answers

1

In your case, I would use a Repeater control, something like the following:

<asp:Repeater ID="rptTable" runat="server" ItemType="tts.tabla">
    <HeaderTemplate>
        <table>
            <thead>
                <tr>
                    <th>Columna 1</th>
                    <th>Columna 2</th>
                    <th>Columna 3</th>
                    <th>Columna 4</th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
                <tr>
                    <td><%# Item.Columna1 %></td>
                    <td><%# Item.Columna2 %></td>
                    <td><%# Item.Columna3 %></td>
                    <td><%# Item.Columna4 %></td>
                </tr>
    </ItemTemplate>
    <FooterTemplate>
            </tbody>
        </table>
    </FooterTemplate>
</asp:Repeater>

This way in your page at the end you will have exactly what you write in the Repeater in this case what you would get in your HTML page is a table like the following:

<table>
    <thead>
        <tr>
            <th>Columna 1</th>
            <th>Columna 2</th>
            <th>Columna 3</th>
            <th>Columna 4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Valor Columna1</td>
            <td>Valor Columna2</td>
            <td>Valor Columna3</td>
            <td>Valor Columna4</td>
        </tr>
        ...
    </tbody>
</table>
    
answered by 01.09.2017 в 15:40
0

Reviewing other examples you would have to use a method like this, to generate the pure HTML and put it in the table:

public static string GetMyTable(IEnumerable list, params string[] columns)
{
    var sb = new StringBuilder();
    foreach (var item in list)
    {
        //todo this should actually make an HTML table, not just get the properties requested
        foreach (var column in columns)
            sb.Append(item.GetType().GetProperty(column).GetValue(item, null));
    }
    return sb.ToString();
}

Lamarlo as follows:

string htmlTable = GetMyTable(Resultado, "Columna1", "Columna2");//Los nombres de tu columnas separados por comas

Then assign the htmlTable to a div of your page ASP :

  • Create a div in your code aspx :

     <div runat="server" id="divTabla">
     </div>
    
  • Assign the table to div :

     divTabla.InnerHtml = htmlTable;
    

Another way to do it, the same without using gridView ... you could use jquery and the component jqueryDataTables .

Jquery.Datatables

In this case you should follow these steps more or less:

  • Create the method in codeBehind that returns a JSON
  • Use Ajax from client side, with jquery to call this method
  • Assign dataSource with jqueryDataTables
  • When using jquery.DataTables , you have the advantage that the table will not be a simple and flat table , but it may have pager, edit mode, sort, search, etc. ., among other advantages and properties that this component has.

        
    answered by 04.05.2017 в 00:58