Tags: | Categories: Blog Posted by admin on 3/9/2009 5:54 PM | Comments (5)

Lately I just want to put skeletons of classes I use the most so I got a repository of code at my fingertips.

 

Creating a control for your toolbox in ASP.NET 2.0 is very easy using the new class CompositeControl from C# 2.0. Well VB and J# also have that class, but I just try very hard to forget those 2 languages exist.

 

CompositeControl is very powerful, you can create a control using existing controls. For example you can create a new control using the TreeView and a Label you create a control like the Microsoft search control. You can put a dropdown and a textbox to create a better dropdown.

 

This is the skeleton for using CompositeControl to create a checked treeview with a label as Microsoft msdn menus.

 

[DefaultProperty("Text")]

    [ToolboxData("<{0}:DropDown1 runat=\"server\"> </{0}:DropDown1>")]

    public class DropDown1 : CompositeControl

    {

        private TreeView m_TreeView;

        private Label m_Label;

        private ArrayList aItems = new ArrayList();

        private ArrayList aSelectedItems = new ArrayList();

       

        [Bindable(true)]

        [Category("Appearance")]

        [DefaultValue("Extending the DropDown control")]

        [Localizable(true)]

        public virtual String Title

        {

            get

            {

                if (ViewState["Title"] == null)

                    return ("No Title");

 

                return ((string)ViewState["Title"]);

            }

            set

            {

                ViewState["Title"] = value;

            }

        }

 

        public string Children

        {

            get

            {

                if (ViewState["Children"] == null)

                    return ("");

 

                return ((string)ViewState["Children"]);

            }

            set

            {

                ViewState["Children"] = value;

            }

        }

 

        [Bindable(true)]

        [Category("Appearance")]

        [DefaultValue("Extending the DropDown control")]

        [Localizable(true)]

        public void Items(ArrayList aList)

        {

            aItems = aList;

            for (int i = 0; i < aItems.Count; i++)

            {

                TreeNode oNode = new TreeNode(aItems[i].ToString());

                m_TreeView.Nodes.Add(oNode);

            }

        }

 

        public ArrayList GetSelectedItems()

        {

            return (aSelectedItems);

        }

 

        protected override void CreateChildControls()

        {

            m_TreeView = new TreeView();

            m_TreeView.ExpandImageUrl = "~/images/dropdown.gif";

            m_TreeView.CollapseImageUrl = "~/images/dropdown.gif";

            m_TreeView.ShowCheckBoxes = TreeNodeTypes.Leaf;

            //m_TreeView.NodeIndent = 0;

            m_TreeView.ForeColor = Color.Black;

            m_TreeView.TreeNodeCheckChanged += new TreeNodeEventHandler(m_TreeView_TreeNodeCheckChanged);

            m_TreeView.TreeNodeCollapsed += new TreeNodeEventHandler(m_TreeView_TreeNodeCollapsed);

           

            m_Label = new Label();

 

            TreeNode oNode = new TreeNode(Title);

            m_TreeView.Nodes.Add(oNode);

            if ( Children.Length > 0 )

            {

                ArrayList aChildren = GetChildren(Children);

                for (int t = 0; t < aChildren.Count; t++)

                {

                    TreeNode oNodeSub = new TreeNode(aChildren[t].ToString());

                    oNode.ChildNodes.Add(oNodeSub);

                }

            }

 

            this.Controls.Add(m_TreeView);

            this.Controls.Add(m_Label);

 

            //base.CreateChildControls();

        }

 

        private ArrayList GetChildren(string sChild)

        {

            ArrayList oArray = new ArrayList();

 

            while (sChild.IndexOf(",") != -1)

            {

                int len = sChild.IndexOf(",");

                string sTemp = sChild.Substring(0, len);

                oArray.Add(sTemp);

                sChild = sChild.Substring(len + 1);

            }

 

            oArray.Add(sChild);

 

            return (oArray);

        }

 

        protected override void OnPreRender(EventArgs e)

        {

            base.OnPreRender(e);

            /*if (Page != null)

                Page.RegisterRequiresPostBack(this);

             * */

        }

 

        [

        Category("Action"),

        Description("Raised when the user checks.")

        ]

        protected void m_TreeView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)

        {

            BuildLabel(m_TreeView, m_Label);

        }

 

        [

        Category("Action"),

        Description("Raised when the user collapses the treeview.")

        ]

        protected void m_TreeView_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)

        {

            BuildLabel(m_TreeView, m_Label);

        }

 

        private void BuildLabel(TreeView oView, Label oLabel)

        {

            aSelectedItems.Clear();

            oLabel.Text = "";

 

            for (int i = 0; i < oView.Nodes.Count; i++)

            {

                for (int j = 0; j < oView.Nodes[i].ChildNodes.Count; j++)

                {

                    if (oView.Nodes[i].ChildNodes[j].Checked == true)

                    {

                        if (oLabel.Text.Length > 0)

                            oLabel.Text += ",";

 

                        oLabel.Text += oView.Nodes[i].ChildNodes[j].Text;

                        aSelectedItems.Add(oView.Nodes[i].ChildNodes[j].Text);

                    }

                }

            }

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            AddAttributesToRender(writer);

 

            writer.AddAttribute(

                HtmlTextWriterAttribute.Cellpadding,

                "1", false);

            writer.RenderBeginTag(HtmlTextWriterTag.Table);

 

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            m_TreeView.RenderControl(writer);

            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            m_Label.RenderControl(writer);

            writer.RenderEndTag();           

            writer.RenderEndTag();

                       

            writer.RenderEndTag();

        }

 

    }

blog comments powered by Disqus