问题描述
在下面的例子中,
<uc1:MyUserControl><p>这是html</p></uc1:MyUserControl>
如何在 MyUserControl 中将 "<p>this is html</p>"
作为字符串访问,以便将其注入响应中?
我不是在谈论传递像 <uc1:MyUserControl myparameter="<p>this is html</p>" 这样的字符串参数/>
,但是如何在开始和结束标记之间或通过诸如 <MessageTemplate>
标记等其他机制访问真正的多行智能 HTML 标记.p>
适用于 ASP.NET MVC 3 的解决方案的奖励积分!
感谢 StriplingWarrior 和 此链接 作为缺失的拼图,魔法被创造了:
所以,在任何情况下:
<%@注册src="../../Shared/Ribbon.ascx" tagname="Ribbon" tagprefix="uc1" %>...<uc1:Ribbon ID="Ribbon" runat="server"><内容>你好世界!我是<b>纯html</b>被传递到用户控件中!</内容></uc1:功能区>
在 Ribbon.ascx 中:
<%@ Control Language="C#" CodeBehind="Ribbon.ascx.cs" Inherits="NunYourBeezwax.Views.Shared.Ribbon" %><桌子><tr><td>我是围绕动态内容的可重复使用的东西</td><td><%= this.Content %></td><td>而我也是这样的</td></tr></表>
最后,在 Ribbon.ascx.cs 中(需要在 MVC 中手动添加)
使用 System.Web.Mvc;使用 System.Web.UI;命名空间 NunYourBeezwax.Views.Shared{[ParseChildren(真,内容")]公共类功能区:ViewUserControl{[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]公共字符串内容{获取;放;}}}
将呈现为:
<tr><td>我是围绕动态内容的可重复使用的东西</td><td>世界你好!我是<p>纯html<p>被传递到 UserControl!</td><td>而我也是这样的</td></tr></表> 解决方案 在典型的 WebForms 控件中,HTML 会自动放入 MyUserControl 的 Controls
集合中的 Literal 控件中.具有模板属性的控件的工作方式略有不同,但您仍然可以通过您正在使用其名称的属性(例如 MessageTemplate
)以类似的方式访问它.
MVC 的工作方式完全不同,我不知道是否有一种方法可以满足您的要求.如果对您不起作用,您可能需要考虑使用 javascript 来分析实际在客户端呈现的内容.
In the following example,
<uc1:MyUserControl>
<p>this is html</p>
</uc1:MyUserControl>
How do I access "<p>this is html</p>"
as a string within MyUserControl so that I might inject it into the response?
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.
Bonus points for a solution that works in ASP.NET MVC 3!
EDIT:
Thanks to StriplingWarrior and this link as the missing puzzle piece, magic was made:
So, in any view:
<%@ 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>
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>
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; }
}
}
Will Render as:
<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>
解决方案 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 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 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-04 06:10
-
在添加DBML对象时删除了Designer.cs
-
定义CLion分析仪的预处理器符号
-
打印出os.popen()的输出而无需在python中进行缓冲
-
如何在实体框架上应用全局过滤器?
-
如何获取正在执行的 Perl 脚本的完整路径?
-
GPS设备的TCP监听器
-
从星期一开始,每周将数据汇总到每周级别
-
asp.net 2010启动页面名称?
-
在Android HttpClient的SSL证书
-
即使用笔,如何阻止乌龟画画?
-
如何使用 Rails 生成此架构?
-
Django管理表单为多对多关系
-
将科学数据存储在关系数据库中
-
HTML画布 - 绘制弯曲的箭头
-
“ NA”导致R中附加时间序列的分解
查看更多