Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Template Control Allowing Similar Children

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
0
0
US
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:
<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:
[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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top