⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriptusercontrol.cs

📁 AJAX 应用 实现页面的无刷新
💻 CS
📖 第 1 页 / 共 2 页
字号:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Drawing;

namespace AjaxControlToolkit
{
    /// <summary>
    /// ScriptUserControl is used to define complex user controls which support ASP.NET AJAX script extensions
    /// </summary>
    [ClientScriptResource(null, Constants.BaseScriptResourceName)]
    public class ScriptUserControl : UserControl, IScriptControl, IControlResolver, IPostBackDataHandler, ICallbackEventHandler, IClientStateManager
    {
        #region [ Fields ]

        private ScriptManager _scriptManager;
        private bool _enableClientState;
        private string _cachedClientStateFieldID;
        private string _callbackArgument;
        private HtmlTextWriterTag _tagKey;
        private string _tagName;
        private Style _controlStyle;

        #endregion

        #region [ Constructor ]

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        /// <param name="tag"></param>
        public ScriptUserControl(HtmlTextWriterTag tag)
            : this(false, tag)
        {
        }

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        protected ScriptUserControl()
            : this(false)
        {
        }

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        /// <param name="tag"></param>
        protected ScriptUserControl(string tag)
            : this(false, tag)
        {
        }

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        /// <param name="enableClientState"></param>
        protected ScriptUserControl(bool enableClientState)
        {
            _enableClientState = enableClientState;
        }

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        /// <param name="enableClientState"></param>
        /// <param name="tag"></param>
        protected ScriptUserControl(bool enableClientState, HtmlTextWriterTag tag)
        {
            _enableClientState = enableClientState;
            _tagKey = tag;
        }

        /// <summary>
        /// Initializes a new ScriptUserControl
        /// </summary>
        /// <param name="enableClientState"></param>
        /// <param name="tag"></param>
        protected ScriptUserControl(bool enableClientState, string tag)
        {
            _enableClientState = enableClientState;
            _tagKey = HtmlTextWriterTag.Unknown;
            _tagName = tag;
        }

        #endregion

        #region [ Properties ]

        /// <summary>
        /// For debugging - setting this causes the extender to load the specified script instead of the one out of the resources.  This
        /// lets you set breakpoints and modify the script without rebuilding, etc.
        /// </summary>
        /// <remarks>
        /// Note to inheritors: If you do not wish the user to set the script path, override script path and throw a NotSupportedException on set.  Also decorate the ScriptPath attribute with a [Browsable(false)] and [EditorBrowsableState(EditorBrowsableState.Never)]
        /// </remarks>
        [DefaultValue("")]
        public virtual string ScriptPath
        {
            get { return (string)(ViewState["ScriptPath"] ?? string.Empty); }
            set { ViewState["ScriptPath"] = value; }
        }

        [DefaultValue(typeof(Color), "")]
        [TypeConverter(typeof(WebColorConverter))]
        public virtual Color BackColor
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Color.Empty;
                }
                return _controlStyle.BackColor;
            }
            set
            {
                ControlStyle.BackColor = value;
            }
        }

        [DefaultValue(typeof(Color), "")]
        [TypeConverter(typeof(WebColorConverter))]
        public virtual Color BorderColor
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Color.Empty;
                }
                return _controlStyle.BorderColor;
            }
            set
            {
                ControlStyle.BorderColor = value;
            }
        }

        [DefaultValue(BorderStyle.None)]
        public virtual BorderStyle BorderStyle
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return BorderStyle.None;
                }
                return _controlStyle.BorderStyle;
            }
            set
            {
                ControlStyle.BorderStyle = value;
            }
        }

        [DefaultValue(typeof(Unit), "")]
        public virtual Unit BorderWidth
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Unit.Empty;
                }
                return _controlStyle.BorderWidth;
            }
            set
            {
                ControlStyle.BorderWidth = value;
            }
        }

        [DefaultValue("")]
        public virtual string CssClass
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return string.Empty;
                }
                return _controlStyle.CssClass;
            }
            set
            {
                ControlStyle.CssClass = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [NotifyParentProperty(true)]
        public virtual FontInfo Font
        {
            get
            {
                return ControlStyle.Font;
            }
        }

        [DefaultValue(typeof(Color), "")]
        [TypeConverter(typeof(WebColorConverter))]
        public virtual Color ForeColor
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Color.Empty;
                }
                return ControlStyle.ForeColor;
            }
            set
            {
                ControlStyle.ForeColor = value;
            }
        }

        [DefaultValue(typeof(Unit), "")]
        public virtual Unit Height
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Unit.Empty;
                }
                return ControlStyle.Height;
            }
            set
            {
                ControlStyle.Height = value;
            }
        }

        [DefaultValue(typeof(Unit), "")]
        public virtual Unit Width
        {
            get
            {
                if (!ControlStyleCreated)
                {
                    return Unit.Empty;
                }
                return ControlStyle.Width;
            }
            set
            {
                ControlStyle.Width = value;
            }
        }



        /// <summary>
        /// The Style for this control
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Style ControlStyle
        {
            get
            {
                if (_controlStyle == null)
                {
                    _controlStyle = CreateControlStyle();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_controlStyle).TrackViewState();
                    }
                }
                return _controlStyle;
            }
        }

        /// <summary>
        /// Whether the control's style has been created
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool ControlStyleCreated
        {
            get { return _controlStyle != null; }
        }

        /// <summary>
        /// The Css Style for this control
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CssStyleCollection Style
        {
            get { return Attributes.CssStyle; }
        }

        /// <summary>
        /// The script type to use for the ScriptControl
        /// </summary>
        protected virtual string ClientControlType
        {
            get
            {
                ClientScriptResourceAttribute attr = (ClientScriptResourceAttribute)TypeDescriptor.GetAttributes(this)[typeof(ClientScriptResourceAttribute)];
                return attr.ComponentType;
            }
        }

        /// <summary>
        /// Whether this control supports ClientState
        /// </summary>
        /// <remarks>
        /// Note to inheritors: You should either pass true to the constructor for enableClientState or override this property to enable client state for inherited controls.
        /// </remarks>
        protected virtual bool SupportsClientState
        {
            get { return _enableClientState; }
        }

        /// <summary>
        /// Gets the ScriptManager for the page
        /// </summary>
        protected ScriptManager ScriptManager
        {
            get
            {
                EnsureScriptManager();
                return _scriptManager;
            }
        }

        /// <summary>
        /// The ID of the ClientState field
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        protected string ClientStateFieldID
        {
            get
            {
                if (_cachedClientStateFieldID == null)
                {
                    _cachedClientStateFieldID = ClientID + "_ClientState";
                }
                return _cachedClientStateFieldID;
            }
        }

        /// <summary>
        /// Gets the tag key used when rendering the outer wrapper element for this user control
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        protected virtual HtmlTextWriterTag TagKey
        {
            get
            {
                return _tagKey;
            }
        }

        /// <summary>
        /// Gets the tag name used when rendering the outer wrapper element for this user control
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Avoiding possibly breaking change")]
        protected virtual string TagName
        {
            get
            {
                if (_tagName == null && TagKey != HtmlTextWriterTag.Unknown)
                {
                    _tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToLower(CultureInfo.InvariantCulture);
                }
                return _tagName;
            }
        }

        #endregion

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -