2 // System.Web.UI.WebControls.DataControlField.cs
5 // Sanjay Gupta (gsanjay@novell.com)
6 // Lluis Sanchez Gual (lluis@novell.com)
8 // (C) 2004 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.ComponentModel;
37 using System.Security.Permissions;
39 namespace System.Web.UI.WebControls {
41 [DefaultPropertyAttribute ("HeaderText")]
42 [TypeConverterAttribute (typeof(ExpandableObjectConverter))]
43 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public abstract class DataControlField : IStateManager, IDataSourceViewSchemaAccessor
47 bool tracking = false;
51 TableItemStyle footerStyle;
52 TableItemStyle headerStyle;
53 TableItemStyle itemStyle;
56 protected DataControlField()
58 viewState = new StateBag ();
61 internal void SetDirty ()
63 viewState.SetDirty (true);
66 protected StateBag ViewState {
67 get { return viewState; }
70 public virtual void ExtractValuesFromCell (IOrderedDictionary dictionary,
71 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
75 public virtual bool Initialize (bool sortingEnabled, Control control)
77 this.sortingEnabled = sortingEnabled;
78 this.control = control;
82 public virtual void InitializeCell (DataControlFieldCell cell,
83 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
85 if (cellType == DataControlCellType.Header)
87 if (HeaderText.Length > 0 && sortingEnabled && SortExpression.Length > 0)
88 cell.Controls.Add ((Control) DataControlButton.CreateButton (String.IsNullOrEmpty (HeaderImageUrl) ? ButtonType.Link : ButtonType.Image, control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, SortExpression, true));
89 else if (HeaderImageUrl.Length > 0) {
90 Image image = new Image ();
91 image.ImageUrl = HeaderImageUrl;
92 cell.Controls.Add (image);
95 cell.Text = HeaderText.Length > 0 ? HeaderText : " ";
97 else if (cellType == DataControlCellType.Footer) {
98 string footerText = FooterText;
99 cell.Text = (footerText.Length > 0) ? footerText : " ";
103 protected internal DataControlField CloneField ()
105 DataControlField field = CreateField ();
106 CopyProperties (field);
110 protected abstract DataControlField CreateField ();
112 protected virtual void CopyProperties (DataControlField newField)
114 newField.AccessibleHeaderText = AccessibleHeaderText;
115 newField.ControlStyle.CopyFrom (ControlStyle);
116 newField.FooterStyle.CopyFrom (FooterStyle);
117 newField.FooterText = FooterText;
118 newField.HeaderImageUrl = HeaderImageUrl;
119 newField.HeaderStyle.CopyFrom (HeaderStyle);
120 newField.HeaderText = HeaderText;
121 newField.InsertVisible = InsertVisible;
122 newField.ItemStyle.CopyFrom (ItemStyle);
123 newField.ShowHeader = ShowHeader;
124 newField.SortExpression = SortExpression;
125 newField.Visible = Visible;
128 protected virtual void OnFieldChanged ()
130 if (FieldChanged != null)
131 FieldChanged (this, EventArgs.Empty);
134 protected virtual void LoadViewState (object savedState)
136 if (savedState == null)
139 object [] states = (object []) savedState;
140 viewState.LoadViewState (states[0]);
142 if (states[1] != null)
143 ((IStateManager)ControlStyle).LoadViewState (states[1]);
144 if (states[2] != null)
145 ((IStateManager)FooterStyle).LoadViewState (states[2]);
146 if (states[3] != null)
147 ((IStateManager)HeaderStyle).LoadViewState (states[3]);
148 if (states[4] != null)
149 ((IStateManager)ItemStyle).LoadViewState (states[4]);
152 protected virtual object SaveViewState()
154 object[] state = new object [5];
155 state [0] = viewState.SaveViewState ();
156 if (controlStyle != null)
157 state [1] = ((IStateManager) controlStyle).SaveViewState ();
158 if (footerStyle != null)
159 state [2] = ((IStateManager) footerStyle).SaveViewState ();
160 if (headerStyle != null)
161 state [3] = ((IStateManager) headerStyle).SaveViewState ();
162 if (itemStyle != null)
163 state [4] = ((IStateManager) itemStyle).SaveViewState ();
165 if (state [0] == null && state [1] == null && state [2] == null &&
166 state [3] == null && state [4] == null)
172 protected virtual void TrackViewState()
174 if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
175 if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
176 if (headerStyle != null) ((IStateManager) headerStyle).TrackViewState ();
177 if (itemStyle != null) ((IStateManager) itemStyle).TrackViewState ();
178 viewState.TrackViewState ();
182 public virtual void ValidateSupportsCallback ()
184 throw new NotSupportedException ("Callback not supported");
187 void IStateManager.LoadViewState(object savedState)
189 LoadViewState(savedState);
192 object IStateManager.SaveViewState()
194 return SaveViewState();
197 void IStateManager.TrackViewState()
202 internal Exception GetNotSupportedPropException (string propName)
204 return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name);
207 internal bool ControlStyleCreated { get { return controlStyle != null; } }
209 internal bool HeaderStyleCreated { get { return headerStyle != null; } }
211 internal bool FooterStyleCreated { get { return footerStyle != null; } }
213 internal bool ItemStyleCreated { get { return itemStyle != null; } }
215 [MonoTODO ("Render this")]
216 [DefaultValueAttribute ("")]
217 [LocalizableAttribute (true)]
218 [WebCategoryAttribute ("Accessibility")]
219 public virtual string AccessibleHeaderText {
221 object val = viewState ["accessibleHeaderText"];
222 return val != null ? (string) val : "";
225 viewState ["accessibleHeaderText"] = value;
230 protected Control Control {
231 get { return control; }
234 [WebCategoryAttribute ("Styles")]
235 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
236 [DefaultValueAttribute (null)]
237 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
238 public Style ControlStyle {
240 if (controlStyle == null) {
241 controlStyle = new Style ();
242 if (IsTrackingViewState)
243 controlStyle.TrackViewState();
249 protected bool DesignMode {
250 get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
253 [DefaultValueAttribute (null)]
254 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
255 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
256 [WebCategoryAttribute ("Styles")]
257 public TableItemStyle FooterStyle {
259 if (footerStyle == null) {
260 footerStyle = new TableItemStyle ();
261 if (IsTrackingViewState)
262 footerStyle.TrackViewState();
268 [LocalizableAttribute (true)]
269 [WebCategoryAttribute ("Appearance")]
271 public virtual string FooterText {
273 object val = viewState ["footerText"];
274 return val != null ? (string) val : "";
277 viewState ["footerText"] = value;
282 [UrlPropertyAttribute]
283 [DefaultValueAttribute ("")]
284 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
285 [WebCategoryAttribute ("Appearance")]
286 public virtual string HeaderImageUrl {
288 object val = viewState ["headerImageUrl"];
289 return val != null ? (string) val : "";
292 viewState ["headerImageUrl"] = value;
297 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
298 [WebCategoryAttribute ("Styles")]
299 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
300 [DefaultValueAttribute (null)]
301 public TableItemStyle HeaderStyle {
303 if (headerStyle == null) {
304 headerStyle = new TableItemStyle ();
305 if (IsTrackingViewState)
306 headerStyle.TrackViewState();
312 [DefaultValueAttribute ("")]
313 [LocalizableAttribute (true)]
314 [WebCategoryAttribute ("Appearance")]
315 public virtual string HeaderText {
317 object val = viewState ["headerText"];
318 return val != null ? (string) val : "";
321 viewState ["headerText"] = value;
326 [WebCategoryAttribute ("Behavior")]
327 [DefaultValueAttribute (true)]
328 public virtual bool InsertVisible {
330 object val = viewState ["InsertVisible"];
331 return val != null ? (bool) val : true;
334 viewState ["InsertVisible"] = value;
339 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
340 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
341 [WebCategoryAttribute ("Styles")]
342 [DefaultValueAttribute (null)]
343 public TableItemStyle ItemStyle {
345 if (itemStyle == null) {
346 itemStyle = new TableItemStyle ();
347 if (IsTrackingViewState)
348 itemStyle.TrackViewState();
354 [WebCategoryAttribute ("Behavior")]
355 [DefaultValueAttribute (true)]
356 public virtual bool ShowHeader {
358 object val = viewState ["showHeader"];
359 return val != null ? (bool) val : true;
362 viewState ["showHeader"] = value;
367 [DefaultValueAttribute ("")]
368 // [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
369 [WebCategoryAttribute ("Behavior")]
370 public virtual string SortExpression {
372 object val = viewState ["sortExpression"];
373 return val != null ? (string) val : "";
376 viewState ["sortExpression"] = value;
381 [WebCategoryAttribute ("Behavior")]
382 [DefaultValueAttribute (true)]
383 public bool Visible {
385 object val = viewState ["visible"];
386 return val != null ? (bool) val : true;
389 if (value == Visible)
391 viewState ["visible"] = value;
396 protected bool IsTrackingViewState
398 get { return tracking; }
401 bool IStateManager.IsTrackingViewState
403 get { return IsTrackingViewState; }
406 object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
407 get { return viewState ["dataSourceViewSchema"]; }
409 viewState ["dataSourceViewSchema"] = value;
413 internal event EventHandler FieldChanged;
415 public override string ToString ()
417 if (string.IsNullOrEmpty (HeaderText))
418 return base.ToString ();