Mark Megahey
Mark Megahey

Reputation: 1

c# Matching placeholders to text.html and replacing

I've run into a small problem with an application I'm building in Visual Studios c#. I have a WebBrowser that displays an email for preview before you send it, I'm trying to use string replacement to automatically set some details of the email in the preview window.

The EmailTemplateConfigClass has a public list get and set property

public List<HeaderFooterReplacement> HeaderFooterReplacements { get; set; }

I'm using foreach to get the HeaderFooterReplacements and access the (Placeholder/Value) properties on them.

namespace EmailApp.Model
{
    public class HeaderFooterReplacement
    {
        public string Placeholder { get; set; }
        public string Value { get; set; }
    }
}

Then using an if conditional to match the placeholder to a preset html email template find the same text in the email and replace it with the value using (.Replace).

    private void PopulatePreview()
    {
        foreach (var HeaderFooterReplacement in EmailTemplateConfig)
        {
            if (headerFooterContents == HeaderFooterReplacement.Placeholder)
            {
                headerFooterContents.Replace("##HEADER_PHONENUMBER##", HeaderFooterReplacement.Value)
                          .Replace("##HTML_TITLE##", HeaderFooterReplacement.Value)
                          .Replace("##EMAIL_HEADING##", HeaderFooterReplacement.Value);
            }
        }

        WebBrowser1.DocumentText = headerFooterContents;
    }

Lastly I'm setting the web browser preview text to be the html after the replacement is complete, there are no console error and everything seem to work fine in debugging mode except when I preview the email It still show the placeholder e.g. ##EMAIL_HEADING##.

Any ideas? Also if you need clarification on anything just ask.

Thanks

Upvotes: 0

Views: 824

Answers (1)

Nino
Nino

Reputation: 7095

in line

if (headerFooterContents == HeaderFooterReplacement.Placeholder)

you're comparing whole contents with placeholder. Also, Replace returns string, and does not replace values in original string, you you have to assign headerFooterContents again. Your foreach loop should be something like this:

foreach (var HeaderFooterReplacement in EmailTemplateConfig)
{
    if (headerFooterContents.Contains(HeaderFooterReplacement.Placeholder))
    {
        headerFooterContents = headerFooterContents.Replace(HeaderFooterReplacement.Placeholder, HeaderFooterReplacement.Value);
    }
}

Upvotes: 1

Related Questions