Click here to Skip to main content
15,895,779 members
Articles / Web Development / ASP.NET

Source Code for JQuery ASP.NET Controls

Rate me:
Please Sign up or sign in to vote.
4.56/5 (15 votes)
10 Jun 2009CPOL 67.6K   3.7K   93  
Get a start to building your own JQuery Controls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web;
using System.Drawing.Design;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Security.Permissions;
using Mullivan.Web.UI.Design.WebControls;

namespace Mullivan.Web.UI.WebControls
{

    [ControlBuilder(typeof(ControlBuilder)),
   Designer(typeof(JQueryAccordionSectionDesigner)),
   ToolboxItem(false),
   ParseChildren(true), PersistChildren(false),
   AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class JQueryAccordionSection : Control
    {
        // Fields
        private ITemplate _contentTemplate;
        private JQueryAccordion _owner;


        public JQueryAccordionSection()
            : base()
        {
            this.Name = "New Section";
        }

        [Browsable(false)]
        public JQueryAccordion Owner
        {
            get
            {
                return _owner;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Position
        {
            get
            {
                if (this._owner != null && this._owner.Sections != null)
                    return this._owner.Sections.IndexOf(this);
                return -1;
            }
        }


        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsActive
        {
            get
            {
                int idx = this.Position;
                if (idx != -1 && idx == _owner.ActiveSectionIndex)
                    return true;
                return false;
            }
            set
            {
                if (_owner != null && value)
                {
                    int idx = _owner.Sections.IndexOf(this);
                    if (idx > -1)
                        _owner.ActiveSectionIndex = idx;
                }
            }
        }

        [MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        TemplateInstance(TemplateInstance.Single),
        Browsable(false)]
        public ITemplate ContentTemplate
        {
            get
            {
                return this._contentTemplate;
            }
            set
            {
                this._contentTemplate = value;
            }
        }

        [Category("Appearance"), DefaultValue("")]
        public string Name
        {
            get
            {
                return (this.ViewState["SectionName"] != null ? this.ViewState["SectionName"].ToString() : string.Empty);
            }
            set
            {
                this.ViewState["SectionName"] = value;
            }
        }


        [DefaultValue(""),
        Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
            typeof(UITypeEditor)),
        UrlProperty,
        Bindable(true),
        Category("Image"), DisplayName("Url")]
        public string ImageUrl
        {
            get
            {
                return (this.ViewState["ImageUrl"] != null ? this.ViewState["ImageUrl"].ToString() : string.Empty);
            }
            set
            {
                this.ViewState["ImageUrl"] = value;
            }
        }

        [Category("Image"), DisplayName("Width")]
        public Unit ImageWidth
        {
            get
            {
                return (this.ViewState["ImageWidth"] != null ? (Unit)this.ViewState["ImageWidth"] : Unit.Empty);
            }
            set
            {
                this.ViewState["ImageWidth"] = value;
            }
        }

        [Category("Image"), DisplayName("Height")]
        public Unit ImageHeight
        {
            get
            {
                return (this.ViewState["ImageHeight"] != null ? (Unit)this.ViewState["ImageHeight"] : Unit.Empty);
            }
            set
            {
                this.ViewState["ImageHeight"] = value;
            }
        }

        [DefaultValue(""), Category("Behavior")]
        public string OnClientSectionSelected
        {
            get
            {
                return (this.ViewState["OnClientSectionSelected"] != null ? this.ViewState["OnClientSectionSelected"].ToString() : string.Empty);
            }
            set
            {
                this.ViewState["OnClientSectionSelected"] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this._contentTemplate != null)
            {
                Control container = new Control();
                this._contentTemplate.InstantiateIn(container);
                this.Controls.Add(container);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.EnsureID();
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
            writer.RenderBeginTag(HtmlTextWriterTag.H3);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            if (!string.IsNullOrEmpty(this.ImageUrl))
            {
                Image img = new Image();
                if (this.ImageHeight != Unit.Empty)
                    img.Height = this.ImageHeight;
                if (this.ImageWidth != Unit.Empty)
                    img.Width = this.ImageWidth;
                img.ImageUrl = this.ImageUrl;
                img.Style.Add(HtmlTextWriterStyle.PaddingRight, "5px");
                img.RenderControl(writer);
            }
            writer.Write(HttpUtility.HtmlEncode(this.Name));
            //End Div
            writer.RenderEndTag();
            //End A
            writer.RenderEndTag();
            //End H3 Tag
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderBeginTag(HtmlTextWriterTag.P);
            this.RenderChildren(writer);
            //End P
            writer.RenderEndTag();
            //End Div
            writer.RenderEndTag();
        }

        internal void SetOwner(JQueryAccordion owner)
        {
            this._owner = owner;
        }

        internal void PreventAutoID()
        {
            Type t = typeof(Control);
            FieldInfo mi = t.GetField("flags", BindingFlags.NonPublic | BindingFlags.Instance);
            object flags = mi.GetValue(this);
            PropertyInfo piIndexer = mi.FieldType.GetProperty("Item", BindingFlags.NonPublic | BindingFlags.Instance);
            object val = piIndexer.GetValue(flags, new object[1] { 0x80 });
            if ((bool)val)
            {
                MethodInfo setMethod = mi.FieldType.GetMethod("Set", BindingFlags.Instance | BindingFlags.NonPublic);
                setMethod.Invoke(flags, new object[1] { 0x40 });
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions