How to make successful XHTML validation happy using Custom System.Web.UI.Control?

I am sure this question has been asked and answered; but I could not find it ...

I am creating a very simple custom System.Web.UI.Control that has several properties. I can define the following tag on my aspx page, and all is happy:

<ns:MyControl runat="server" MyProperty="Value" />

However, if I want to have one or more "child" properties, for example:

<ns:MyControl runat="server" MyProperty="Value">
  <Element AnotherProperty="AnotherValue1" />
  <Element AnotherProperty="AnotherValue2" />
</ns:MyControl>

I cannot figure out what I need to do to check XHTML. I always have

  • Content between the end and end tags of the XXX element is not allowed
  • 'XXX' is not supported
  • Name contains uppercase letters that are not allowed

, , , , . , , , :

  [ParseChildren(true)]
  [PersistChildren(false)] 
  public class MyControl : Control
  {
    public String MyProperty { get; set; }
    public String Element { get; set; }
  }

, . , , XHTML .

.

+3
2

2008 , : ASP.net .

, :

<Abc:CustomControlUno runat="server" ID="Control1">
    <Children>
        <Abc:Control1Child IntegerProperty="1" StringProperty="Item1" />
        <Abc:Control1Child IntegerProperty="2" StringProperty="Item2" />
    </Children>
</Abc:CustomControlUno>

:

[ParseChildren(true)]
[PersistChildren(true)]
[ToolboxData("<{0}:CustomControlUno runat=server></{0}:CustomControlUno>")]
public class CustomControlUno : WebControl, INamingContainer
{
    private Control1ChildrenCollection _children;

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Control1ChildrenCollection Children
    {
        get
        {
            if (_children == null)
                _children = new Control1ChildrenCollection();
            return _children;
        }
    }
}

public class Control1ChildrenCollection : List<Control1Child>
{
}

public class Control1Child
{
    private int integerProperty;
    private string stringProperty;

    public string StringProperty
    {
        get { return stringProperty; }
        set { stringProperty = value; }
    }

    public int IntegerProperty
    {
        get { return integerProperty; }
        set { integerProperty = value; }
    }
}
+2

ASP.NET Menu . , <Items>, Items ( ).

[PersistenceMode(PersistenceMode.InnerProperty)]
public MenuItemCollection Items
{
    get { /* ... */ }
}

, , , [ParseChildren(true)] [PersistChildren(false)] , .

(a la DropDownList), PersistenceMode.InnerDefaultProperty .

0

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


All Articles