Bubbling events on a server side control

Dean Cleaver <[email protected]> Thu, 3 Apr 2008 22:30:20 +1300
Newsgroups gmane.comp.windows.devel.dotnet.web
Message-ID <4543A775711185429E59B18055D416742322E2@sxslakl004.xceptionsoftware.com>
I've tried to create a control that can be either a TextBox or a
DropDownList, but am having trouble "bubbling" the SelectedIndexChanged
event for the DropDownList (within an UpdatePanel if that makes any
difference). My code is like this (snipped a bit for brevity) - is there
something obvious I am missing?

Dino



using System;
using System.ComponentModel;
using System.Data;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

namespace Client.Project.WebControls
{
    [
    DefaultEvent("SelectedIndexChanged"),
    DefaultProperty("Text"),
    ToolboxData("<{0}:DetailInputBox runat=3D\"server\">
</{0}:DetailInputBox>"),
    ]
    public class DetailInputBox : WebControl, IPostBackEventHandler
    {
        protected override void OnInit(EventArgs e)
        {
            Controls.Clear();

            Controls.Add(this.TextBox);
            Controls.Add(this.DropDownList);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.IsValid || this.ReadOnly)
                writer.WriteLine("<div class=3D\"detailBlock\">");
            else
                writer.WriteLine("<div class=3D\"detailBlockError\">");

            writer.WriteLine("    <div class=3D\"detailInput\">");

            if (this.DropDownList.Items.Count > 0)
                {
                  this.DropDownList.SelectedIndexChanged +=3D
DropDownList_SelectedIndexChanged;
                        this.DropDownList.RenderControl(writer);
                }=09
            else
                this.TextBox.RenderControl(writer);

            writer.WriteLine("    </div>");
            writer.WriteLine("    <div class=3D\"clear\"></div>");
            writer.WriteLine("</div>");
        }

        [Browsable(false),]
        private TextBox textBox =3D null;
        /// <summary>
        /// Gets or sets the required text box validator.
        /// </summary>
        /// <value>The required text box validator.</value>
        private TextBox TextBox
        {
            get
            {
                if (textBox =3D=3D null)
                {
                    textBox =3D new TextBox();
                    textBox.ID =3D this.ID + "Text";
                }
                return textBox;
            }
        }

        [Browsable(false),]
        private DropDownList dropDownList =3D null;
        /// <summary>
        /// Gets or sets the required text box validator.
        /// </summary>
        /// <value>The required text box validator.</value>
        public DropDownList DropDownList
        {
            get
            {
                if (dropDownList =3D=3D null)
                {
                    dropDownList =3D new DropDownList();
                    dropDownList.ID =3D this.ID + "List";
                }
                return dropDownList;
            }
        }

        private bool isValid =3D true;
        [Browsable(false)]
        public bool IsValid
        {
            get { return this.isValid; }
            set { this.isValid =3D value; }
        }

        [
         Description("The Details Fields ValidationGroup"),
         Category("Custom Properties"),
        ]
        public string ValidationGroup
        {
            get { return this.TextBox.ValidationGroup; }
            set=20
                {=20
                        this.TextBox.ValidationGroup =3D value;
                        this.DropDownList.ValidationGroup =3D value;
                }
        }

        public ListItemCollection Items
        {
            get { return this.DropDownList.Items; }
        }

        public string SelectedValue
        {
            get { return this.DropDownList.SelectedValue; }
        }

        public int SelectedIndex
        {
            get { return this.DropDownList.SelectedIndex; }
            set { this.DropDownList.SelectedIndex =3D value; }
        }

        void DropDownList_SelectedIndexChanged(object sender, EventArgs
e)
        {
            this.OnSelectedIndexChanged(e);
        }

        public event EventHandler SelectedIndexChanged;

        protected virtual void OnSelectedIndexChanged(EventArgs args)
        {
            EventHandler thisEvent =3D this.SelectedIndexChanged;

            if (thisEvent !=3D null)
                thisEvent(this, args);
        }

        public bool AutoPostBack
        {
            get { return this.DropDownList.AutoPostBack; }
            set=20
            {
                this.TextBox.AutoPostBack =3D value;
                this.DropDownList.AutoPostBack =3D value;
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            this.OnSelectedIndexChanged(EventArgs.Empty);
        }
    }
}

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This list is hosted by DevelopMentor=AE  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com