Hi,
here is an example.
Assume you have ASPX as follows:
***************************
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
<asp:Button ID="btnSend" Runat="server" Text="Send as email" />
<asp:Label ID="lblInfo" Runat="server" />
</form>
***************************
And code as follows on page:
***************************
Private Sub BindGrid()
'Create an example data source
Dim dt As New DataTable
dt.Columns.Add("ID", GetType(System.Int32))
dt.Columns("ID").AutoIncrement = True
dt.Columns.Add("Text", GetType(System.String))
Dim dr As DataRow = dt.NewRow()
dr(1) = "First"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(1) = "Second"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(1) = "Third"
dt.Rows.Add(dr)
'Bind the dataGrid
DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindGrid()
End If
'If email was sent
If Request.QueryString("emailsent") = "true" Then
lblInfo.Text = "Email was sent"
End If
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
'Set flag to indicate we want to send an email
Context.Items("sendasemail") = True
End Sub
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
'Rendering logic varies based on if rendering or sending the email
If Context.Items("sendasemail") Is Nothing Then
MyBase.Render(writer)
Else
'Get the HTML output
Dim htmlStringWriter As New IO.StringWriter
Dim customHtmlWriter As New HtmlTextWriter(htmlStringWriter)
'We are only interested in the DataGrid's output
DataGrid1.RenderControl(customHtmlWriter)
'If you want to get the complete page output
'Use MyBase.Render(customHtmlWriter)
'Send the email
Dim mailmsg As New Mail.MailMessage
mailmsg.To = "re******@domain.com"
mailmsg.From = "se****@domain.com"
mailmsg.Subject = "Page you requested"
mailmsg.Body = htmlStringWriter.ToString()
mailmsg.BodyFormat = Mail.MailFormat.Html
System.Web.Mail.SmtpMail.SmtpServer = "yoursmtpserver"
System.Web.Mail.SmtpMail.Send(mailmsg)
Response.Redirect(Request.Url.AbsoluteUri & "?emailsent=true")
End If
End Sub
***************************************
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Tarren" <noemailplease@thankyou> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
Hi:
I have an aspx page that takes a dataset, makes the datagrid, and displays
it.
The new thing I have to do is take this same output and send it as an
email.
Is there an easy way to do this? Any examples?
Thanks for the help!
- T