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

Content is not allowed between the opening and closing tags for user control(用户控制的开始和结束标签之间不允许有内容)

本文介绍了用户控制的开始和结束标签之间不允许有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个用户控件假设 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 :)

这篇关于用户控制的开始和结束标签之间不允许有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:用户控制的开始和结束标签之间不允许有内容

基础教程推荐