0

I need to send a table obtained using a repeater in a mail, are there examples on how embed a repeater in a mail body? I'm using MailMessage to send the mail. I tried to implement this example:

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);

    MyRepeater.RenderControl(htmlWrite);

    string emailContent = stringWrite.ToString();

but there's an error:

Control ''m_repeater_btn" of type 'LinkButton' must be placed inside a form tag with    runat=server.

Even if i put the tag form i get still an error, how should i put it?

this is my asp.net page:

<%@ 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>
Camilla
  • 949
  • 2
  • 14
  • 26

3 Answers3

1

1) Try to execute your page (not single repeater):

    private string LoadMailTemplate()
    {
        var sb = new StringBuilder();

        using (var sw = new StringWriter(sb))
        {
            this.Server.Execute(this, sw, false);
        }

        return sb.ToString();
    }

where this is your page class.

2) Also you can put your repeater on the control. Here is code that execute control's body:

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

Call:

string  emailBody = LoadMailTemplate("~/modules/eurolight/MailTemplates/OrderMailTemplate.ascx");
algreat
  • 8,592
  • 5
  • 41
  • 54
  • it is a path of the control. For example templatePath = "~/modules/eurolight/MailTemplates/OrderMailTemplate.ascx". But first try to execute yor page (without using controls) – algreat Jan 21 '13 at 14:41
  • try first this code: var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { string emailBody = page.Server.Execute(page, sw, false); } Use 'this' instead of 'page' – algreat Jan 21 '13 at 14:42
  • it says: cannot implicitly convert type void to type string – Camilla Jan 21 '13 at 14:46
  • how can I find the path of the control? – Camilla Jan 21 '13 at 14:49
  • In your case you don't need a control path. You can try to execute your page inside your ViewMySisters.aspx.cs file: this.Server.Execute(this, sw, false); – algreat Jan 21 '13 at 14:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23097/discussion-between-algreat-and-camilla) – algreat Jan 21 '13 at 14:52
0

An email is not an asp.net page. There for you are limited to flat HTML controls. I change change the link button to an anchor tag and link it back to your web application and load the details based on query string.

Brian P
  • 1,569
  • 13
  • 23
0

You need this code:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)

End Sub
4444
  • 3,541
  • 10
  • 32
  • 43