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 (new DataControlButton (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 [MonoTODO ("Render this")]
208 [DefaultValueAttribute ("")]
209 [LocalizableAttribute (true)]
210 [WebCategoryAttribute ("Accessibility")]
211 public virtual string AccessibleHeaderText {
213 object val = viewState ["accessibleHeaderText"];
214 return val != null ? (string) val : "";
217 viewState ["accessibleHeaderText"] = value;
222 protected Control Control {
223 get { return control; }
226 [WebCategoryAttribute ("Styles")]
227 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
228 [DefaultValueAttribute (null)]
229 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
230 public Style ControlStyle {
232 if (controlStyle == null) {
233 controlStyle = new Style ();
234 if (IsTrackingViewState)
235 controlStyle.TrackViewState();
241 protected bool DesignMode {
242 get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
245 [DefaultValueAttribute (null)]
246 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
247 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
248 [WebCategoryAttribute ("Styles")]
249 public TableItemStyle FooterStyle {
251 if (footerStyle == null) {
252 footerStyle = new TableItemStyle ();
253 if (IsTrackingViewState)
254 footerStyle.TrackViewState();
260 [LocalizableAttribute (true)]
261 [WebCategoryAttribute ("Appearance")]
263 public virtual string FooterText {
265 object val = viewState ["footerText"];
266 return val != null ? (string) val : "";
269 viewState ["footerText"] = value;
274 [UrlPropertyAttribute]
275 [DefaultValueAttribute ("")]
276 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
277 [WebCategoryAttribute ("Appearance")]
278 public virtual string HeaderImageUrl {
280 object val = viewState ["headerImageUrl"];
281 return val != null ? (string) val : "";
284 viewState ["headerImageUrl"] = value;
289 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
290 [WebCategoryAttribute ("Styles")]
291 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
292 [DefaultValueAttribute (null)]
293 public TableItemStyle HeaderStyle {
295 if (headerStyle == null) {
296 headerStyle = new TableItemStyle ();
297 if (IsTrackingViewState)
298 headerStyle.TrackViewState();
304 [DefaultValueAttribute ("")]
305 [LocalizableAttribute (true)]
306 [WebCategoryAttribute ("Appearance")]
307 public virtual string HeaderText {
309 object val = viewState ["headerText"];
310 return val != null ? (string) val : "";
313 viewState ["headerText"] = value;
318 [WebCategoryAttribute ("Behavior")]
319 [DefaultValueAttribute (true)]
320 public virtual bool InsertVisible {
322 object val = viewState ["InsertVisible"];
323 return val != null ? (bool) val : true;
326 viewState ["InsertVisible"] = value;
331 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
332 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
333 [WebCategoryAttribute ("Styles")]
334 [DefaultValueAttribute (null)]
335 public TableItemStyle ItemStyle {
337 if (itemStyle == null) {
338 itemStyle = new TableItemStyle ();
339 if (IsTrackingViewState)
340 itemStyle.TrackViewState();
346 [WebCategoryAttribute ("Behavior")]
347 [DefaultValueAttribute (true)]
348 public virtual bool ShowHeader {
350 object val = viewState ["showHeader"];
351 return val != null ? (bool) val : true;
354 viewState ["showHeader"] = value;
359 [DefaultValueAttribute ("")]
360 // [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
361 [WebCategoryAttribute ("Behavior")]
362 public virtual string SortExpression {
364 object val = viewState ["sortExpression"];
365 return val != null ? (string) val : "";
368 viewState ["sortExpression"] = value;
373 [WebCategoryAttribute ("Behavior")]
374 [DefaultValueAttribute (true)]
375 public bool Visible {
377 object val = viewState ["visible"];
378 return val != null ? (bool) val : true;
381 viewState ["visible"] = value;
386 protected bool IsTrackingViewState
388 get { return tracking; }
391 bool IStateManager.IsTrackingViewState
393 get { return IsTrackingViewState; }
396 object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
397 get { return viewState ["dataSourceViewSchema"]; }
399 viewState ["dataSourceViewSchema"] = value;
403 internal event EventHandler FieldChanged;
405 public override string ToString ()
407 if (string.IsNullOrEmpty (HeaderText))
408 return base.ToString ();