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 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.ComponentModel;
39 using System.Collections.Specialized;
41 namespace System.Web.UI.WebControls
43 [PersistChildrenAttribute(false)]
44 [ParseChildrenAttribute(true)]
45 public class WebControl : Control, IAttributeAccessor
47 HtmlTextWriterTag tagKey;
48 AttributeCollection attributes;
49 StateBag attributeState;
54 protected WebControl () : this (HtmlTextWriterTag.Span)
58 public WebControl (HtmlTextWriterTag tag)
63 protected WebControl (string tag)
68 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
69 [WebSysDescription ("A keyboard shortcut for the WebControl.")]
\r
70 public virtual string AccessKey
\r
74 object o = ViewState["AccessKey"];
\r
77 return String.Empty;
\r
81 if (value != null && value.Length > 1)
82 throw new ArgumentOutOfRangeException ("value");
83 ViewState["AccessKey"] = value;
\r
87 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88 [WebSysDescription ("Attribute tags for the Webcontrol.")]
\r
89 public AttributeCollection Attributes
\r
93 if(attributes==null)
\r
95 //FIXME: From where to get StateBag and how? I think this method is OK!
\r
96 if(attributeState == null)
\r
98 attributeState = new StateBag(true);
\r
99 if(IsTrackingViewState)
\r
101 attributeState.TrackViewState();
\r
104 attributes = new AttributeCollection(attributeState);
\r
110 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
111 [TypeConverter (typeof (WebColorConverter))]
112 [WebSysDescription ("The background color for the WebControl.")]
\r
113 public virtual Color BackColor
\r
116 if (!ControlStyleCreated)
\r
117 return Color.Empty;
\r
118 return ControlStyle.BackColor;
\r
122 ControlStyle.BackColor = value;
\r
126 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
127 [TypeConverter (typeof (WebColorConverter))]
128 [WebSysDescription ("The border color for the WebControl.")]
\r
129 public virtual Color BorderColor
\r
132 if (!ControlStyleCreated)
\r
133 return Color.Empty;
\r
134 return ControlStyle.BorderColor;
\r
138 ControlStyle.BorderColor = value;
\r
142 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
143 [WebSysDescription ("The style/type of the border used for the WebControl.")]
\r
144 public virtual BorderStyle BorderStyle
\r
147 if (!ControlStyleCreated)
\r
148 return BorderStyle.NotSet;
\r
149 return ControlStyle.BorderStyle;
\r
153 ControlStyle.BorderStyle = value;
\r
157 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
158 [WebSysDescription ("The width of the border used for the WebControl.")]
\r
159 public virtual Unit BorderWidth
\r
162 if (!ControlStyleCreated)
\r
164 return ControlStyle.BorderWidth;
\r
168 if (value.Value < 0)
\r
169 throw new ArgumentOutOfRangeException ("value");
\r
170 ControlStyle.BorderWidth = value;
\r
174 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
175 [WebSysDescription ("The style used to display this Webcontrol.")]
\r
176 public Style ControlStyle
\r
180 if(controlStyle == null)
\r
182 controlStyle = CreateControlStyle();
\r
183 if(IsTrackingViewState)
\r
185 controlStyle.TrackViewState();
\r
187 controlStyle.LoadViewState(null);
\r
189 return controlStyle;
\r
193 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
194 [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
\r
195 public bool ControlStyleCreated
\r
199 return (controlStyle!=null);
\r
203 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
204 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
\r
205 public virtual string CssClass
\r
209 return ControlStyle.CssClass;
\r
213 ControlStyle.CssClass = value;
\r
217 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
218 [WebSysDescription ("The activation state of this WebControl.")]
219 public virtual bool Enabled {
224 if (enabled != value) {
225 ViewState ["Enabled"] = value;
226 if (IsTrackingViewState)
227 EnableViewState = true;
234 [DefaultValue (null), NotifyParentProperty (true), WebCategory ("Appearance")]
235 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
236 [WebSysDescription ("The font of this WebControl.")]
\r
237 public virtual FontInfo Font
\r
241 return ControlStyle.Font;
\r
245 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
246 [TypeConverter (typeof (WebColorConverter))]
247 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
\r
248 public virtual Color ForeColor
\r
251 if (!ControlStyleCreated)
\r
252 return Color.Empty;
\r
253 return ControlStyle.ForeColor;
\r
257 ControlStyle.ForeColor = value;
\r
261 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
262 [WebSysDescription ("The height of this WebControl.")]
\r
263 public virtual Unit Height
\r
267 return ControlStyle.Height;
\r
272 throw new ArgumentOutOfRangeException ("value");
273 ControlStyle.Height = value;
\r
277 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
278 [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
\r
279 public CssStyleCollection Style
\r
283 return Attributes.CssStyle;
\r
287 [DefaultValue (0), WebCategory ("Behavior")]
288 [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
\r
289 public virtual short TabIndex
\r
293 object o = ViewState["TabIndex"];
\r
300 if(value < short.MinValue || value > short.MaxValue)
\r
301 throw new ArgumentOutOfRangeException ("value");
\r
302 ViewState["TabIndex"] = value;
\r
306 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
307 [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
\r
308 public virtual string ToolTip
\r
312 object o = ViewState["ToolTip"];
\r
315 return String.Empty;
\r
319 ViewState["ToolTip"] = value;
\r
323 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
324 [WebSysDescription ("The width of this WebControl.")]
\r
325 public virtual Unit Width
\r
329 return ControlStyle.Width;
\r
334 throw new ArgumentOutOfRangeException ("value");
335 ControlStyle.Width = value;
\r
339 public void ApplyStyle(Style s)
341 if (s != null && !s.IsEmpty)
342 ControlStyle.CopyFrom (s);
345 public void CopyBaseAttributes(WebControl controlSrc)
\r
348 * AccessKey, Enabled, ToolTip, TabIndex, Attributes
\r
350 AccessKey = controlSrc.AccessKey;
\r
351 Enabled = controlSrc.Enabled;
\r
352 ToolTip = controlSrc.ToolTip;
\r
353 TabIndex = controlSrc.TabIndex;
\r
354 AttributeCollection otherAtt = controlSrc.Attributes;
\r
355 foreach (string key in otherAtt.Keys)
\r
356 Attributes [key] = otherAtt [key];
\r
359 public void MergeStyle(Style s)
\r
361 ControlStyle.MergeWith(s);
\r
364 public virtual void RenderBeginTag(HtmlTextWriter writer)
\r
366 AddAttributesToRender(writer);
\r
367 writer.RenderBeginTag(TagName);
\r
370 public virtual void RenderEndTag(HtmlTextWriter writer)
\r
372 writer.RenderEndTag();
\r
376 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
377 protected virtual HtmlTextWriterTag TagKey
\r
386 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
387 protected virtual string TagName
\r
391 if(tagName == null && TagKey != 0)
\r
393 tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
\r
395 // What if tagName is null and tagKey 0?
\r
400 protected virtual void AddAttributesToRender(HtmlTextWriter writer)
\r
404 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
\r
406 if(AccessKey.Length>0)
\r
408 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
\r
412 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
\r
414 if(ToolTip.Length>0)
\r
416 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
\r
420 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
\r
422 if(ControlStyleCreated)
\r
424 if(!ControlStyle.IsEmpty)
\r
426 ControlStyle.AddAttributesToRender(writer, this);
\r
429 if(attributeState != null){
\r
430 IEnumerator ie = Attributes.Keys.GetEnumerator ();
\r
431 while (ie.MoveNext ()){
\r
432 string key = (string) ie.Current;
\r
433 writer.AddAttribute (key, Attributes [key]);
\r
438 protected virtual Style CreateControlStyle ()
440 return new Style (ViewState);
443 protected override void LoadViewState (object savedState)
445 if (savedState == null)
448 Pair saved = (Pair) savedState;
449 base.LoadViewState (saved.First);
451 if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
452 ControlStyle.LoadViewState (null);
454 if (saved.Second != null)
456 if (attributeState == null)
\r
458 attributeState = new StateBag(true);
459 attributeState.TrackViewState();
\r
461 attributeState.LoadViewState (saved.Second);
464 object enable = ViewState["Enabled"];
\r
467 Enabled = (bool)enable;
\r
468 EnableViewState = true;
\r
472 protected override void Render(HtmlTextWriter writer)
474 RenderBeginTag (writer);
475 RenderContents (writer);
476 RenderEndTag (writer);
479 protected virtual void RenderContents(HtmlTextWriter writer)
481 base.Render (writer);
484 protected override object SaveViewState()
486 if (EnableViewState)
\r
487 ViewState["Enabled"] = enabled;
488 if (ControlStyleCreated)
489 ControlStyle.SaveViewState ();
491 object baseView = base.SaveViewState ();
492 object attrView = null;
493 if (attributeState != null)
494 attrView = attributeState.SaveViewState ();
496 if (baseView == null && attrView == null)
499 return new Pair (baseView, attrView);
502 protected override void TrackViewState()
504 base.TrackViewState();
505 if (ControlStyleCreated)
506 ControlStyle.TrackViewState ();
507 if (attributeState != null)
508 attributeState.TrackViewState ();
511 string IAttributeAccessor.GetAttribute(string key)
513 if (attributes != null)
514 return Attributes [key] as string;
519 void IAttributeAccessor.SetAttribute(string key, string value)
521 Attributes [key] = value;