我需要在邮件中发送使用转发器获得的表格,是否有关于如何在邮件正文中嵌入转发器的示例?我正在使用MailMessage发送邮件。我试图实现这个例子:
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
MyRepeater.RenderControl(htmlWrite);
string emailContent = stringWrite.ToString();
但是有一个错误:
Control ''m_repeater_btn" of type 'LinkButton' must be placed inside a form tag with runat=server.
即使我把标签形式我还是一个错误,我该怎么说呢?
这是我的asp.net页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ViewMySisters.aspx.cs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Repeater ID="MyRepeater" runat="server">
<HeaderTemplate>
<table style="border-collapse:collapse; border:1px solid grey;" width="500">
<tr>
<td style="border: 1px solid grey;">firstname</td>
<td style="border: 1px solid grey;">lastname</td>
<td style="border: 1px solid grey;">age</td>
<td style="border: 1px solid grey;">work experienes</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border: 1px solid grey;"><%# DataBinder.Eval(Container.DataItem, "NAME")%></td>
<td style="border: 1px solid grey;"><%# DataBinder.Eval(Container.DataItem, "FAMILY NAME")%></td>
<td style="border: 1px solid grey;"><%# DataBinder.Eval(Container.DataItem, "AGE")%></td>
<td style="border: 1px solid grey;"><asp:LinkButton ID="btn" runat="server" Text=">>>>" OnCommand="btnDetails_Command" CommandArgument='<%# Eval("ID") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="sendMail" runat="server" Text="send" OnClick="sendmail_Clicked" />
</asp:Content>
答案 0 :(得分:1)
1)尝试执行您的页面(不是单个中继器):
private string LoadMailTemplate()
{
var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
{
this.Server.Execute(this, sw, false);
}
return sb.ToString();
}
其中this
是您的网页类。
2)你也可以把你的转发器放在控制器上。这是执行控件正文的代码:
public static string LoadMailTemplate(string templatePath)
{
var page = new Page();
Control myControl = page.LoadControl(templatePath);
var sb = new StringBuilder();
page.Controls.Add(myControl);
using (var sw = new StringWriter(sb))
{
page.Server.Execute(page, sw, false);
}
return sb.ToString();
}
致电:
string emailBody = LoadMailTemplate("~/modules/eurolight/MailTemplates/OrderMailTemplate.ascx");
答案 1 :(得分:0)
电子邮件不是asp.net页面。因为你只限于平面HTML控件。我将链接按钮更改为锚标记并将其链接回您的Web应用程序并根据查询字符串加载详细信息。
答案 2 :(得分:0)
您需要以下代码:
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub