gpt4 book ai didi

html - 使用 HTML Table 而不是 Gridview

转载 作者:行者123 更新时间:2023-11-28 01:05:59 26 4
gpt4 key购买 nike

我想使用 HTML 表格而不是 gridview 来获取数据。为什么选择 HTML 表格?因为我将使用电子邮件发送的输出,所以我更喜欢 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 表格。

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

代码可以正常工作,但不是我想要的方式。我不想使用 GridView 。我希望它像这样在 html 表中:

 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 方法时,它会呈现内容。

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

示例

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

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

添加一个新的运行时文本模板(也称为预处理模板)并将其命名为 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()

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

结果是:

enter image description here

关于html - 使用 HTML Table 而不是 Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760288/

26 4 0