2016-09-29 32 views
2

我想使用HTML表格而不是gridview來檢查數據。爲什麼HTML表格?因爲我將使用輸出發送電子郵件,所以我更喜歡HTML表格而不是gridview。另外我不想使用對象,因爲系統只能在服務器上運行。它會自動發送一封電子郵件。任何人都可以幫我解決我的問題嗎?謝謝。使用HTML表格而不是Gridview

這是我到目前爲止。在下面的例子中,我使用了gridview,因爲我不知道如何使用Append來使用HTML Table。

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

任何幫助將不勝感激! :) 謝謝。

+0

這是一個錯誤的對面。 GridView是一個可以呈現HTML表格的服務器端控件。換句話說,它可以做你所要求的,而不必寫HTML。關閉所有花哨的選項(如分頁),給它一些列定義,瞧。 –

回答

1

作爲一個選項,您可以使用Run-Time Text Template來創建電子郵件模板。通過這種方式,您可以簡單地使用模板來使用模板生成輸出。這就像使用ASP.NET MVC和Razor引擎可以做的一樣,但它不僅限於MVC甚至ASP.NET。無論您需要創建模板,您都可以使用這個想法。

運行時文本模板的工作方式類似於aspx頁面。對於使用t4模板瞭解ASP.NET的人來說確實很容易。它使用指令和標籤,並混合內容和代碼。您使用代碼來使輸出動態。然後在您調用其方法TransformText時呈現內容。

您可以使用任何類型作爲Model。該模型可以是您的業務或視圖模型類中的一個,也可以是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

+0

抱歉忘了提及我不允許使用對象或前端編碼。我需要在後臺完成這一切。你有想法如何實現它? –

+0

它完全基於服務器編碼。正如我在答案中提到的那樣,您可以使用任何類型的模型,包括「DataTable」。這是一個非常有用的技術,可以應用於任何類型的應用程序,以獲得諸如電子郵件模板之類的東西。 –

+0

抱歉,但我不熟悉這一點。我不知道如何將您的建議與我的代碼合併。你能用我的代碼給我一個樣本嗎?謝謝。 –

相關問題