用户控制的开始和结束标签之间不允许有内容不允许、结束、标签、内容

2023-09-07 14:57:15 作者:爱你,所以放手让你走

我想建立一个用户控件假设 MyDiv.ascx.此控件呈现 div 标记并在后面执行一些代码,例如添加一些属性等,这在此处无关紧要.问题是我想要用户控件的开始标签和结束标签之间的文本.例如:

I want to build a user control suppose MyDiv.ascx. This control renders the div tag and do few more code behind stuff like adding few attributes etc which is not a matter of concern here. The problem is I want text between the opening and closing tags of the user control. Eg:

文本与其他一些 HTML 标记一起出现在此处.

The text goes here with some other HTML tags.

所以当我做这样的事情时,我在运行网站时遇到了解析错误.VS2008 也警告我说元素的开始和结束标签之间不允许有内容 MyDiv".

So when do something like this I get a parsing error while running the website. Also VS2008 warns me by saying "Content is not allowed between the opening and closing tags for element MyDiv".

问题 1:我可以做这样的事情,即打开之间的文本/标记和用户控件的结束标签?

问题 2:如果是,如何

推荐答案

我相信你只需要对控件应用几个属性:

I believe you just need to apply a couple of attributes to the control:

[ParseChildren(false)]
[PersistChildren(true)]
public class MyDiv : UserControl
{
    ...

您可能然后需要覆盖 AddedControl - 我不确定.

You may then need to override AddedControl - I'm not sure.

这样说 - 这就是我写过的唯一一个用户控件的工作原理:)

Put it this way - that's what works for the one and only user control I've ever written :)