使用 HTML Table 而不是 Gridview

Use HTML Table instead of Gridview

我想使用 HTML table 而不是 gridview 来获取数据。为什么 HTML Table?因为我将使用电子邮件发送的输出,所以我更喜欢 HTML Table 而不是 gridview。我也不想使用对象,因为系统只会在服务器上 运行。它会自动发送一封电子邮件。谁能帮我解决我的问题?谢谢。

这是我目前所拥有的。在下面的示例中,我使用的是 gridview,因为我不知道如何使用 HTML Table 和 Append 来做到这一点。

Vb.Net

这就是我调用函数的方式

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

这是我的电子邮件功能,我想使用追加将其转换为 html table。

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

代码运行正常,但不是我想要的方式。我不想使用网格视图。我希望它在 html table 中,例如 :

 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>")

如有任何帮助,我们将不胜感激! :) 谢谢。

作为一个选项,您可以使用 Run-Time Text Template 创建电子邮件模板。这样您就可以简单地使用模型来使用模板生成输出。这就像使用 ASP.NET MVC 和 Razor 引擎可以做的一样,但它不仅限于 MVC 甚至 ASP.NET。您可以在任何需要创建模板的地方使用这个想法。

运行-时间文本模板的工作方式类似于 aspx 页面。对于知道 ASP.NET 的人来说,使用 t4 模板真的很容易。它使用指令和标签,您可以混合内容和代码。您使用代码使输出动态化。然后当您调用其 TransformText 方法时它会呈现内容。

您可以使用任何类型作为 Model。该模型可以是您的业务或视图模型 classes 之一,也可以是 DataTable.

例子

向您的项目添加一个新的 class:

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

添加一个新的 运行-Time 文本模板(也称为预处理模板)并将其命名为 MailTemplate。然后把这个内容放到文件中:

<#@ 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>

使用此代码在运行时生成输出:

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()

现在您可以使用输出来发送电子邮件或将其写入回复。

结果将是: