Add child controls to a server control

Last post 01-24-2006 12:08 PM by DWesthead. 1 replies.

Sort Posts:

  • Add child controls to a server control

    01-24-2006, 10:14 AM
    • Member
      110 point Member
    • isa
    • Member since 01-04-2006, 2:28 PM
    • Germany
    • Posts 22
    Hi,

    How can I add child controls to a server control on the .aspx page WITHOUT adding a Template? I want something like this:

    (I only want to add controls of type "mystuff:MyOtherServerControl" as child control)


    <mystuff:MyServerControl ID="..." runat="..." ... >
        <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
        <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
        <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
        <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
    </mystuff:MyServerControl>


    Are there any solutions/ideas how to do this?

    Any help would be highly appreciated.


    ----------------------------------------------------------------------------------------------

    I know this would work, but I need a solution without a template:

    <mystuff:MyServerControl ID="..." runat="..." ... >
        <SomeTemplate>
            <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
            <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
            <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
            <mystuff:MyOtherServerControl ID="..." runat="..." SomeProperties="..." />
        </SomeTemplate>
    </mystuff:MyServerControl>

  • Re: Add child controls to a server control

    01-24-2006, 12:08 PM
    • Participant
      821 point Participant
    • DWesthead
    • Member since 12-20-2005, 11:08 AM
    • Staffordshire, UK
    • Posts 168

    I built up a bespoke login control recently and needed this:

    protected override void CreateChildControls()

    {

    Controls.Add(new LiteralControl("Username: "));

    this.txtUsername = new TextBox();

    Controls.Add(txtUsername);

    txtUsername.Text = "";

    txtUsername.ToolTip = "Enter a Username";

    txtUsername.Attributes.Add("autocomplete", "off");

    Controls.Add(new LiteralControl("</br>"));

    Controls.Add(new LiteralControl("Password: "));

    this.txtPassword = new TextBox();

    Controls.Add(txtPassword);

    txtPassword.Text = "";

    txtPassword.TextMode = TextBoxMode.Password;

    txtPassword.ToolTip = "Enter a Password";

    txtPassword.Attributes.Add("autocomplete", "off");

    Controls.Add(new LiteralControl("</br>"));

    Button btnLogin = new Button();

    Controls.Add(btnLogin);

    btnLogin.Text = "Login";

    btnLogin.ToolTip = "Click to process login credentials";

    btnLogin.Click += new EventHandler(btnLogin_Click);

    Controls.Add(new LiteralControl("</br>"));

    this.lblErrMsg = new Label();

    lblErrMsg.Text = "";

    lblErrMsg.ForeColor = System.Drawing.Color.Red;

    Controls.Add(this.lblErrMsg);

    }

    As for adding another Custom control as a child - asl long as the reference is there in the control you want as the parent, I cant see why it would not be available like the normal server controls (tho i must admit this is something I havent had time to find out for definite)

     

    Please remember to click “Mark as Answer” on the post, if it helps you.
Page 1 of 1 (2 items)