Home All Groups Group Topic Archive Search About

Conditional Control / Nested Tags

Author
9 Oct 2007 3:24 AM
Sam via DotNetMonster.com
I need to create a control that contains two child collections and renders
one of them depending on a condition. How can I make it parse "true" and
"false" nested tags? I cannot use the standard MultiView control as I do need
these "true" and "false" tags.

<MyControl runat="server">
    <true>
        <!-- Some child controls that are visible when the condition is true -
->
    </true>

    <false>
        <!-- Some child controls that are visible when the condition is false
-->
    </false>
</MyControl>

--
Message posted via http://www.dotnetmonster.com

Author
9 Oct 2007 5:55 PM
David R. Longnecker
Sam-

By "child collections," Are you wanting XML as it somewhat looks below, or
generic collections, or actual controls?

If you have a custom control, why not pass a parameter to it and handle rendering
additional "child" controls inside the custom control.

<MyControl runat="server" id="myCustomControl" LoadControls="true" />

Here's a quick example.  This custom control will generate 10 text boxes
if LoadControls is "true" and 10 buttons if it's false.  You would replace
the control creation with whatever programmatic functionality you desire.

The ServerControl.cs file:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TSCustomControls
{
    [DefaultProperty("LoadControls")]
    [ToolboxData("<{0}:VariableControl runat=server></{0}:VariableControl>")]
    public class VariableControl : WebControl
    {
        [Bindable(true)]
        [DefaultValue("true")]
        [Localizable(true)]
        public string LoadControls
        {
            get
            {
                String s = (String)ViewState["LoadControls"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["LoadControls"] = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (LoadControls == "true")
            {
                // For true, create textboxes.
                for (int i = 0; i <= 10; i++)
                {
                    TextBox tb = new TextBox();
                    tb.Text = i.ToString();
                    base.Controls.Add(tb);
                }
            }

            if (LoadControls == "false")
            {
                // For false, create buttons.
                for (int i = 0; i <= 10; i++)
                {
                    Button btn = new Button();
                    btn.Text = i.ToString();
                    base.Controls.Add(btn);
                }
            }

            base.Render(output);
        }
    }
}

the ASPX usage example:

<cc1:VariableControl ID="VariableControl1" runat="server" LoadControls="true"
/>

the generated HTML output:

<span id="VariableControl1">
<input name="ctl03" type="text" value="0" />
<input name="ctl04" type="text" value="1" />
<input name="ctl05" type="text" value="2" />
<input name="ctl06" type="text" value="3" />
<input name="ctl07" type="text" value="4" />
<input name="ctl08" type="text" value="5" />
<input name="ctl09" type="text" value="6" />
<input name="ctl10" type="text" value="7" />
<input name="ctl11" type="text" value="8" />
<input name="ctl12" type="text" value="9" />
<input name="ctl13" type="text" value="10" />
</span>

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

Show quote
> I need to create a control that contains two child collections and
> renders one of them depending on a condition. How can I make it parse
> "true" and "false" nested tags? I cannot use the standard MultiView
> control as I do need these "true" and "false" tags.
>
> <MyControl runat="server">
> <true>
> <!-- Some child controls that are visible when the condition
> is true -
> ->
> </true>
> <false>
> <!-- Some child controls that are visible when the condition
> is false
> -->
> </false>
> </MyControl>

AddThis Social Bookmark Button