Passing html markup to an ASP.NET user control

In the following example

<uc1:MyUserControl> <p>this is html</p> </uc1:MyUserControl> 

How can I access "<p>this is html</p>" as a string in MyUserControl so that I can insert it into the response?

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

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

EDIT:

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

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 (must be added manually 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 display 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> 
+6
source share
1 answer

In typical WebForms controls, HTML will automatically fit into the Literal control in the MyUserControl Controls collection. Controls with template properties work slightly differently, but you can still access this in the same way through a property whose name you use (for example, MessageTemplate ).

MVC works in a completely different way, and I don't know if there is a way to do what you ask. You might want to consider using javascript to analyze what the visualized client side actually gets if it doesn't work for you.

+3
source

Source: https://habr.com/ru/post/890506/


All Articles