Ejemplo n.º 1
0
        private HiddenField CreateClientStateField(ExtenderControlBase extender)
        {
            // add a hidden field so we'll pick up the value
            //
            HiddenField field = new HiddenField();

            field.ID = GetClientStateFieldID(extender);
            Controls.Add(field);
            extender.ClientStateFieldID = field.ClientID;
            return(field);
        }
Ejemplo n.º 2
0
 public void AddExtender(ExtenderControlBase control, IEnumerable <string> clientIDs, Control dependedControl)
 {
     if (registeredControls.ContainsKey(control.BehaviorID))
     {
         return;
     }
     _extenderControls.Add(new TripletItem(control, clientIDs, dependedControl));
     registeredControls[control.BehaviorID] = true;
     if (_loadedClientStateValues)
     {
         LoadClientStateValues(control);
     }
 }
Ejemplo n.º 3
0
 public void AddExtender(ExtenderControlBase control, string clientID, Control dependedControl)
 {
     if (registeredControls.ContainsKey(control.BehaviorID))
     {
         return;
     }
     _extenderControls.Add(clientID == null
                               ? new TripletItem(control, null, dependedControl)
                               : new TripletItem(control, new[] { clientID }, dependedControl));
     registeredControls[control.BehaviorID] = true;
     if (_loadedClientStateValues)
     {
         LoadClientStateValues(control);
     }
 }
Ejemplo n.º 4
0
        private void SaveClientStateValues(ExtenderControlBase extender)
        {
            if (extender.EnableClientState)
            {
                HiddenField hiddenField = null;

                if (string.IsNullOrEmpty(extender.ClientStateFieldID))
                {
                    hiddenField = CreateClientStateField(extender);
                }
                else
                {
                    hiddenField = (HiddenField)NamingContainer.FindControl(GetClientStateFieldID(extender));
                }

                if (hiddenField != null)
                {
                    hiddenField.Value = extender.ClientState;
                }
            }
        }
Ejemplo n.º 5
0
 private void LoadClientStateValues(ExtenderControlBase extenderControl)
 {
     if (extenderControl.EnableClientState)
     {
         var hiddenField = (HiddenField)NamingContainer.FindControl(GetClientStateFieldID(extenderControl));
         if (hiddenField != null)
         {
             if (string.IsNullOrEmpty(hiddenField.Value) && !string.IsNullOrEmpty(Page.Request.Params[hiddenField.UniqueID]))
             {
                 hiddenField.Value = Page.Request.Params[hiddenField.UniqueID];
             }
             extenderControl.ClientState = hiddenField.Value;
         }
         else
         {
             var field = CreateClientStateField(extenderControl);
             field.Value = Page.Request.Params[field.UniqueID];
             extenderControl.ClientState = field.Value;
         }
     }
 }
        public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor,
                                             IUrlResolutionService urlResolver, IControlResolver controlResolver)
        {
            // validate preconditions
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (urlResolver == null)
            {
                urlResolver = instance as IUrlResolutionService;
            }
            if (controlResolver == null)
            {
                controlResolver = instance as IControlResolver;
            }

            // describe properties
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

            foreach (PropertyDescriptor prop in properties)
            {
                ExtenderControlPropertyAttribute propAttr  = null;
                ExtenderControlEventAttribute    eventAttr = null;
                string propertyName = prop.Name;

                // Try getting a property attribute
                propAttr = (ExtenderControlPropertyAttribute)prop.Attributes[typeof(ExtenderControlPropertyAttribute)];
                if (propAttr == null || !propAttr.IsScriptProperty)
                {
                    // Try getting an event attribute
                    eventAttr = (ExtenderControlEventAttribute)prop.Attributes[typeof(ExtenderControlEventAttribute)];
                    if (eventAttr == null || !eventAttr.IsScriptEvent)
                    {
                        continue;
                    }
                }

                // attempt to rename the property/event
                ClientPropertyNameAttribute nameAttr =
                    (ClientPropertyNameAttribute)prop.Attributes[typeof(ClientPropertyNameAttribute)];
                if (!string.IsNullOrEmpty(nameAttr.PropertyName))
                {
                    propertyName = nameAttr.PropertyName;
                }

                object value = prop.GetValue(instance);
                if (value == null)
                {
                    continue;
                }

                // determine whether to serialize the value of a property.  readOnly properties should always be serialized
                bool serialize = prop.ShouldSerializeValue(instance) || prop.IsReadOnly;
                if (serialize)
                {
                    // get the value of the property, skip if it is null
                    Control c = null;


                    // convert and resolve the value
                    if (eventAttr != null && prop.PropertyType != typeof(String))
                    {
                        throw new InvalidOperationException(
                                  "ExtenderControlEventAttribute can only be applied to a property with a PropertyType of System.String.");
                    }
                    else
                    {
                        if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum)
                        {
                            // Check if we can use any of our custom converters
                            // (first do a direct lookup on the property type,
                            // but also check all of its base types if nothing
                            // was found)
                            Converter <object, string> customConverter = null;
                            if (!_customConverters.TryGetValue(prop.PropertyType, out customConverter))
                            {
                                foreach (KeyValuePair <Type, Converter <object, string> > pair in _customConverters)
                                {
                                    if (prop.PropertyType.IsSubclassOf(pair.Key))
                                    {
                                        customConverter = pair.Value;
                                        break;
                                    }
                                }
                            }

                            // Use the custom converter if found, otherwise use
                            // its current type converter
                            if (customConverter != null)
                            {
                                value = customConverter(value);
                            }
                            else
                            {
                                // Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization
                                if (propAttr != null && propAttr.UseJsonSerialization)
                                {
                                    if (value is IEnumerable)
                                    {
                                        List <object> lst = new List <object>();
                                        foreach (object a in (IEnumerable)value)
                                        {
                                            object b;
                                            Type   typ = a.GetType();
                                            if (a as IUICollectionItem != null)
                                            {
                                                b = a is UriValue
                                                        ? urlResolver.ResolveClientUrl(((UriValue)a).Value.ToString())
                                                        : ((IUICollectionItem)a).Value;
                                            }
                                            else if (typ.IsPrimitive || typ.IsValueType)
                                            {
                                                b = a;
                                            }
                                            else
                                            {
                                                b = BuildGraph(a, urlResolver, controlResolver);
                                            }
                                            if (b != null)
                                            {
                                                lst.Add(b);
                                            }
                                        }
                                        if (lst.Count > 0)
                                        {
                                            value = lst;
                                        }
                                    }
                                    else if (value.GetType().IsPrimitive || value.GetType().IsValueType)
                                    {
                                    }
                                    else
                                    {
                                        value = BuildGraph(value, urlResolver, controlResolver);
                                    }
                                    // Use ASP.NET JSON serialization
                                }
                                else
                                {
                                    // Use the property's own converter
                                    TypeConverter conv = prop.Converter;
                                    value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value);
                                }
                            }
                        }
                        if (prop.Attributes[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (prop.Attributes[typeof(UrlPropertyAttribute)] != null && urlResolver != null)
                        {
                            value = urlResolver.ResolveClientUrl((string)value);
                        }
                    }

                    // add the value as an appropriate description
                    if (eventAttr != null)
                    {
                        descriptor.AddEvent(propertyName, (string)value);
                    }
                    else if (prop.Attributes[typeof(ElementReferenceAttribute)] != null)
                    {
                        if (c == null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (c != null)
                        {
                            value = c.ClientID;
                        }
                        descriptor.AddElementProperty(propertyName, (string)value);
                    }
                    else if (prop.Attributes[typeof(ComponentReferenceAttribute)] != null)
                    {
                        if (c == null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (c != null)
                        {
                            ExtenderControlBase ex = c as ExtenderControlBase;
                            if (ex != null && ex.BehaviorID.Length > 0)
                            {
                                value = ex.BehaviorID;
                            }
                            else
                            {
                                value = c.ClientID;
                            }
                        }
                        descriptor.AddComponentProperty(propertyName, (string)value);
                    }
                    else
                    {
                        if (c != null)
                        {
                            value = c.ClientID;
                        }
                        descriptor.AddProperty(propertyName, value);
                    }
                }
            }


            // determine if we should describe methods
            foreach (
                MethodInfo method in
                instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
            {
                ExtenderControlMethodAttribute methAttr =
                    (ExtenderControlMethodAttribute)
                    Attribute.GetCustomAttribute(method, typeof(ExtenderControlMethodAttribute));
                if (methAttr == null || !methAttr.IsScriptMethod)
                {
                    continue;
                }

                // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method
                Control control = instance as Control;
                if (control != null)
                {
                    // Force WebForms.js
                    control.Page.ClientScript.GetCallbackEventReference(control, null, null, null);

                    // Add the callback target
                    descriptor.AddProperty("_callbackTarget", control.UniqueID);
                }
                break;
            }
        }
Ejemplo n.º 7
0
 public void AddExtender(ExtenderControlBase control)
 {
     AddExtender(control, (IEnumerable <string>)null, null);
 }
Ejemplo n.º 8
0
 public void AddExtender(ExtenderControlBase control, IEnumerable <string> clientIDs)
 {
     AddExtender(control, clientIDs, null);
 }
Ejemplo n.º 9
0
 public void AddExtender(ExtenderControlBase control, string clientID)
 {
     AddExtender(control, clientID, null);
 }
Ejemplo n.º 10
0
 private string GetClientStateFieldID(ExtenderControlBase extender)
 {
     return(string.Format(CultureInfo.InvariantCulture, "{0}_ClientState", extender.ID));
 }