opensubscriber
   Find in this group all groups
 
Unknown more information…

d : dotnet-web@discuss.develop.com 3 April 2008 • 5:30PM -0400

[DOTNET-WEB] Bubbling events on a server side control
by Dean Cleaver

REPLY TO AUTHOR
 
REPLY TO GROUP



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=\"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=\"detailBlock\">");
            else
                writer.WriteLine("<div class=\"detailBlockError\">");

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

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

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

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

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

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

        [
         Description("The Details Fields ValidationGroup"),
         Category("Custom Properties"),
        ]
        public string ValidationGroup
        {
            get { return this.TextBox.ValidationGroup; }
            set
                {
                        this.TextBox.ValidationGroup = value;
                        this.DropDownList.ValidationGroup = 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 = value; }
        }

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

        public event EventHandler SelectedIndexChanged;

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

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

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

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

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

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

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.