Click here to Skip to main content
15,891,951 members
Articles / Web Development / XHTML

Write jQuery Plugin WebControl for ASP.NET Just in Few Minutes!

Rate me:
Please Sign up or sign in to vote.
4.50/5 (27 votes)
9 Jun 2009GPL33 min read 220.2K   2.6K   115  
Write jQuery plugin WebControl for ASP.NET just in few miniutes!
///  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
///  Dual licensed under the MIT and GPL licenses:
///  http://www.opensource.org/licenses/mit-license.php
///  http://www.gnu.org/licenses/gpl.html
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.ComponentModel;

namespace DNA.UI.JQuery
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public abstract class JQueryMultiViewControl : WebControl,INamingContainer,IPostBackDataHandler
    {
        protected int NewIndex = -1;
       private ViewCollection views = new ViewCollection();

        [Browsable(false)]
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        [Category("Behavior")]
        [Bindable(true)]
        public bool AutoPostBack
       {
           get
           {
               Object obj = ViewState["AutoPostBack"];
               return (obj == null) ? false : (bool)obj;
           }
           set
           {
               ViewState["AutoPostBack"] = value;
           }
       }

        [Browsable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ViewCollection Views
        {
            get { return views; }
            set { views = value; }
        }

        [Category("Appearance")]
        [Bindable(true)]
        [Browsable(true)]
        [Themeable(true)]
        public string HeaderStyle
        {
            get
            {
                Object obj = ViewState["HeaderStyle"];
                return (obj == null) ? String.Empty : (string)obj;
            }
            set
            {
                ViewState["HeaderStyle"] = value;
            }
        }

        [Category("Appearance")]
        [Bindable(true)]
        [Browsable(true)]
        [Themeable(true)]
        public string ContentStyle
        {
            get
            {
                Object obj = ViewState["ContentStyle"];
                return (obj == null) ? String.Empty : (string)obj;
            }
            set
            {
                ViewState["ContentStyle"] = value;
            }
        }

        [Category("Appearance")]
        [Bindable(true)]
        [Browsable(true)]
        [Themeable(true)]
        [CssClassProperty]
        public string ContentCssClass
        {
            get
            {
                Object obj = ViewState["ContentCssClass"];
                return (obj == null) ? String.Empty : (string)obj;
            }
            set
            {
                ViewState["ContentCssClass"] = value;
            }
        }

        [Category("Appearance")]
        [Bindable(true)]
        [Browsable(true)]
        [Themeable(true)]
        [CssClassProperty]
        public string HeaderCssClass
        {
            get
            {
                Object obj = ViewState["HeaderCssClass"];
                return (obj == null) ? String.Empty : (string)obj;
            }
            set
            {
                ViewState["HeaderCssClass"] = value;
            }
        }

        protected void ApplyStyles()
        {
            foreach (View view in views)
            {
                if (!string.IsNullOrEmpty(HeaderStyle))
                    view.HeaderStyle = HeaderStyle;
                if (!string.IsNullOrEmpty(ContentStyle))
                    view.ContentStyle = ContentStyle;
                if (!string.IsNullOrEmpty(ContentCssClass))
                    view.ContentCssClass = ContentCssClass;
                if (!string.IsNullOrEmpty(HeaderCssClass))
                    view.HeaderCssClass = HeaderCssClass;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ApplyStyles();
        }

        protected override void OnInit(EventArgs e)
        {
            foreach (View view in views)
                Controls.Add(view);
            Page.RegisterRequiresPostBack(this);
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            ScriptBuilder.RegisterJQueryControl(this);
            base.OnPreRender(e);
        }

        protected string HiddenKey
        {
            get { return ClientID + "_selectedID"; }
        }

        protected virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            return true;
        }

        protected virtual void RaisePostDataChangedEvent()
        {
        }

        #region IPostBackDataHandler Members

        bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
           return ((JQueryMultiViewControl)this).LoadPostData(postDataKey,postCollection);
        }

        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            ((JQueryMultiViewControl)this).RaisePostDataChangedEvent();
        }

        #endregion
    }
}

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 GNU General Public License (GPLv3)


Written By
Architect DotNetAge
China China
In 1999, I started programming using Delphi, VB, VJ.From 2002 I started with .NET using C#.Since 2005 when i had became an EIP product manager I was focus on EIP and CMS technique. In 2008 i established dotnetage.com and started to shared my ideas and projects online. I believe "No shared no grow"

www.dotnetage.com

Comments and Discussions