×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Custom Template Control Allowing Similar Children

Custom Template Control Allowing Similar Children

Custom Template Control Allowing Similar Children

(OP)
I was hoping to create a custom control whose template would only allow elements of a certain type. Below is an example of desired markup:

CODE --> Markup

<cc1:EntityView runat="server">
    <LabelledControls>
        <cc1:LabelledTextBox runat="server" />
        <cc1:LabelledTextBox runat="server" />
        <cc1:LabelledDropDownList runat="server" />
        <cc1:LabelledTextBox runat="server" />
    </LabelledControls>
</cc1:EntityView> 

I've tried several different things but each method seems to have a limitation/flaw which prevents me from getting it to work. I don't make custom controls that often, and when I do, they have been relatively simple. One way that I have tried doing it is shown below, however wouldn't it basically require me to exhaustively set each of the properties and methods for the TextBox, DropDownList, etc?

CODE --> C#

[ToolboxData("<{0}:EntityView runat=server></{0}:EntityView>")]
[ParseChildren(true)]
public class EntityView : WebControl
{
    private Collection<LabelledControl> _LabelledControls = new Collection<LabelledControl>();

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Browsable(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public Collection<LabelledControl> LabelledControls
    {
        get { return _LabelledControls; }
    }
}

[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public abstract class LabelledControl : WebControl
{

}

[ToolboxData("<{0}:LabelledTextBox runat=server></{0}:LabelledTextBox>")]
public class LabelledTextBox : LabelledControl
{
    private Label _lbl = new Label();
    private TextBox _txt = new TextBox();
    private Unit _Width;

    public String LabelText
    {
        get { return _lbl.Text; }
        set { _lbl.Text = value; }
    }

    public String Text
    {
        get { return _txt.Text; }
        set { _txt.Text = value; }
    }

    public override Unit Width
    {
        get { return _Width; }
        set { _Width = value; }
    }

    public Unit LabelWidth
    {
        get { return _lbl.Width; }
        set { _lbl.Width = value; }
    }

    public Unit TextBoxWidth
    {
        get { return _txt.Width; }
        set { _txt.Width = value; }
    }

    protected override void Render(HtmlTextWriter output)
    {
        output.Write("<span style=\"display: inline-block; width: " + _Width + "; border: 1px solid red;\"" + (String.IsNullOrWhiteSpace(CssClass) ? "" : "class=\"" + CssClass + "\"") + ">");
        output.Write("<span style=\"float: left; vertical-align: bottom;\">" + _lbl.Text + ":" + "</span>");
        _txt.Style["float"] = "right";
        _txt.RenderControl(output);
        output.Write("</span>");
    }
}
        
[ToolboxData("<{0}:LabelledDropDownList runat=server></{0}:LabelledDropDownList>")]
public class LabelledDropDownList : LabelledControl
{
    private Label _lbl = new Label();
    private DropDownList _ddl = new DropDownList();
    private Unit _Width;

    public String LabelText
    {
        get { return _lbl.Text; }
        set { _lbl.Text = value; }
    }

    public override Unit Width
    {
        get { return _Width; }
        set { _Width = value; }
    }

    public Unit LabelWidth
    {
        get { return _lbl.Width; }
        set { _lbl.Width = value; }
    }

    public Unit DropDownListWidth
    {
        get { return _ddl.Width; }
        set { _ddl.Width = value; }
    }

    protected override void Render(HtmlTextWriter output)
    {
        output.Write("<span style=\"display: inline-block; width: " + _Width + "; border: 1px solid red;\"" + (String.IsNullOrWhiteSpace(CssClass) ? "" : "class=\"" + CssClass + "\"") + ">");
            output.Write("<span style=\"float: left; vertical-align: bottom;\">" + _lbl.Text + ":" + "</span>");
            _ddl.Style["float"] = "right";
            _ddl.RenderControl(output);
        output.Write("</span>");
    }
} 

By the way, I realize there are multiple ways that I can render the contents and I planned on making changes to that code, anyhow. Right now, I am more worried about the code/control structure and how to get it to parse correctly in markup.

If anyone could be of assistance, I would greatly appreciate your help. Thank you!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close