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

📄 extendercontrolbase.cs

📁 AJAX 应用 实现页面的无刷新
💻 CS
📖 第 1 页 / 共 3 页
字号:

                if (hiddenField != null)
                {
                    hiddenField.Value = ClientState;
                }
            }
        }

        private bool ShouldSerializeBehaviorID()
        {
            return IsRenderingScript || 0 != String.Compare(ClientID, BehaviorID, StringComparison.OrdinalIgnoreCase);
        }        

        /// <summary>
        /// Serializes a single property value out to it's string representation.
        /// </summary>
        /// <param name="props">The TargetProperties object</param>
        /// <param name="prop">The property</param>
        /// <returns></returns>
        [Obsolete("Replaced by a call to ScriptObjectBuilder")]
        protected object SerializeProperty(PropertyDescriptor prop)
        {
            return SerializeProperty(prop, false);
        }

        /// <summary>
        /// Serializes a given property's value to a string.  This method owns checking
        /// that the property should actually be serialized, as well as doing the work
        /// to convert the value to a string using the appropriate TypeConverter.
        /// </summary>
        /// <param name="force">True to force serialization</param>
        /// <returns></returns>
        [Obsolete("Replaced by a call to ScriptObjectBuilder")]
        protected virtual object SerializeProperty(PropertyDescriptor prop, bool force)
        {
            if (prop == null) return null;

            // First, get the current value.
            //
            object val = prop.GetValue(this);

            // check to see if we should bother to serialize this.
            //
            if (val != null)
            {
                bool serialize = prop.ShouldSerializeValue(this);

                if (serialize)
                {
                    DesignerSerializationVisibilityAttribute sv = (DesignerSerializationVisibilityAttribute)prop.Attributes[typeof(DesignerSerializationVisibilityAttribute)];

                    if (sv != null && sv.Visibility == DesignerSerializationVisibility.Hidden)
                    {
                        // If a property is marked as hidden, we may just be hiding it from the ASPX serializer
                        // in the designer.  In that case, we've got a list of "forced" properties that will
                        // ignore that value.
                        //
                        serialize = (-1 != Array.IndexOf<string>(ForceSerializationProps, prop.Name));
                    }
                }

                if (force || serialize) {
                    bool convertToString = !prop.PropertyType.IsPrimitive && !(prop.PropertyType.IsEnum);

                    // Convert to a string via the TypeConverter.
                    // Use InvariantCulture so that properties always serialize the same regardless
                    // of the host web server's culture.
                    // 
                    // If the property type is a System.Drawing.Color, use the ColorTranslator to convert
                    // the value to an HTML color string (#rrggbb)
                    //
                    if (convertToString) {
                        if (prop.PropertyType == typeof(Color))
                        {
                            val = ColorTranslator.ToHtml((Color)val);
                        }
                        else
                        {
                            TypeConverter tc = prop.Converter;
                            val = tc.ConvertToString(null, CultureInfo.InvariantCulture, val);
                        }
                    }

                    // if this thing is a control reference,
                    // then switch the ID out for the client ID
                    //
                    if (prop.PropertyType == typeof(string)) {

                        // If the property has the ResolveClientUrlAttribute attribute,
                        // call ResolveClientUrl to fix up any virtual paths
                        if (null != prop.Attributes[typeof(UrlPropertyAttribute)]) {
                            val = ResolveClientUrl((string)val);
                        }
                    }
                } else {
                    return null;
                }
            }
            return val;
        }

        #region Extender Helper Methods
        internal static string[] ForceSerializationProps = new string[] { "ClientStateFieldID" };

        private bool _enableClientState;
        private string _clientState;
        private ProfilePropertyBindingCollection _profileBindings;

        [Browsable(false), Obsolete("WARNING: ProfileBindings are disabled for this Toolkit release pending technical issues.  We hope to re-enable this in an upcoming release")]        
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]        
        public ProfilePropertyBindingCollection ProfileBindings {
            get {
                if (_profileBindings == null) {
                    _profileBindings = new ProfilePropertyBindingCollection();
                }
                return _profileBindings;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeProfileBindings() {
            return false;
            //return _profileBindings != null &&  _profileBindings.Count > 0;
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string ClientState {
            get {
                return _clientState;
            }
            set {
                _clientState = value;
                //OnChanged(EventArgs.Empty);
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [ExtenderControlProperty()]
        [IDReferenceProperty(typeof(HiddenField))]
        [DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string ClientStateFieldID 
        {
            get { return GetPropertyValue("ClientStateFieldID", ""); }
            set { SetPropertyValue("ClientStateFieldID", value); }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool EnableClientState {
            get {
                return _enableClientState;
            }
            set {
                _enableClientState = value;
            }
        }


        [EditorBrowsable(EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public bool ShouldSerializeClientStateFieldID() {
            return EnableClientState;
        }

        /// <summary>
        /// Suppresses the "unused parameter" warning of code analysis for cases
        /// where a parameter is deliberately unused
        /// </summary>
        /// <param name="unused">unused parameter</param>
        protected static void SuppressUnusedParameterWarning(object unused) {
            if (null != unused)
            {
                unused.GetType();  // Do nothing assignment
            }
        }
        #endregion

        #region PropertySupportMethods
        /// <summary>
        /// Checks if all properties are valid
        /// </summary>
        /// <param name="throwException">true iff an exception is to be thrown for invalid parameters</param>
        /// <returns>true iff all parameters are valid</returns>
        protected virtual bool CheckIfValid(bool throwException) {
            bool valid = true;
            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this)) {
                // If the property is tagged with RequiredPropertyAttribute, but doesn't have a value, throw an exception
                if ((null != prop.Attributes[typeof(RequiredPropertyAttribute)]) && ((null == prop.GetValue(this)) || !prop.ShouldSerializeValue(this))) {
                    valid = false;
                    if (throwException) {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "{0} missing required {1} property value for {2}.", GetType().ToString(), prop.Name, ID), prop.Name);
                    }
                }
            }
            return valid;
        }

        /// <summary>
        /// Called during rendering to give derived classes a chance to validate their properties
        /// </summary>
        /// <remarks>
        /// If the properties aren't valid, an exception of type ArgumentException should be thrown
        /// </remarks>
        public virtual void EnsureValid() {
            CheckIfValid(true);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "V", Justification="V stands for value")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1715:IdentifiersShouldHaveCorrectPrefix", MessageId = "T", Justification = "V stands for value")]
        protected V GetPropertyValue<V>(string propertyName, V nullValue)
        {
            if (ViewState[propertyName] == null)
            {
                return nullValue;
            }
            return (V)ViewState[propertyName];
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "V", Justification = "V stands for value")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1715:IdentifiersShouldHaveCorrectPrefix", MessageId = "T", Justification = "V stands for value")]
        protected void SetPropertyValue<V>(string propertyName, V value)
        {
            ViewState[propertyName] = value;
        }

        [Obsolete("Use GetPropertyValue<V> instead")]
        protected string GetPropertyStringValue(string propertyName)
        {
            return GetPropertyValue<string>(propertyName, "");
        }

        [Obsolete("Use SetPropertyValue<V> instead")]
        protected void SetPropertyStringValue(string propertyName, string value)
        {
            SetPropertyValue<string>(propertyName, value);
        }

        [Obsolete("Use GetPropertyValue<V> instead")]
        protected int GetPropertyIntValue(string propertyName)
        {
            return GetPropertyValue<int>(propertyName, 0);
        }

        [Obsolete("Use SetPropertyValue<V> instead")]
        protected void SetPropertyIntValue(string propertyName, int value)
        {
            SetPropertyValue<int>(propertyName, value);
        }

        [Obsolete("Use GetPropertyValue<V> instead")]
        protected bool GetPropertyBoolValue(string propertyName)
        {
            return GetPropertyValue<bool>(propertyName, false);
        }

        [Obsolete("Use SetPropertyValue<V> instead")]
        protected void SetPropertyBoolValue(string propertyName, bool value)
        {
            SetPropertyValue<bool>(propertyName, value);
        }

        #endregion

        #region [ IControlResolver Members ]

        public Control ResolveControl(string controlId)
        {
            return FindControl(controlId);
        }

        #endregion
    }
}

⌨️ 快捷键说明

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