Search code examples
htmlasp.netvb.netaspxgridview

Use HTML Table instead of Gridview


I want to use HTML table instead of gridview to fecth the data. Why HTML Table? Because I'll be using the output for email sending so I prefer HTML Table instead of gridview. Also I don't want to use object as the system will run on the server only. It will automatically send an email. Can anyone help me with my problem? Thank you.

Here is what I have so far. On the example below I'm using gridview because I don't know how to do it using HTML Table using Append.

Vb.Net

This is how I call my function

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        SendEmail()
 End Sub

This is my email function that i want to be converted into html table using append.

Protects Sub SendEmail()
     For Each dt As System.Data.DataTable In prod3()
            Dim i As Integer = i + 1
            Dim dv As New System.Data.DataView(dt)
            Dim dt2 As System.Data.DataTable = dv.ToTable(False, {"Name", "Product", "Expiry"})
            Dim y As Date = dt.Rows(0)("pdate")
        Dim dte1, dte2, dte3 As String

            Select Case i
                Case 1
                    dte1 = dt.Rows(0)("pdate").ToString
                    dte1 = y.Date.ToString("MM/dd/yyyy")
                    dte1 = y
                    GridView11.DataSource = dt2
                    GridView11.DataBind()
                Case 2
                    dte2 = dt.Rows(0)("pdate").ToString
                    dte2 = y.Date.ToString("MM/dd/yyyy")
                    dte2 = y
                    GridView12.DataSource = dt2
                    GridView12.DataBind()
                Case 3
                    dte2 = dt.Rows(0)("pdate").ToString
                    dte2 = y.Date.ToString("MM/dd/yyyy")
                    dte2 = y
                    GridView13.DataSource = dt2
                    GridView13.DataBind()
            End Select

        Next
End SUb

Public Function prod3() As List(Of DataTable)

        Dim ds As New DataSet
        Dim cmd As New SqlCommand
        Dim ldt As New List(Of DataTable)
        Dim adp As SqlDataAdapter = New SqlDataAdapter
        Dim c As New SqlConnection("myconnection")
        cmd.Connection = c
        cmd.CommandText = "storedprocname"
        cmd.Parameters.AddWithValue("@name", "%")
        cmd.Parameters.AddWithValue("@product", "%")
        cmd.Parameters.AddWithValue("@expiry", "%")
        cmd.Parameters.AddWithValue("@datefrom", DateTime.Today.AddDays(1)) 
        cmd.Parameters.AddWithValue("@dateto", DateTime.Today.AddDays(3)) 
        cmd.Parameters.AddWithValue("@cost", "%")
        cmd.CommandType = CommandType.StoredProcedure
        adp.SelectCommand = cmd
        adp.Fill(ds)
        Dim dv As New DataView(ds.Tables(0))
        Dim dvfilter As DataTable = dv.ToTable(True, {"pdate"})
        For Each dtrow As DataRow In dvfilter.Rows
            Dim dt2 As New DataTable
            dv.RowFilter = "date =#" + dtrow("pdate") + "#"
            dt2 = dv.ToTable(False, {"DATE", "Name", "Product", "Expiry"})
            ldt.Add(dt2)
        Next
        Return ldt
    End Function

The code is working but not the way I want. I don't want to use gridview. I want it to be in html table like :

 Dim builder As New StringBuilder
        builder.Append("<!DOCTYPE html><html>")
        builder.Append("<head>")
        builder.Append("</head>")
        builder.Append("<body>")
        builder.Append("<table>")
        builder.Append("</table>")
        builder.Append("<body>")

Any help would be much appreciated! :) Thank you.


Solution

  • As an option you can use Run-Time Text Template to create an email template. This way you can simply use a model for generating output using a template. It's like what you can do using ASP.NET MVC and Razor engine, but it's not limited to MVC or even ASP.NET. You can use this idea wherever you need to create a template.

    Run-time text template works like a aspx page. For a person who know ASP.NET using t4 templates is really easy. It uses directives and tags and you mix content and code. You use code to make the output dynamic. Then it renders the content when you call its TransformText method.

    You can use any type as Model. The model can be one of your business or view model classes or it can be a DataTable.

    Example

    Add a new class to your project:

    Public Class Product
        Public Property Name As String
        Public Property Price As Integer
    End Class
    

    Add a new Run-Time Text Template (which is also known as Preprocessed Template) and name it MailTemplate. Then put this content to the file:

    <#@ template language="VB" #>
    <#@ assembly name="System.Core" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ parameter type="System.Collections.Generic.List(Of Product)" name="Model"#>
    <html>
    <head>
        <title>Products</title>
        <style type="text/css">
            body { font-family: Calibri;width:400px;}
            table { text-align:center; }
            .container {width:400px;}
        </style>
    </head>
    <body>
    <div class="container">
    <h1 style="text-align:center;">List of Recent Products</h1><hr/>
    Here is list of recent products:
    <table style="width:100%">
        <tr><th>Index</th><th>Name</th><th>Price</th></tr>
        <# Dim index As Integer = 1
           For Each item in Model 
        #>
        <tr>
            <td><#=index#></td>
            <td><#=item.Name#></td>
            <td><#=item.Price#></td>
        </tr>
        <#     index = index + 1
           Next 
        #>
    </table>
    <div>
    </body>
    </html>
    

    Use this code to generate output at runtime:

    Dim template As New My.Templates.MailTemplate
    template.Session = New Dictionary(Of String, Object)
    Dim model = New List(Of Product)()
    model.Add(New Product With {.Name = "Product 1", .Price = 100})
    model.Add(New Product With {.Name = "Product 2", .Price = 100})
    model.Add(New Product With {.Name = "Product 3", .Price = 100})
    template.Session("Model") = model
    template.Initialize()
    Dim output = template.TransformText()
    

    Now you can use output to send an email or write it to response.

    The result would be:

    enter image description here