0

I'm attempting to get something working similar to Passing html markup into an ASP.NET User Control, except I'm NOT using MVC, and that answer doesn't seem to work anymore, I get 500 errors when I duplicate that code and try to use it as is.

I've got the need to add a user control to a page that will require multiple bits of html markup in it, using something akin to templates (similar to Angular templates). In the Control, I then need to grab those pieces of HTML and inject them into the rest of the structure. I also need to be able to grab attributes similar how a normal control would work, and inject their values as well.

For example, here is what I will have my developers insert into the aspx pages:

<uc:FlexImgContent runat="server" FlexImgClasses="col-sm-4 col-lg-3" FlexContentClasses="col-sm-8 col-lg-9">
    <FlexImage>
        <img class="img-responsive" width="100%"
                 src="assets/images/img.png"
                 alt="A descriptive alt tag">
    </FlexImage>

    <FlexContent>
        <h3>An h3 title (could be h2, h4 as well, hence the need for html)</h3>

        <p>A block of content following. There could be more...</p>

        <button class="btn" type="button">Like a button!</button>
    </FlexContent>
</uc:FlexImgContent>

In the .ascx control, I would have something akin to this:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FlexImgContent.ascx.cs" Inherits="OUR.DOMAIN.STUFF.Core.CommonControl" %>

<div class="flexible row">
    <div class="<%= this.FlexImgClasses %>">
        <figure>
            <%= this.FlexImage %>
        </figure>
    </div>

    <div class="<%= this.FlexContentClasses %>">
        <%= this.FlexContent %>
    </div>
</div>

And then in the code behind, I've tried a bunch of stuff, mostly attempting to simulate the Passing html markup answer linked above, but couldn't get it to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sdc.Server.StampsWebsite._Controls
{
    [ParseChildren(true, "FlexImage")] // How do I get multiple bits of data here, like FlexImage, FlexContent, FlexImgClasses, FlexContentClasses, etc?
    public partial class FlexImgContent : System.Web.UI.UserControl
    {

        // Do I need this for each public string? 
        [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
        public string FlexImage { get; set; }
        public string FlexContent { get; set; }

        // From other answers, I know these are wrong but am thoroughly confused at how to get this right
        public string FlexImageClasses { get; set; }
        public string FlexContentClasses { get; set; }

        // Does anything need to go into the page load?
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}
Will Lanni
  • 923
  • 12
  • 25

1 Answers1

0

Another approach here is to have a bunch of literal controls that you show/hide based on your requirements. You could even set their content dynamically from code behind if that is what you needed.

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • Maybe it's just me--I've noticed I learn better when I have something to reference--but answers that contain examples seem way more helpful. I did try to drop in my content as ``, and then process the Literal in the code behind using a get and set, but I get compilation errors. I basically referenced this answer to try to do it: https://stackoverflow.com/a/1827822/1078904 -- is that kind of what you are referring to? – Will Lanni Dec 11 '17 at 20:18