locked
CompositeControl to CompositeDataBoundControl RRS feed

  • Question

  • User192013029 posted

    i created a custom composite control a few years ago that worked then and still does. hadn't changed it at all in that time until this week. decided i wanted to try implementing the xml file i currently use as input (set via property) as an xmldatasource instead. the first thing i did was change the derived class from CompositeControl to CompositeDataBoundControl resulting in being prompted by the editor to implement the CreateChildControls abstract method which in this case is the version that returns an int and passes in an IEnumerable. i previously had implemented the CCC that returned and passed in nothing.

    the problem is, once the databound CCC was added and the code implemented, it was never called. the old version was still being called which of course does no good since it doesn't give me an IEnumerable.

    any ideas on what i might be missing here?

    Wednesday, June 22, 2011 8:41 AM

All replies

  • User192013029 posted

    ..

    Thursday, June 23, 2011 11:14 AM
  • User3866881 posted

    i created a custom composite control a few years ago that worked then and still does. hadn't changed it at all in that time until this week. decided i wanted to try implementing the xml file i currently use as input (set via property) as an xmldatasource instead. the first thing i did was change the derived class from CompositeControl to CompositeDataBoundControl resulting in being prompted by the editor to implement the CreateChildControls abstract method which in this case is the version that returns an int and passes in an IEnumerable. i previously had implemented the CCC that returned and passed in nothing.

    the problem is, once the databound CCC was added and the code implemented, it was never called. the old version was still being called which of course does no good since it doesn't give me an IEnumerable.

    any ideas on what i might be missing here?

    Hello :)

    Wolud you mind showing your codes for us to see?

    Thursday, June 23, 2011 10:22 PM
  • User192013029 posted

    original CompositeControl..

    
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design;
    using System.IO;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Diagnostics;
    using System.Collections.Specialized;
    
    namespace MyXMLViewer2Control
    {
        public enum VIEWSTATE
        {
            TEXT = 0,
            TREE = 1
        }
    
        [DefaultProperty("XmlFilePath")]
        [ToolboxData("<{0}:MyXMLViewer2 runat=server></{0}:MyXMLViewer2>")]
        //[Designer(typeof(MyXMLDesigner))]
        public class MyXMLViewer2 : CompositeControl, INamingContainer
        {
            Button btnSwapView;
            Button btnSaveXML;
            TextBox txtTextView;
            Xml eXml;
            Label label;
            string dummy = @"<?xml version=""1.0"" encoding=""utf-8"" ?>" + "\n<Dummy></Dummy>";
    
            public delegate void SaveXmlEventHandler(object sender, SaveXmlEventArgs e);
            private static readonly object SaveXmlEventKey = new object();
    
            public event SaveXmlEventHandler SaveXml
            {
                add { Events.AddHandler(SaveXmlEventKey, value); }
                remove { Events.RemoveHandler(SaveXmlEventKey, value); }
            }
    
            protected virtual void OnSaveXml(SaveXmlEventArgs e)
            {
                SaveXmlEventHandler handler = Events[SaveXmlEventKey] as SaveXmlEventHandler;
    
                if (handler != null)
                    handler(this, e);
            }
    
            protected override void OnLoad(EventArgs e)
            {
                //xmlds = new XmlDataSource();
                //xmlds.DataFile = XmlFilePath;
                //xmlds.ID = "xmlds";
                //xmlds.EnableCaching = true;
                //xmlds.EnableViewState = true;
    
                //this.DataSourceID = xmlds.ID;
                //this.DataBind(true);
            }
    
            protected override void OnInit(EventArgs e)
            {
    
            }
    
            protected override bool OnBubbleEvent(object source, EventArgs args)
            {
                bool handled = false;
    
                CommandEventArgs ce = args as CommandEventArgs;
                if (ce != null && ce.CommandName == "SwapView")
                {
                    Debug.WriteLine("In SwapView handler");
                    CurViewState = (CurViewState == VIEWSTATE.TEXT) ? VIEWSTATE.TREE : VIEWSTATE.TEXT;
                    //LoadXMLFile();
                    handled = true;
                }
                else if (ce != null && ce.CommandName == "SaveXml")
                {
                    OnSaveXml(new SaveXmlEventArgs(TextBoxText));
                    handled = true;
                }
    
                return handled;
            }
    
            #region Properties
    
            [Bindable(true)]
            [Category("General")]
            [Localizable(true)]
            [DefaultValue("")]
            [EditorAttribute(typeof(XmlUrlEditor), typeof(UITypeEditor))]
            public virtual string XmlFilePath
            {
                get
                {
                    Debug.WriteLine("In get XmlFilePath");
                    string xmlFilePath = (string)ViewState["XmlFilePath"];
                    return ((string.IsNullOrEmpty(xmlFilePath)) ? string.Empty : xmlFilePath);
                }
    
                set
                {
                    Debug.WriteLine("In set XmlFilePath");
                    ViewState["XmlFilePath"] = value;
                    if (value == string.Empty)
                        this.FileSelected = false;
                    else
                        this.FileSelected = true;
                }
            }
            //protected string xmlFilePath;
    
            [Browsable(false)]
            [DefaultValue(VIEWSTATE.TEXT)]
            public virtual VIEWSTATE CurViewState
            {
                get
                {
                    if (ViewState["CurViewState"] != null)
                    {
                        VIEWSTATE curViewState = (VIEWSTATE)ViewState["CurViewState"];
                        Debug.WriteLine("In get CurViewState: " + curViewState);
                        return curViewState;
                    }
                    else
                    {
                        Debug.WriteLine("In get CurViewState: " + VIEWSTATE.TEXT.ToString());
                        return VIEWSTATE.TEXT;
                    }
                }
                set
                {
                    Debug.WriteLine("In set CurViewState: " + value.ToString());
                    ViewState["CurViewState"] = value;
                }
            }
            //protected VIEWSTATE curViewState;
    
            [Browsable(false)]
            [DefaultValue(false)]
            public virtual bool FileSelected
            {
                get;
                set;
             }
    
            [Browsable(false)]
            public virtual string TextBoxText
            {
                get
                {
                    return txtTextView.Text;
                }
            }
    
            #endregion
    
            #region Control Style
    
            protected override Style CreateControlStyle()
            {
                return new TableStyle(ViewState);
            }
    
            public virtual GridLines GridLines
            {
                get { return ((TableStyle)ControlStyle).GridLines; }
                set { ((TableStyle)ControlStyle).GridLines = value; }
            }
    
            public virtual int CellSpacing
            {
                get { return ((TableStyle)ControlStyle).CellSpacing; }
                set { ((TableStyle)ControlStyle).CellSpacing = value; }
            }
    
            public virtual int CellPadding
            {
                get { return ((TableStyle)ControlStyle).CellPadding; }
                set { ((TableStyle)ControlStyle).CellPadding = value; }
            }
    
            public virtual HorizontalAlign HorizontalAlign
            {
                get { return ((TableStyle)ControlStyle).HorizontalAlign; }
                set { ((TableStyle)ControlStyle).HorizontalAlign = value; }
            }
    
            #endregion
    
            private void LoadTextView()
            {
                Debug.WriteLine("In LoadTextView");
                if (FileSelected && !base.DesignMode)
                {
                    string path = base.MapPathSecure(XmlFilePath);
                    using (StreamReader sr = File.OpenText(path))
                    {
                        string s = string.Empty;
                        while ((s = sr.ReadLine()) != null)
                            txtTextView.Text += s + "\n";
                    }
                }
                else if (!base.DesignMode)
                    txtTextView.Text = dummy;
            }
    
            private void LoadTreeView()
            {
                Debug.WriteLine("In LoadTreeView");
                if (FileSelected && !base.DesignMode)
                {
                    string path = base.MapPathSecure(XmlFilePath);
                    eXml.DocumentSource = path;
                }
                else if (!base.DesignMode)
                    eXml.DocumentContent = dummy;
            }
    
            protected override HtmlTextWriterTag TagKey
            {
                get { return HtmlTextWriterTag.Table; }
            }
    
            protected override void CreateChildControls()
            {
                //Debug.WriteLine("In CreateChildControls");
                Controls.Clear();
    
                btnSwapView = new Button();
                btnSwapView.Width = Unit.Percentage(100);
                btnSwapView.Height = Unit.Pixel(25);
                btnSwapView.CommandName = "SwapView";
                btnSwapView.ID = "SwapViewButton";
                btnSwapView.Text = "Swap View";
                btnSwapView.ForeColor = Color.Maroon;
    
                btnSaveXML = new Button();
                btnSaveXML.Width = Unit.Percentage(100);
                btnSaveXML.Height = Unit.Pixel(25);
                btnSaveXML.CommandName = "SaveXml";
                btnSaveXML.ID = "SaveXMLButton";
                btnSaveXML.Text = "Save XML";
                btnSaveXML.ForeColor = Color.Maroon;
    
                txtTextView = new TextBox();
                txtTextView.TextMode = TextBoxMode.MultiLine;
                txtTextView.Wrap = true;
                //txtTextView.Width = new Unit(this.Width.Value - this.BorderWidth.Value);
                txtTextView.Width = Unit.Percentage(98);    // make fit within border
                txtTextView.Height = Unit.Percentage(100);
                txtTextView.ID = "TextViewTextbox";
                txtTextView.Font.Size = FontUnit.Medium;
                txtTextView.Text = string.Empty;
                txtTextView.ReadOnly = true;
                LoadTextView();
    
                eXml = new Xml();
                eXml.TransformSource = @"~/defaultss.xslt";
                eXml.ID = "TreeViewBrowser";
                LoadTreeView();
    
                label = new Label();
                label.Width = Unit.Percentage(100);
                label.Height = Unit.Pixel(25);
                label.ID = "MsgLabel";
                label.Font.Size = FontUnit.Small;
                label.Font.Name = "Tahoma";
                label.Text = "XML file must be selected in design mode";
    
                this.Controls.Add(btnSwapView);
                this.Controls.Add(btnSaveXML);
                this.Controls.Add(txtTextView);
                this.Controls.Add(eXml);
                this.Controls.Add(label);
    
                ChildControlsCreated = true;
                base.CreateChildControls();
            }
    
            //protected override void Render(HtmlTextWriter writer)
            protected override void RenderContents(HtmlTextWriter writer)
            {
                Debug.WriteLine("In RenderContents");
                //AddAttributesToRender(writer);    // need if using Render
    
                //writer.RenderBeginTag(HtmlTextWriterTag.Table);   // need if using Render (end tag as well)
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
                writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "color: Black; background-color: White; font-weight: normal;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                //    <td align="left" valign="middle" style="color: Black; background-color: White; font-weight: bold;">
                btnSwapView.RenderControl(writer);
                writer.RenderEndTag();  // td
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
                writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "color: Black; background-color: White; font-weight: normal;");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);    // new td
                //    <td align="right" valign="middle" style="color: Black; background-color: White; font-weight: bold;">
                btnSaveXML.RenderControl(writer);
                writer.RenderEndTag();  // td
                writer.RenderEndTag();  // tr
    
                if (CurViewState == VIEWSTATE.TEXT)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                    writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "color: Black; background-color: White;font-weight: normal; height: 100%;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    <td align="center" valign="middle" colspan="2" style="color: Black; background-color: White;
                    //        font-weight: bold; height: 100%; width: 100%;">
                    txtTextView.RenderControl(writer);
                    writer.RenderEndTag();  // td
                    writer.RenderEndTag();  // tr
                }
                else
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
                    writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "color:Black; background-color: White;font-weight: normal; height: 100%;width:100%");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //<td align="left" valign="middle" colspan="2" style="color:Black;background-color:White;font-weight:normal;height:100%;width:100%;">
                    eXml.RenderControl(writer);
                    writer.RenderEndTag();  // tr
                    writer.RenderEndTag();  // td
                }
    
                if (!FileSelected)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                    writer.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "color:Black;background-color:White;font-weight:bold;height:25px;width:100%;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //  <td align="center" valign="middle" colspan="2" style="color:Black;background-color:White;font-weight:bold;height:25px;width:100%;">
                    label.RenderControl(writer);
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
                //writer.RenderEndTag();
            }
    
            #region IPostBackDataHandler Members
    
            public bool LoadPostData(string postDataKey, NameValueCollection values)
            {
                bool fileSelectedChanged = false;
                
                string fileSelected = values["FileSelected"];
                if (fileSelected != FileSelected.ToString())
                {
                    FileSelected = bool.Parse(fileSelected);
                    fileSelectedChanged = true;
                }
    
                return fileSelectedChanged;
            }
    
            public void RaisePostDataChangedEvent()
            {
                throw new NotImplementedException();
            }
    
            #endregion
    
            #region IPostBackEventHandler Members
    
            public void RaisePostBackEvent(string eventArgument)
            {
                throw new NotImplementedException();
            }
    
            #endregion
    
        }
    }
    
    Saturday, June 25, 2011 12:38 PM
  • User1957418580 posted

    Have you tried just creating a whole new control to be sure the new one is loaded in the process?  Maybe just build MyDBXMLViewer? 

    Sunday, June 26, 2011 7:19 AM
  • User192013029 posted

    i tried creating a brand new class from scratch where i derived from CompositeDataBoundControl from the start, implemented the IEnumerable CCC and cut and paste the rest of the code (except for the standard CCC) into the new class but the standard CCC was still being executed. this makes no sense to me. every sample i've seen, the IEnumerable CCC is always implemented and does get executed. i don't know.. could it be that the datasource isn't set yet? my understanding is that a collection of 5 null objects gets sent by default if it's not already set. am i wrong on that?

    Sunday, June 26, 2011 10:09 AM
  • User192013029 posted

    i should clarify by saying that MyXmlViewer2 is a class library project that contains both my original control and the new class i created. the new one as i said is exactly the same as the original except for the name (CompositeDB) and the derivation (CDBC). that one doesn't work either. is there something about the project maybe?

    Sunday, June 26, 2011 11:53 AM