将html标记传递到ASP.NET用户控件中 [英] Passing html markup into an ASP.NET User Control

查看:69
本文介绍了将html标记传递到ASP.NET用户控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,

<uc1:MyUserControl>

   <p>this is html</p>

</uc1:MyUserControl>

如何在MyUserControl中作为字符串访问"<p>this is html</p>",以便可以将其注入响应中?

How do I access "<p>this is html</p>" as a string within MyUserControl so that I might inject it into the response?

我不是在谈论传递像<uc1:MyUserControl myparameter="<p>this is html</p>" />这样的字符串参数,而是如何在开始和结束标记之间或通过诸如<MessageTemplate>标记之类的其他机制访问真正的多行智能感知HTML标记.

I'm not talking about passing in a string parameter like <uc1:MyUserControl myparameter="<p>this is html</p>" />, but how do I access true multi-lined intellisensed HTML markup either between the opening and closing tags or by some other mechanism such as a <MessageTemplate> tag.

为可在ASP.NET MVC 3中使用的解决方案加分!

Bonus points for a solution that works in ASP.NET MVC 3!

感谢StriplingWarrior和此链接作为失踪的拼图,魔法创造了:

Thanks to StriplingWarrior and this link as the missing puzzle piece, magic was made:

因此,在任何视图中:

<%@ Register src="../../Shared/Ribbon.ascx" tagname="Ribbon" tagprefix="uc1" %>
...
<uc1:Ribbon ID="Ribbon" runat="server">
    <Content>
    Hello world! I am <b>pure html</b> being passed into a UserControl!
    </Content>
</uc1:Ribbon>

在Ribbon.ascx中:

In Ribbon.ascx:

<%@ Control Language="C#" CodeBehind="Ribbon.ascx.cs" Inherits="NunYourBeezwax.Views.Shared.Ribbon" %>
<table>
    <tr>
        <td>I am reusable stuff that wraps around dynamic content</td>
        <td><%= this.Content %></td>
        <td>And I am stuff too</td>
    </tr>
</table>

最后,在Ribbon.ascx.cs中(需要在MVC中手动添加)

And finally, in Ribbon.ascx.cs (Need to manually add in MVC)

using System.Web.Mvc;
using System.Web.UI;

namespace NunYourBeezwax.Views.Shared
{
    [ParseChildren(true, "Content")]
    public class Ribbon : ViewUserControl
    {
        [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
        public string Content { get; set; }
    }
}

将渲染为:

<table>
    <tr>
        <td>I am reusable stuff that wraps around dynamic content</td>
        <td>Hello world! I am <p>pure html<p> being passed into a UserControl!</td>
        <td>And I am stuff too</td>
    </tr>
</table>

推荐答案

在典型的WebForms控件中,HTML将自动放入MyUserControl的Controls集合中的Literal控件中.带有模板属性的控件的工作方式略有不同,但是您仍然可以通过您正在使用其名称的属性(例如MessageTemplate)以类似的方式访问此控件.

In typical WebForms controls, the HTML will be automatically put into a Literal control in MyUserControl's Controls collection. Controls with template properties work a little differently, but you may still be able to access this in a similar way through the property whose name you're using (e.g. MessageTemplate).

MVC的工作原理完全不同,我不知道是否有一种方法可以解决您所要求的问题.您可能需要考虑使用javascript来分析在客户端不起作用的情况下实际呈现给客户端的内容.

MVC works totally differently, and I don't know if there's a way to do what you're asking there. You may want to consider using javascript to analyze what actually gets rendered client-side if it doesn't work for you.

这篇关于将html标记传递到ASP.NET用户控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆