Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add child nodes to custom asp.net user control derived from System.Web.UI.Control

I would like to know how to add some additional child nodes to a custom user control class derived from System.Web.UI.Control.

For example currently I have a control that contains no child nodes and on the design surface looks like the following.

<cust:MyCustomControl id="ctlMyCustomControl" runat="server" attribute1="somevalue" attribute2="somevalue" ></MyCustomControl>

What I am looking for is to have the ability to add n number of child nodes to this control from the design surface and then access their values from the code. So adding to the control stated above.

<cust:MyCustomControl id="ctlMyCustomControl" runat="server" attribute1="somevalue" attribute2="somevalue" >
  <childnode1>value1</childnode1>
  <childnode2>value2</childnode2>
</MyCustomControl>

It is not clear to me how to access the child nodes.

Any insight on how to do this is appreciated.

like image 656
Doug Avatar asked Sep 04 '10 14:09

Doug


People also ask

How do you make the event raised by child controls in a user control available to the host page?

By default, events raised by child controls in a user control are not available to the host page. However, you can define events for your user control and raise them so that the host page is notified of the event. You do this in the same way that you define events for any class.

Which are group of controls derived directly from the system web UI web Control base class?

Web Server Controls are group of controls derived directly from the System. Web. UI. WebControls base class.

Which class do you derive your custom server control?

The Control class is the base class for all ASP.NET server controls, including custom controls, user controls, and pages.

What is user control how it differs from ASP.NET server controls?

A User Control is a partial web page, created in the same way as any other web page in ASP.NET, except that it has an . ASCX extension, and it can be embedded in your other ASPX pages. Server controls are controls that execute on the server and render markup to the browser.

How do I create a new user in ASP NET?

Enter newuser as the user name, enter [email protected] as the email address, enter a password, and then click the Create Userbutton. ASP.NET creates the new user account, logs you in as the new user, and returns to the home page.

How do I create a custom control that has a UI?

If you are authoring a control that does have a UI, derive from WebControl or any control in the System.Web.UI.WebControls namespace that provides an appropriate starting point for your custom control. The Control class is the base class for all ASP.NET server controls, including custom controls, user controls, and pages.

How to add a parent control to a htmlgenericcontrol?

you need to use a panel first. then you can create child HtmlGenericControl and then parent HtmlGenericControl and then you can add the parent control into the panel. for example:

What is the control class in ASP NET?

Remarks. The Control class is the base class for all ASP.NET server controls, including custom controls, user controls, and pages. ASP.NET pages are instances of the Page class, which inherits from the Control class, and that handle requests for files that have an .aspx extension. The Control class can directly or indirectly be used as...


2 Answers

You want to be able to describe asp.net control properties declaratively.

To be able to have the following markup:

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

You need the following code:

[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
{

    public int IntegerProperty { get; set; }
    private string StringProperty { get; set; }
}
like image 158
Rob Avatar answered Oct 23 '22 10:10

Rob


If you wanted to support the given syntax as-is (without having to use the tag prefixes), you could use a ControlBuilder:

 //MyControlBuilder 
 public class MyControlBuilder : ControlBuilder
 {
   public override Type GetChildControlType(string tagName, IDictionary attribs)
   { 
     if (tagName.StartsWith("childnode")) return typeof(Control);
     else return base.GetChildControlType(tagName,attribs);
   }

   public override void AppendLiteralString(string s)
   { 
     //ignore literals betwen tags
   }
 }

 //MyCustomControl
 [ParseChildren(false)]
 [ControlBuilder(typeof(MyControlBuilder))]
 public class MyCustomControl : Control
 {
   public string attribute1 {get;set;}
   public string attribute2 {get;set;}

   protected override void AddParsedSubObject(object obj)
   {
     Control ctrl = (Control) obj;
     LiteralControl childNode = ctrl.Controls[0];

     //Add as-is (e.g., literal "value1")
     Controls.Add(childNode);
   }
 }

See also http://msdn.microsoft.com/en-us/library/system.web.ui.controlbuilder.aspx

like image 43
Mark Cidade Avatar answered Oct 23 '22 09:10

Mark Cidade