简体   繁体   中英

Visual Studio designer warning for custom web server control “Content is not allowed between the opening and closing tags for element <control-name>”

The following code works as intended but I can't seem to get rid of the VS designer warning above.

Here's my custom web server control class:

<ParseChildren(True, "Content")>
Public Class Test
    Inherits WebControl

    Property Content As Control

    Private Sub Test_Init(sender As Object, e As EventArgs) Handles Me.Init
        Me.Controls.Add(Content)
    End Sub

End Class

Here is the markup:

<app:Test runat="server">
    This is a test<br />
    <asp:Button runat="server" Text="Click !" />
</app:Test>

I have tried different combinations of the following attributes on the Content property, but I can't get rid of the warning:

<PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>

Found a few sources which indicate that the above attributes should get rid of the warning. I've had no luck with these. Could someone please point me in the right direction?

So with a fresh start today, after the weekend, I finally got it to work as I intended.

I changed <ParseChildren(True, "Content")> to <ParseChildren(False), PersistChildren(True)> on the class declaration, removed the Content property and it started working as expected.

Note that <ParseChildren(False), PersistChildren(True)> will automatically place content inside the opening and closing tags into the child control collection of the custom control, making it easy to work with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM