// RenderAsGroup returns a RadioButtonGroup object if the group should be
        // rendered in a single <select> statement, or null if autopostback should
        // be enabled.
        //
        private RadioButtonGroup RenderAsGroup(RadioButton r)
        {
            bool startedSequence  = false;
            bool finishedSequence = false;

            RadioButtonGroup group = new RadioButtonGroup();

            //
            foreach (Control c in r.Parent.Controls)
            {
                RadioButton    radioSibling   = c as RadioButton;
                LiteralControl literalSibling = c as LiteralControl;
                if (radioSibling != null && radioSibling.UniqueGroupName == r.UniqueGroupName)
                {
                    startedSequence = true;
                    group.ButtonsInGroup++;

                    if (radioSibling.Checked == true)
                    {
                        group.SelectedButton = radioSibling.UniqueID;
                    }
                    if (finishedSequence || !radioSibling.Enabled)
                    {
                        group.ButtonsInGroup = -1; // can't be rendered in a group
                        break;
                    }
                }
                else if (startedSequence && (literalSibling == null || !IsWhiteSpace(literalSibling.Text)))
                {
                    finishedSequence = true;
                }
            }
            return(group);
        }
        // RenderAsGroup returns a RadioButtonGroup object if the group should be 
        // rendered in a single <select> statement, or null if autopostback should
        // be enabled.
        // 
        private RadioButtonGroup RenderAsGroup(RadioButton r) {
            bool startedSequence = false;
            bool finishedSequence = false;

            RadioButtonGroup group = new RadioButtonGroup();

            //
            foreach (Control c in r.Parent.Controls) {
                RadioButton radioSibling = c as RadioButton;
                LiteralControl literalSibling = c as LiteralControl;
                if (radioSibling != null && radioSibling.UniqueGroupName == r.UniqueGroupName) {
                    startedSequence = true;
                    group.ButtonsInGroup++;

                    if (radioSibling.Checked == true) {
                        group.SelectedButton = radioSibling.UniqueID;
                    }
                    if (finishedSequence || !radioSibling.Enabled) {
                        group.ButtonsInGroup = -1; // can't be rendered in a group
                        break;
                    }
                }
                else if (startedSequence && (literalSibling == null || !IsWhiteSpace(literalSibling.Text))) {
                    finishedSequence = true;
                }
            }
            return group;
        }
        // Renders the control.
        protected internal override void Render(HtmlTextWriter markupWriter)
        {
            WmlTextWriter writer = (WmlTextWriter)markupWriter;

            DetermineGroup(Control);
            RadioButtonGroup group = GetGroupByName(Control.UniqueGroupName);

            if (group != null)
            {
                if (!group.RegisteredGroup)
                {
                    // GroupFormVariable is passed as the name & value becuase it is both the key
                    // for the postback data, and the WML client side var used to
                    // select an item in the list.
                    ((WmlPageAdapter)PageAdapter).RegisterPostField(writer, GroupFormVariable, GroupFormVariable, true /*dynamic field*/, false /*random*/);
                    group.RegisteredGroup = true;
                }
                if (group.RenderAsGroup)
                {
                    // Render opening select if not already opened
                    if (!group.RenderedSelect)
                    {
                        if (group.SelectedButton != null)
                        {
                            ((WmlPageAdapter)PageAdapter).AddFormVariable(writer, GroupFormVariable, group.SelectedButton, false /*random*/);
                        }
                        writer.WriteBeginSelect(GroupFormVariable, group.SelectedButton, null /*iname */,
                                                null /*ivalue*/, Control.ToolTip, false /*multiple */);
                        if (!writer.AnalyzeMode)
                        {
                            group.RenderedSelect = true;
                        }
                    }

                    // Render option
                    // We don't do autopostback if the radio button has been selected.
                    // This is to make it consistent that it only posts back if its
                    // state has been changed.  Also, it avoids the problem of missing
                    // validation since the data changed event would not be fired if the
                    // selected radio button was posting back.
                    if (Control.AutoPostBack && !Control.Checked)
                    {
                        ((WmlPageAdapter)PageAdapter).RenderSelectOptionAsAutoPostBack(writer, Control.Text, Control.UniqueID);
                    }
                    else
                    {
                        ((WmlPageAdapter)PageAdapter).RenderSelectOption(writer, Control.Text, Control.UniqueID);
                    }

                    // Close, if list is finished
                    if (!writer.AnalyzeMode && --group.ButtonsInGroup == 0)
                    {
                        writer.WriteEndSelect();
                    }
                }
                // must render as autopostback, radio buttons not in consecutive group.
                else
                {
                    if (!Control.Enabled)
                    {
                        RenderDisabled(writer);
                        return;
                    }

                    string iname  = Control.Checked ? Control.ClientID : null;
                    string ivalue = Control.Checked ? "1" : null;
                    if (ivalue != null)
                    {
                        ((WmlPageAdapter)PageAdapter).AddFormVariable(writer, iname, ivalue, false /*random*/);
                    }

                    writer.WriteBeginSelect(null, null, iname, ivalue, Control.ToolTip, true /*multiple*/);
                    if (!Control.Checked)
                    {
                        ((WmlPageAdapter)PageAdapter).RenderSelectOptionAsAutoPostBack(writer, Control.Text, GroupFormVariable, Control.UniqueID);
                    }
                    else
                    {
                        ((WmlPageAdapter)PageAdapter).RenderSelectOption(writer, Control.Text, Control.UniqueID);
                    }
                    writer.WriteEndSelect();
                }
            }
        }