简体   繁体   中英

Pass HTML Markup from .aspx User Control to .ascx .NET 4.6

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)
        {
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM