1

In my database I am storing html that also includes asp inline expressions <%= %>. I need to have these inline expressions rendered on page, but they are being rendered as string literals.

For example. My database field 'Description' stores HTML markup and content. Within the data in this field there are also asp .net inline expressions such as <%= AuthorName %>. These variables are declared in the code behind and need to be rendered on page wherever the 'description' is bound to.

Also, there are some user controls (.ascx) controls that need to be dynamically rendered.

Here's some detailed example. My code behind has certain variables declared:

protected Int64 EventId, EventInstanceId;
protected string EventName, HostedBy, Tag, ShortDescription, LongDescription;
protected decimal Cost, EarlyRegDiscount;
protected DateTime StartDateTime, EndDateTime, EarlyRegDate;

These variables are initialized on Page_Load through information stored in the database.

My front end looks something like this:

<div id="EventName">
    <%= EventName %>
</div>
<div id="Description">
    <%= LongDescription %>
</div>

The c# string variable (and associated database field) LongDescription contains additional HTML markup, as well as asp inline expressions such as <%= HostedBy %>. So wherever <%= LongDescription %> is rendered and it's containing HTML markup, it should also render any embeded expressions such as <%= HostedBy %>. How can this be achieved?

SR8
  • 53
  • 5
  • Q: Could you show us the code you've tried? Or at least the code you hope to use? – paulsm4 Feb 07 '19 at 01:25
  • 1
    @paulsm4 Just added some detailed example. Thank you in advance. – SR8 Feb 07 '19 at 01:52
  • Is the implementation flexible? Does the markup loaded from the database absolutely have to contain ASP.Net server tags, or could you implement a custom syntax in the markup which will be replaced by variable values? – Benjamin Ray Feb 07 '19 at 02:34
  • 1
    Yes, the implementation is flexible. We have control over the front end, code behind, and also database content. I think I have a sense of where you might be going but would appreciate more details... @BenjaminRay Thank you. – SR8 Feb 07 '19 at 02:43

1 Answers1

1

Because the implementation is flexible (as you stated in your comment above), it seems like there would be better ways to do what you want to accomplish. That being said, this solution might work for you.

Instead of using ASP.Net inline expressions within the variable, you could use a custom syntax for placeholders that will be replaced by variable contents. I have used this in the past to load a template and populate it with data (e.g. HTML email notification template loaded from disk/db, with actual variable values inserted programmatically).

protected String WidgetOutput;
protected String DatabaseEntry;

protected String WidgetName;
protected String WidgetDescription;

protected void Page_Load(object sender, EventArgs e) {

    // Set some sample variable content
    WidgetName = "Fred The Widget";
    WidgetDescription = "I am a basic widget";

    // Load value of DatabaseEntry from database. (Setting it manually here for testing purposes.)
    DatabaseEntry = "<div><h1>Widget Info</h1><p>This widget's name is <strong>%%WidgetName%%</strong>.</p><p>This widget's description is <strong>%%WidgetDescription%%<strong>.</p></div>";

    // Prepare the database entry for output by replacing placeholders with actual variable contents.
    DatabaseEntry = DatabaseEntry.Replace("%%WidgetName%%", WidgetName);
    DatabaseEntry = DatabaseEntry.Replace("%%WidgetDescription%%", WidgetDescription);

    // Populate the variable that will be output on the page
    WidgetOutput = DatabaseEntry;

}

You mentioned the need to include output from UserControls. If those are also to be rendered inside the template, you could use this answer to render the control output to a string, then replace it with variables like I did above.

If this doesn't work for you, perhaps you could explain a bit more about why you have to do things the way you're doing them, and then we might be able to guide you towards a better solution.

Benjamin Ray
  • 1,855
  • 1
  • 14
  • 32
  • 1
    Thank you for the example. Not sure why I hadn't thought of it before as I've done some similar implementation with email templates used for transactional purposes. Getting the custom user control to function was a bit more tricky. I had to instatiate a Page and Form object, even then it won't work as the control itself would not render and had an error. I would get the Page and Form output, but nothing for the control. I had to set EnableEventValidation="false" and override theVerifyRenderingInServerForm. Working well now. Thank again. – SR8 Feb 08 '19 at 04:54