protected virtual void ApplyStyle(ResolvedControl control) { if (SetHtmlAttributes != null) { foreach (var attr in SetHtmlAttributes) { if (!control.HtmlAttributes.ContainsKey(attr.Key) || attr.Value.append) { ResolvedHtmlAttributeSetter setter = null; if (attr.Value.value is ResolvedBinding) { setter = new ResolvedHtmlAttributeBinding(attr.Key, (ResolvedBinding)attr.Value.value); } else { setter = new ResolvedHtmlAttributeValue(attr.Key, (string)attr.Value.value); } control.SetHtmlAttribute(setter); } } } if (SetProperties != null) { foreach (var prop in SetProperties) { if (!control.Properties.ContainsKey(prop.Key)) { control.Properties.Add(prop.Key, prop.Value); } } } }
protected virtual void ApplyStyle(ResolvedControl control) { if (SetHtmlAttributes != null) { foreach (var attr in SetHtmlAttributes) { if (control.HtmlAttributes == null || !control.HtmlAttributes.ContainsKey(attr.Key) || attr.Value.append) { control.SetHtmlAttribute(attr.Key, attr.Value.value); } } } if (SetProperties != null) { foreach (var prop in SetProperties) { if (!control.Properties.ContainsKey(prop.Key)) { control.Properties.Add(prop.Key, prop.Value); } } } }
/// <summary> /// Processes the HTML attribute. /// </summary> private void ProcessAttribute(DothtmlAttributeNode attribute, ResolvedControl control, DataContextStack dataContext) { if (attribute.AttributePrefix == "html") { if (!control.Metadata.HasHtmlAttributesCollection) { throw new DotvvmCompilationException($"control { control.Metadata.Name } does not have html attribute collection", attribute.Tokens); } control.SetHtmlAttribute(attribute.AttributeName, ProcessAttributeValue(attribute.ValueNode, dataContext)); return; } if (!string.IsNullOrEmpty(attribute.AttributePrefix)) { throw new DotvvmCompilationException("Attributes with XML namespaces are not supported!", attribute.Tokens); } // find the property var property = FindProperty(control.Metadata, attribute.AttributeName); if (property != null) { if (property.IsBindingProperty) { var typeChange = DataContextChangeAttribute.GetDataContextExpression(dataContext, control, property); if (typeChange != null) { dataContext = new DataContextStack(typeChange, dataContext); } } if (!property.MarkupOptions.MappingMode.HasFlag(MappingMode.Attribute)) { throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot be used as attribute", attribute.Tokens); } // set the property if (attribute.ValueNode == null) { throw new DotvvmCompilationException($"The attribute '{property.Name}' on the control '{control.Metadata.Name}' must have a value!", attribute.Tokens); } else if (attribute.ValueNode is DothtmlValueBindingNode) { // binding var bindingNode = (attribute.ValueNode as DothtmlValueBindingNode).BindingNode; if (!property.MarkupOptions.AllowBinding) { throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot contain binding.", bindingNode.Tokens); } var resolvedBinding = ProcessBinding(bindingNode, dataContext); control.SetProperty(new ResolvedPropertyBinding(property, resolvedBinding)); } else { // hard-coded value in markup if (!property.MarkupOptions.AllowHardCodedValue) { throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot contain hard coded value.", attribute.ValueNode.Tokens); } var textValue = attribute.ValueNode as DothtmlValueTextNode; var value = ReflectionUtils.ConvertValue(textValue.Text, property.PropertyType); control.SetPropertyValue(property, value); } } else if (control.Metadata.HasHtmlAttributesCollection) { // if the property is not found, add it as an HTML attribute control.SetHtmlAttribute(attribute.AttributeName, ProcessAttributeValue(attribute.ValueNode, dataContext)); } else { throw new DotvvmCompilationException($"The control '{control.Metadata.Type}' does not have a property '{attribute.AttributeName}' and does not allow HTML attributes!"); } }