Composite Server Control and DropDdown list events not firing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonyduckett
    New Member
    • Apr 2010
    • 1

    Composite Server Control and DropDdown list events not firing

    Hi

    I am trying to build a composite server control that allows the user to embed their own markup and hookup their own events. The control fires up a highly stylised modal popup. Anyone using the control could do the following:

    Code:
    <cc2:Popup ID="Popup_CreateContent" runat="server"  IconClass="ui_question" 
        CssClass="ui_fullscreenloader" Title="Manage Content" Visible="false" DialogWidth="ui_ModalWide">
        <PopupControls>
            <uc2:CreateContent ID="ctlCreateContent" runat="server" />
        </PopupControls>
    </cc2:Popup>
    Then in the user control they can embed pretty much whatever they like and hook up their own events.

    Code:
    <table>
        <tr><td><asp:Literal id="lblContentCode" runat="server" Text="Content Code" /></td>
            <td><asp:TextBox ID="txtContentCode" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Literal id="LtlContentType" runat="server" Text="Quick Code" /></td>
            <td><asp:DropDownList ID="ddlContentType" runat="server" AutoPostBack="true" 
                    onselectedindexchanged="onContentCode_SelectedIndexChanged">
                    </asp:DropDownList></td>
        </tr>
        <tr><td colspan="2">
            <span class="ui_buttonAltSml"><asp:Button id="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" /></span>
            <span class="ui_buttonAltSml"><asp:Button id="btnClose" runat="server" OnClick="btnClose_Click" Text="Close" /></span>            
        </td>
        </tr>
    </table>
    The code for the Server Control is below

    Code:
        [DefaultProperty("Title")]
        [ToolboxData("<{0}:Popup runat=server></{0}:TroposPopup>")]
        [ParseChildren(true)]
        public class Popup : CompositeControl, IPostBackDataHandler
        {
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                controls.Clear();
                foreach (Control c in this.PopupControls)
                {
                    if (c.HasControls()) RecurseControls(c);
                }
                if (Page != null) Page.RegisterRequiresPostBack(this);
            }
    
            public override ControlCollection Controls
            {
                get
                {
                    EnsureChildControls();
                    return base.Controls;
                }
            }
    
            public enum OperationalWidth
            {
                ui_ModalWide,
                ui_Modal
            }
            [
            Category("Appearance"),
            DefaultValue("ui_Modal"),
            Description("Specifies the width styling of the popup."),
            NotifyParentProperty(true),
            ]
            public OperationalWidth DialogWidth
            {
                get
                {
                    OperationalWidth w = (OperationalWidth)ViewState["DialogWidth"];
                    return w;
                }
    
                set
                {
                    ViewState["DialogWidth"] = value;
                }
            }
    
            /// <summary>
            /// alternatives ui_error, ui_warning, ui_question, ui_info - default is ui_info
            /// </summary>
            /// 
            public enum IconType
            {
                ui_error,
                ui_warning,
                ui_question,
                ui_info
            }
    
            [
            Category("Behavior"),
            DefaultValue("ui_info"),
            Description("The icon type for the popup."),
            NotifyParentProperty(true),
            ]
            public IconType IconClass
            {
                get
                {
                    IconType s = (IconType)ViewState["IconClass"];
                    return s;
                }
    
                set
                {
                    ViewState["IconClass"] = value;
                }
            }
            /// <summary>
            /// The title on the dialog
            /// </summary>
            [
            Category("Behavior"),
            DefaultValue("Tropos"),
            Description("The title bar text of the popup."),
            NotifyParentProperty(true),
            ]
            public string Title
            {
                get
                {
                    String s = (String)ViewState["Title"];
                    return ((s == null) ? "" : s);
                }
    
                set
                {
                    ViewState["Title"] = value;
                }
            }
    
            [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
            public List<Control> PopupControls
            {
                get
                {
                    List<Control> c = (List<Control>)ViewState["ControlsList"];
                    return ((c == null) ? new List<Control>() : c);
                }
    
                set
                {
                    ViewState["ControlsList"] = value;
                }
            }
    
            private List<Control> controls = new List<Control>();
    
            protected override void CreateChildControls()
            {
    
                Controls.Clear();
                ClearChildViewState();
    
                //Wrapper
                HtmlGenericControl ui_ModalWide = new HtmlGenericControl("DIV");
                ui_ModalWide.ID = "ui_ModalWide";
                ui_ModalWide.Attributes.Add("class", DialogWidth.ToString());
    
                HtmlGenericControl ui_modallgt = new HtmlGenericControl("DIV");
                ui_modallgt.Attributes.Add("class", "ui_modallgt ui_formdefault");
    
                HtmlGenericControl ui_modal_wrapper = new HtmlGenericControl("DIV");
                ui_modal_wrapper.Attributes.Add("class", "ui_modal_wrapper");
    
                HtmlGenericControl ui_modalContent = new HtmlGenericControl("DIV");
                ui_modalContent.Attributes.Add("class", "ui_modalContent tropos");
    
                HtmlGenericControl ui_ = new HtmlGenericControl("DIV");
                ui_.Attributes.Add("class", IconClass.ToString());
    
    
                //Dialog Title
                HtmlGenericControl h2 = new HtmlGenericControl("H2");
                h2.InnerText = Title;
                ui_.Controls.Add(h2);
    
    
                //Controls
                HtmlGenericControl ui_modalaction = new HtmlGenericControl("DIV");
                ui_modalaction.Attributes.Add("class", "ui_modalaction");
    
                foreach (Control c in controls)
                {
                    ui_modalaction.Controls.Add(c);
                }
    
                //End Divs for rounded corners
                HtmlGenericControl ui_modal_wrapperEnd = new HtmlGenericControl("DIV");
                ui_modal_wrapperEnd.Attributes.Add("class", "ui_modal_wrapperEnd");
    
                HtmlGenericControl ui_modalContentEnd = new HtmlGenericControl("DIV");
                ui_modalContentEnd.Attributes.Add("class", "ui_modalContentEnd");
    
                ui_modallgt.Controls.Add(ui_modal_wrapper);
                ui_modal_wrapper.Controls.Add(ui_modalContent);
    
                ui_modal_wrapperEnd.Controls.Add(ui_modalContentEnd);
    
                ui_modallgt.Controls.Add(ui_modal_wrapperEnd);
                ui_ModalWide.Controls.Add(ui_modallgt);
    
                ui_modalContent.Controls.Add(ui_);
                ui_modalContent.Controls.Add(ui_modalaction);
    
                Controls.Add(ui_ModalWide);
    
            }
    
            private void RecurseControls(Control c)
            {
                foreach (Control _c in c.Controls)
                {
                    if (_c.HasControls()) RecurseControls(_c);
                    controls.Add(_c);
                }
            }
    
            protected override bool OnBubbleEvent(object source, EventArgs args)
            {
                return base.OnBubbleEvent(source, args);
            }
    
    
            public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                return true;
    
            }
    
            public void RaisePostDataChangedEvent()
            {
              
            }       
        }
    With buttons the postbacks work fine, but with the dropdown in the user control the postback is lost and the control state los as well.

    I need the end user to be able to implement whatever markup they need to - how can I achieve this?

    TIA

    Tony
Working...