将HTML标记从.aspx用户控件传递到.ascx .NET 4.6

时间:2017-12-10 00:48:11

标签: c# asp.net

我试图获得与Passing html markup into an ASP.NET User Control类似的工作,除了我没有使用MVC,而且这个答案似乎不再有效,我得到500个错误复制该代码并尝试按原样使用它。

我需要将一个用户控件添加到页面中,该页面需要多个html标记位,使用类似于模板的内容(类似于Angular模板)。在Control中,我需要抓取那些HTML片段并将它们注入到结构的其余部分。我还需要能够获取类似于普通控件如何工作的属性,并注入它们的值。

例如,以下是我将开发人员插入到aspx页面中的内容:

<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>

在.ascx控件中,我会有类似的东西:

<%@ 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>

然后在后面的代码中,我尝试了很多东西,主要是尝试模拟上面链接的Passing html标记答案,但无法使其工作。

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

1 个答案:

答案 0 :(得分:0)

此处的另一种方法是根据您的要求显示/隐藏一堆literal controls。如果你需要的话,你甚至可以从代码中动态设置他们的内容。