Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / UI / HtmlControls / HtmlHead.cs / 3 / HtmlHead.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.HtmlControls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class HtmlHeadBuilder : ControlBuilder {
public override Type GetChildControlType(string tagName, IDictionary attribs) {
if (String.Equals(tagName, "title", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlTitle);
if (String.Equals(tagName, "link", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlLink);
if (String.Equals(tagName, "meta", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlMeta);
return null;
}
public override bool AllowWhitespaceLiterals() {
return false;
}
}
///
/// Represents the HEAD element.
///
[
ControlBuilderAttribute(typeof(HtmlHeadBuilder)),
AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)
]
public sealed class HtmlHead : HtmlGenericControl {
private StyleSheetInternal _styleSheet;
private HtmlTitle _title;
private String _cachedTitleText;
///
/// Initializes an instance of an HtmlHead class.
///
public HtmlHead() : base("head") {
}
public HtmlHead(string tag) : base(tag) {
if (tag == null) {
tag = String.Empty;
}
_tagName = tag;
}
public IStyleSheet StyleSheet {
get {
if (_styleSheet == null) {
_styleSheet = new StyleSheetInternal(this);
}
return _styleSheet;
}
}
public String Title {
get {
if (_title == null) {
return _cachedTitleText;
}
return _title.Text;
}
set {
if (_title == null) {
// Side effect of adding a title to the control assigns _title
_cachedTitleText = value;
}
else {
_title.Text = value;
}
}
}
protected internal override void AddedControl(Control control, int index) {
base.AddedControl(control, index);
if (control is HtmlTitle) {
if (_title != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneTitleAllowed));
}
_title = (HtmlTitle)control;
}
}
///
///
/// Allows the HEAD element to register itself with the page.
///
protected internal override void OnInit(EventArgs e) {
base.OnInit(e);
Page p = Page;
if (p == null) {
throw new HttpException(SR.GetString(SR.Head_Needs_Page));
}
if (p.Header != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneHeadAllowed));
}
p.SetHeader(this);
}
internal void RegisterCssStyleString(string outputString) {
((StyleSheetInternal)StyleSheet).CSSStyleString = outputString;
}
protected internal override void RemovedControl(Control control) {
base.RemovedControl(control);
if (control is HtmlTitle) {
_title = null;
}
}
///
///
/// Notifies the Page when the HEAD is being rendered.
///
protected internal override void RenderChildren(HtmlTextWriter writer) {
base.RenderChildren(writer);
if (_title == null) {
// Always render out a tag since it is required for xhtml 1.1 compliance
writer.RenderBeginTag(HtmlTextWriterTag.Title);
if (_cachedTitleText != null) {
writer.Write(_cachedTitleText);
}
writer.RenderEndTag();
}
if ((string)Page.Request.Browser["requiresXhtmlCssSuppression"] != "true") {
RenderStyleSheet(writer);
}
}
internal void RenderStyleSheet(HtmlTextWriter writer) {
if(_styleSheet != null) {
_styleSheet.Render(writer);
}
}
internal static void RenderCssRule(CssTextWriter cssWriter, string selector,
Style style, IUrlResolutionService urlResolver) {
cssWriter.WriteBeginCssRule(selector);
CssStyleCollection attrs = style.GetStyleAttributes(urlResolver);
attrs.Render(cssWriter);
cssWriter.WriteEndCssRule();
}
///
/// Implements the IStyleSheet interface to represent an embedded
/// style sheet within the HEAD element.
///
private sealed class StyleSheetInternal : IStyleSheet, IUrlResolutionService {
private HtmlHead _owner;
private ArrayList _styles;
private ArrayList _selectorStyles;
private int _autoGenCount;
public StyleSheetInternal(HtmlHead owner) {
_owner = owner;
}
// CssStyleString registered by the PartialCachingControl
private string _cssStyleString;
internal string CSSStyleString {
get {
return _cssStyleString;
}
set {
_cssStyleString = value;
}
}
public void Render(HtmlTextWriter writer) {
if ((_styles == null) && (_selectorStyles == null) && CSSStyleString == null) {
return;
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.RenderBeginTag(HtmlTextWriterTag.Style);
CssTextWriter cssWriter = new CssTextWriter(writer);
if (_styles != null) {
for (int i = 0; i < _styles.Count; i++) {
StyleInfo si = (StyleInfo)_styles[i];
string cssClass = si.style.RegisteredCssClass;
if (cssClass.Length != 0) {
RenderCssRule(cssWriter, "." + cssClass, si.style, si.urlResolver);
}
}
}
if (_selectorStyles != null) {
for (int i = 0; i < _selectorStyles.Count; i++) {
SelectorStyleInfo si = (SelectorStyleInfo)_selectorStyles[i];
RenderCssRule(cssWriter, si.selector, si.style, si.urlResolver);
}
}
if (CSSStyleString != null) {
writer.Write(CSSStyleString);
}
writer.RenderEndTag();
}
#region Implementation of IStyleSheet
void IStyleSheet.CreateStyleRule(Style style, IUrlResolutionService urlResolver, string selector) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (selector.Length == 0) {
throw new ArgumentNullException("selector");
}
if (_selectorStyles == null) {
_selectorStyles = new ArrayList();
}
if (urlResolver == null) {
urlResolver = this;
}
SelectorStyleInfo styleInfo = new SelectorStyleInfo();
styleInfo.selector = selector;
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
_selectorStyles.Add(styleInfo);
Page page = _owner.Page;
// If there are any partial caching controls on the stack, forward the styleInfo to them
if (page.PartialCachingControlStack != null) {
foreach (BasePartialCachingControl c in page.PartialCachingControlStack) {
c.RegisterStyleInfo(styleInfo);
}
}
}
void IStyleSheet.RegisterStyle(Style style, IUrlResolutionService urlResolver) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (_styles == null) {
_styles = new ArrayList();
}
else if (style.RegisteredCssClass.Length != 0) {
// if it's already registered, throw an exception
throw new InvalidOperationException(SR.GetString(SR.HtmlHead_StyleAlreadyRegistered));
}
if (urlResolver == null) {
urlResolver = this;
}
StyleInfo styleInfo = new StyleInfo();
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
int index = _autoGenCount++;
string name = "aspnet_s" + index.ToString(NumberFormatInfo.InvariantInfo);
style.SetRegisteredCssClass(name);
_styles.Add(styleInfo);
}
#endregion
#region Implementation of IUrlResolutionService
string IUrlResolutionService.ResolveClientUrl(string relativeUrl) {
return _owner.ResolveClientUrl(relativeUrl);
}
#endregion
private sealed class StyleInfo {
public Style style;
public IUrlResolutionService urlResolver;
}
}
}
internal sealed class SelectorStyleInfo {
public string selector;
public Style style;
public IUrlResolutionService urlResolver;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.HtmlControls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class HtmlHeadBuilder : ControlBuilder {
public override Type GetChildControlType(string tagName, IDictionary attribs) {
if (String.Equals(tagName, "title", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlTitle);
if (String.Equals(tagName, "link", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlLink);
if (String.Equals(tagName, "meta", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlMeta);
return null;
}
public override bool AllowWhitespaceLiterals() {
return false;
}
}
///
/// Represents the HEAD element.
///
[
ControlBuilderAttribute(typeof(HtmlHeadBuilder)),
AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)
]
public sealed class HtmlHead : HtmlGenericControl {
private StyleSheetInternal _styleSheet;
private HtmlTitle _title;
private String _cachedTitleText;
///
/// Initializes an instance of an HtmlHead class.
///
public HtmlHead() : base("head") {
}
public HtmlHead(string tag) : base(tag) {
if (tag == null) {
tag = String.Empty;
}
_tagName = tag;
}
public IStyleSheet StyleSheet {
get {
if (_styleSheet == null) {
_styleSheet = new StyleSheetInternal(this);
}
return _styleSheet;
}
}
public String Title {
get {
if (_title == null) {
return _cachedTitleText;
}
return _title.Text;
}
set {
if (_title == null) {
// Side effect of adding a title to the control assigns _title
_cachedTitleText = value;
}
else {
_title.Text = value;
}
}
}
protected internal override void AddedControl(Control control, int index) {
base.AddedControl(control, index);
if (control is HtmlTitle) {
if (_title != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneTitleAllowed));
}
_title = (HtmlTitle)control;
}
}
///
///
/// Allows the HEAD element to register itself with the page.
///
protected internal override void OnInit(EventArgs e) {
base.OnInit(e);
Page p = Page;
if (p == null) {
throw new HttpException(SR.GetString(SR.Head_Needs_Page));
}
if (p.Header != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneHeadAllowed));
}
p.SetHeader(this);
}
internal void RegisterCssStyleString(string outputString) {
((StyleSheetInternal)StyleSheet).CSSStyleString = outputString;
}
protected internal override void RemovedControl(Control control) {
base.RemovedControl(control);
if (control is HtmlTitle) {
_title = null;
}
}
///
///
/// Notifies the Page when the HEAD is being rendered.
///
protected internal override void RenderChildren(HtmlTextWriter writer) {
base.RenderChildren(writer);
if (_title == null) {
// Always render out a tag since it is required for xhtml 1.1 compliance
writer.RenderBeginTag(HtmlTextWriterTag.Title);
if (_cachedTitleText != null) {
writer.Write(_cachedTitleText);
}
writer.RenderEndTag();
}
if ((string)Page.Request.Browser["requiresXhtmlCssSuppression"] != "true") {
RenderStyleSheet(writer);
}
}
internal void RenderStyleSheet(HtmlTextWriter writer) {
if(_styleSheet != null) {
_styleSheet.Render(writer);
}
}
internal static void RenderCssRule(CssTextWriter cssWriter, string selector,
Style style, IUrlResolutionService urlResolver) {
cssWriter.WriteBeginCssRule(selector);
CssStyleCollection attrs = style.GetStyleAttributes(urlResolver);
attrs.Render(cssWriter);
cssWriter.WriteEndCssRule();
}
///
/// Implements the IStyleSheet interface to represent an embedded
/// style sheet within the HEAD element.
///
private sealed class StyleSheetInternal : IStyleSheet, IUrlResolutionService {
private HtmlHead _owner;
private ArrayList _styles;
private ArrayList _selectorStyles;
private int _autoGenCount;
public StyleSheetInternal(HtmlHead owner) {
_owner = owner;
}
// CssStyleString registered by the PartialCachingControl
private string _cssStyleString;
internal string CSSStyleString {
get {
return _cssStyleString;
}
set {
_cssStyleString = value;
}
}
public void Render(HtmlTextWriter writer) {
if ((_styles == null) && (_selectorStyles == null) && CSSStyleString == null) {
return;
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.RenderBeginTag(HtmlTextWriterTag.Style);
CssTextWriter cssWriter = new CssTextWriter(writer);
if (_styles != null) {
for (int i = 0; i < _styles.Count; i++) {
StyleInfo si = (StyleInfo)_styles[i];
string cssClass = si.style.RegisteredCssClass;
if (cssClass.Length != 0) {
RenderCssRule(cssWriter, "." + cssClass, si.style, si.urlResolver);
}
}
}
if (_selectorStyles != null) {
for (int i = 0; i < _selectorStyles.Count; i++) {
SelectorStyleInfo si = (SelectorStyleInfo)_selectorStyles[i];
RenderCssRule(cssWriter, si.selector, si.style, si.urlResolver);
}
}
if (CSSStyleString != null) {
writer.Write(CSSStyleString);
}
writer.RenderEndTag();
}
#region Implementation of IStyleSheet
void IStyleSheet.CreateStyleRule(Style style, IUrlResolutionService urlResolver, string selector) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (selector.Length == 0) {
throw new ArgumentNullException("selector");
}
if (_selectorStyles == null) {
_selectorStyles = new ArrayList();
}
if (urlResolver == null) {
urlResolver = this;
}
SelectorStyleInfo styleInfo = new SelectorStyleInfo();
styleInfo.selector = selector;
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
_selectorStyles.Add(styleInfo);
Page page = _owner.Page;
// If there are any partial caching controls on the stack, forward the styleInfo to them
if (page.PartialCachingControlStack != null) {
foreach (BasePartialCachingControl c in page.PartialCachingControlStack) {
c.RegisterStyleInfo(styleInfo);
}
}
}
void IStyleSheet.RegisterStyle(Style style, IUrlResolutionService urlResolver) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (_styles == null) {
_styles = new ArrayList();
}
else if (style.RegisteredCssClass.Length != 0) {
// if it's already registered, throw an exception
throw new InvalidOperationException(SR.GetString(SR.HtmlHead_StyleAlreadyRegistered));
}
if (urlResolver == null) {
urlResolver = this;
}
StyleInfo styleInfo = new StyleInfo();
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
int index = _autoGenCount++;
string name = "aspnet_s" + index.ToString(NumberFormatInfo.InvariantInfo);
style.SetRegisteredCssClass(name);
_styles.Add(styleInfo);
}
#endregion
#region Implementation of IUrlResolutionService
string IUrlResolutionService.ResolveClientUrl(string relativeUrl) {
return _owner.ResolveClientUrl(relativeUrl);
}
#endregion
private sealed class StyleInfo {
public Style style;
public IUrlResolutionService urlResolver;
}
}
}
internal sealed class SelectorStyleInfo {
public string selector;
public Style style;
public IUrlResolutionService urlResolver;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ObjectReferenceStack.cs
- TemplateManager.cs
- Axis.cs
- ZipArchive.cs
- Rotation3D.cs
- DebuggerAttributes.cs
- UriExt.cs
- DesignerVerbToolStripMenuItem.cs
- HostingEnvironmentException.cs
- SymbolMethod.cs
- DataGridViewRowHeightInfoPushedEventArgs.cs
- PerformanceCounterLib.cs
- DesignBinding.cs
- CurrentChangedEventManager.cs
- ErrorInfoXmlDocument.cs
- ToggleButtonAutomationPeer.cs
- EntityClassGenerator.cs
- PersistChildrenAttribute.cs
- ConnectionsZone.cs
- XmlTextAttribute.cs
- FloaterParagraph.cs
- WindowsFormsHelpers.cs
- Assembly.cs
- HttpPostLocalhostServerProtocol.cs
- Polygon.cs
- AssertFilter.cs
- LabelLiteral.cs
- SynchronizationLockException.cs
- LexicalChunk.cs
- RemotingHelper.cs
- CalendarSelectionChangedEventArgs.cs
- DocumentViewer.cs
- DrawItemEvent.cs
- Paragraph.cs
- XmlBaseWriter.cs
- ImageClickEventArgs.cs
- Statements.cs
- RegexTypeEditor.cs
- adornercollection.cs
- TraceXPathNavigator.cs
- SqlCommandSet.cs
- CorePropertiesFilter.cs
- GridViewRow.cs
- Serializer.cs
- entityreference_tresulttype.cs
- TemplateControlParser.cs
- SchemaObjectWriter.cs
- DtdParser.cs
- SqlServices.cs
- IndentTextWriter.cs
- StandardOleMarshalObject.cs
- BitStream.cs
- CultureTable.cs
- PieceNameHelper.cs
- GridProviderWrapper.cs
- AnnotationHighlightLayer.cs
- StringExpressionSet.cs
- ImageKeyConverter.cs
- EntityDataSourceChangedEventArgs.cs
- HandleCollector.cs
- TCEAdapterGenerator.cs
- storepermissionattribute.cs
- StorageBasedPackageProperties.cs
- ResizeGrip.cs
- SHA512.cs
- Path.cs
- RectangleGeometry.cs
- DataBindingCollection.cs
- Authorization.cs
- AtomServiceDocumentSerializer.cs
- MetaTable.cs
- ClientType.cs
- DebugInfo.cs
- ProgressBar.cs
- CorrelationResolver.cs
- SqlConnectionManager.cs
- PageThemeBuildProvider.cs
- SessionStateContainer.cs
- WeakHashtable.cs
- ComplexTypeEmitter.cs
- Utils.cs
- FrugalMap.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- TypeValidationEventArgs.cs
- SimplePropertyEntry.cs
- GregorianCalendarHelper.cs
- ToolStripGripRenderEventArgs.cs
- DbQueryCommandTree.cs
- HttpCacheVaryByContentEncodings.cs
- UnsafeNativeMethods.cs
- Base64Encoder.cs
- HttpRequest.cs
- RelationalExpressions.cs
- CroppedBitmap.cs
- WindowsGraphicsCacheManager.cs
- PageContent.cs
- FileChangesMonitor.cs
- ReflectPropertyDescriptor.cs
- HitTestDrawingContextWalker.cs
- CodeAttributeArgumentCollection.cs