2 // System.Web.UI.WebControls.WebControl.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 using System.Collections;
14 using System.ComponentModel;
18 using System.Collections.Specialized;
20 namespace System.Web.UI.WebControls
22 [PersistChildrenAttribute(false)]
23 [ParseChildrenAttribute(true)]
24 public class WebControl : Control, IAttributeAccessor
26 HtmlTextWriterTag tagKey;
27 AttributeCollection attributes;
28 StateBag attributeState;
33 protected WebControl () : this (HtmlTextWriterTag.Span)
37 public WebControl (HtmlTextWriterTag tag)
42 protected WebControl (string tag)
47 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
48 [WebSysDescription ("A keyboard shortcut for the WebControl.")]
\r
49 public virtual string AccessKey
\r
53 object o = ViewState["AccessKey"];
\r
56 return String.Empty;
\r
60 if (value != null && value.Length > 1)
61 throw new ArgumentOutOfRangeException ("value");
62 ViewState["AccessKey"] = value;
\r
66 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
67 [WebSysDescription ("Attribute tags for the Webcontrol.")]
\r
68 public AttributeCollection Attributes
\r
72 if(attributes==null)
\r
74 //FIXME: From where to get StateBag and how? I think this method is OK!
\r
75 if(attributeState == null)
\r
77 attributeState = new StateBag(true);
\r
78 if(IsTrackingViewState)
\r
80 attributeState.TrackViewState();
\r
83 attributes = new AttributeCollection(attributeState);
\r
89 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
90 [TypeConverter (typeof (WebColorConverter))]
91 [WebSysDescription ("The background color for the WebControl.")]
\r
92 public virtual Color BackColor
\r
95 if (!ControlStyleCreated)
\r
97 return ControlStyle.BackColor;
\r
101 ControlStyle.BackColor = value;
\r
105 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
106 [TypeConverter (typeof (WebColorConverter))]
107 [WebSysDescription ("The border color for the WebControl.")]
\r
108 public virtual Color BorderColor
\r
111 if (!ControlStyleCreated)
\r
112 return Color.Empty;
\r
113 return ControlStyle.BorderColor;
\r
117 ControlStyle.BorderColor = value;
\r
121 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
122 [WebSysDescription ("The style/type of the border used for the WebControl.")]
\r
123 public virtual BorderStyle BorderStyle
\r
126 if (!ControlStyleCreated)
\r
127 return BorderStyle.NotSet;
\r
128 return ControlStyle.BorderStyle;
\r
132 ControlStyle.BorderStyle = value;
\r
136 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
137 [WebSysDescription ("The width of the border used for the WebControl.")]
\r
138 public virtual Unit BorderWidth
\r
141 if (!ControlStyleCreated)
\r
143 return ControlStyle.BorderWidth;
\r
147 if (value.Value < 0)
\r
148 throw new ArgumentOutOfRangeException ("value");
\r
149 ControlStyle.BorderWidth = value;
\r
153 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
154 [WebSysDescription ("The style used to display this Webcontrol.")]
\r
155 public Style ControlStyle
\r
159 if(controlStyle == null)
\r
161 controlStyle = CreateControlStyle();
\r
162 if(IsTrackingViewState)
\r
164 controlStyle.TrackViewState();
\r
166 controlStyle.LoadViewState(null);
\r
168 return controlStyle;
\r
172 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173 [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
\r
174 public bool ControlStyleCreated
\r
178 return (controlStyle!=null);
\r
182 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
183 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
\r
184 public virtual string CssClass
\r
188 return ControlStyle.CssClass;
\r
192 ControlStyle.CssClass = value;
\r
196 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
197 [WebSysDescription ("The activation state of this WebControl.")]
198 public virtual bool Enabled {
203 if (enabled != value) {
204 ViewState ["Enabled"] = value;
205 if (IsTrackingViewState)
206 EnableViewState = true;
213 [DefaultValue (null), NotifyParentProperty (true), WebCategory ("Appearance")]
214 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
215 [WebSysDescription ("The font of this WebControl.")]
\r
216 public virtual FontInfo Font
\r
220 return ControlStyle.Font;
\r
224 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
225 [TypeConverter (typeof (WebColorConverter))]
226 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
\r
227 public virtual Color ForeColor
\r
230 if (!ControlStyleCreated)
\r
231 return Color.Empty;
\r
232 return ControlStyle.ForeColor;
\r
236 ControlStyle.ForeColor = value;
\r
240 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
241 [WebSysDescription ("The height of this WebControl.")]
\r
242 public virtual Unit Height
\r
246 return ControlStyle.Height;
\r
251 throw new ArgumentOutOfRangeException ("value");
252 ControlStyle.Height = value;
\r
256 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
257 [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
\r
258 public CssStyleCollection Style
\r
262 return Attributes.CssStyle;
\r
266 [DefaultValue (0), WebCategory ("Behavior")]
267 [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
\r
268 public virtual short TabIndex
\r
272 object o = ViewState["TabIndex"];
\r
279 if(value < short.MinValue || value > short.MaxValue)
\r
280 throw new ArgumentOutOfRangeException ("value");
\r
281 ViewState["TabIndex"] = value;
\r
285 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
286 [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
\r
287 public virtual string ToolTip
\r
291 object o = ViewState["ToolTip"];
\r
294 return String.Empty;
\r
298 ViewState["ToolTip"] = value;
\r
302 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
303 [WebSysDescription ("The width of this WebControl.")]
\r
304 public virtual Unit Width
\r
308 return ControlStyle.Width;
\r
313 throw new ArgumentOutOfRangeException ("value");
314 ControlStyle.Width = value;
\r
318 public void ApplyStyle(Style s)
320 if (s != null && !s.IsEmpty)
321 ControlStyle.CopyFrom (s);
324 public void CopyBaseAttributes(WebControl controlSrc)
\r
327 * AccessKey, Enabled, ToolTip, TabIndex, Attributes
\r
329 AccessKey = controlSrc.AccessKey;
\r
330 Enabled = controlSrc.Enabled;
\r
331 ToolTip = controlSrc.ToolTip;
\r
332 TabIndex = controlSrc.TabIndex;
\r
333 AttributeCollection otherAtt = controlSrc.Attributes;
\r
334 foreach (string key in otherAtt.Keys)
\r
335 Attributes [key] = otherAtt [key];
\r
338 public void MergeStyle(Style s)
\r
340 ControlStyle.MergeWith(s);
\r
343 public virtual void RenderBeginTag(HtmlTextWriter writer)
\r
345 AddAttributesToRender(writer);
\r
346 writer.RenderBeginTag(TagName);
\r
349 public virtual void RenderEndTag(HtmlTextWriter writer)
\r
351 writer.RenderEndTag();
\r
354 protected virtual HtmlTextWriterTag TagKey
\r
362 protected virtual string TagName
\r
366 if(tagName == null && TagKey != 0)
\r
368 tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
\r
370 // What if tagName is null and tagKey 0?
\r
375 protected virtual void AddAttributesToRender(HtmlTextWriter writer)
\r
379 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
\r
381 if(AccessKey.Length>0)
\r
383 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
\r
387 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
\r
389 if(ToolTip.Length>0)
\r
391 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
\r
395 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
\r
397 if(ControlStyleCreated)
\r
399 if(!ControlStyle.IsEmpty)
\r
401 ControlStyle.AddAttributesToRender(writer, this);
\r
404 if(attributeState != null){
\r
405 IEnumerator ie = Attributes.Keys.GetEnumerator ();
\r
406 while (ie.MoveNext ()){
\r
407 string key = (string) ie.Current;
\r
408 writer.AddAttribute (key, Attributes [key]);
\r
413 protected virtual Style CreateControlStyle ()
415 return new Style (ViewState);
418 protected override void LoadViewState (object savedState)
420 if (savedState == null)
423 Pair saved = (Pair) savedState;
424 base.LoadViewState (saved.First);
426 if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
427 ControlStyle.LoadViewState (null);
429 if (saved.Second != null)
431 attributeState = new StateBag(true);
432 attributeState.TrackViewState();
433 attributeState.LoadViewState (saved.Second);
436 object enable = ViewState["Enabled"];
\r
439 Enabled = (bool)enable;
\r
440 EnableViewState = true;
\r
444 protected override void Render(HtmlTextWriter writer)
446 RenderBeginTag (writer);
447 RenderContents (writer);
448 RenderEndTag (writer);
451 protected virtual void RenderContents(HtmlTextWriter writer)
453 base.Render (writer);
456 protected override object SaveViewState()
458 if (EnableViewState)
\r
459 ViewState["Enabled"] = enabled;
460 if (ControlStyleCreated)
461 ControlStyle.SaveViewState ();
463 object baseView = base.SaveViewState ();
464 object attrView = null;
465 if (attributeState != null)
466 attrView = attributeState.SaveViewState ();
468 if (baseView == null && attrView == null)
471 return new Pair (baseView, attrView);
474 protected override void TrackViewState()
476 base.TrackViewState();
477 if (ControlStyleCreated)
478 ControlStyle.TrackViewState ();
479 if (attributeState != null)
480 attributeState.TrackViewState ();
483 string IAttributeAccessor.GetAttribute(string key)
485 if (attributes != null)
486 return Attributes [key] as string;
491 void IAttributeAccessor.SetAttribute(string key, string value)
493 Attributes [key] = value;