2 // System.Web.UI.WebControls.ImageField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37 using System.Reflection;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class ImageField : DataControlField
45 public static readonly string ThisExpression = "!";
47 PropertyDescriptor imageProperty;
48 PropertyDescriptor textProperty;
50 public override bool Initialize (bool sortingEnabled, Control control)
52 return base.Initialize (sortingEnabled, control);
55 [DefaultValueAttribute ("")]
56 [LocalizableAttribute (true)]
57 [WebSysDescription ("")]
58 [WebCategoryAttribute ("Appearance")]
59 public virtual string AlternateText {
61 object ob = ViewState ["AlternateText"];
62 if (ob != null) return (string) ob;
66 ViewState ["AlternateText"] = value;
71 [DefaultValueAttribute (true)]
72 [WebSysDescription ("")]
73 [WebCategoryAttribute ("Behavior")]
74 public virtual bool ConvertEmptyStringToNull {
76 object ob = ViewState ["ConvertEmptyStringToNull"];
77 if (ob != null) return (bool) ob;
81 ViewState ["ConvertEmptyStringToNull"] = value;
86 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
87 [DefaultValueAttribute ("")]
88 [WebSysDescription ("")]
89 [WebCategoryAttribute ("Data")]
90 public virtual string DataAlternateTextField {
92 object ob = ViewState ["DataAlternateTextField"];
93 if (ob != null) return (string) ob;
97 ViewState ["DataAlternateTextField"] = value;
102 [DefaultValueAttribute ("")]
103 [WebSysDescription ("")]
104 [WebCategoryAttribute ("Data")]
105 public virtual string DataAlternateTextFormatString {
107 object ob = ViewState ["DataAlternateTextFormatString"];
108 if (ob != null) return (string) ob;
112 ViewState ["DataAlternateTextFormatString"] = value;
117 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
118 [DefaultValueAttribute ("")]
119 [WebSysDescription ("")]
120 [WebCategoryAttribute ("Data")]
121 public virtual string DataImageUrlField {
123 object ob = ViewState ["DataImageUrlField"];
124 if (ob != null) return (string) ob;
128 ViewState ["DataImageUrlField"] = value;
133 [DefaultValueAttribute ("")]
134 [WebSysDescription ("")]
135 [WebCategoryAttribute ("Data")]
136 public virtual string DataImageUrlFormatString {
138 object ob = ViewState ["DataImageUrlFormatString"];
139 if (ob != null) return (string) ob;
143 ViewState ["DataImageUrlFormatString"] = value;
148 [DefaultValueAttribute ("")]
149 [LocalizableAttribute (true)]
150 [WebSysDescription ("")]
151 [WebCategoryAttribute ("Behavior")]
152 public virtual string NullDisplayText {
154 object ob = ViewState ["NullDisplayText"];
155 if (ob != null) return (string) ob;
159 ViewState ["NullDisplayText"] = value;
164 [DefaultValueAttribute ("")]
165 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
166 [UrlPropertyAttribute]
167 [WebSysDescription ("")]
168 [WebCategoryAttribute ("Behavior")]
169 public virtual string NullImageUrl {
171 object ob = ViewState ["NullImageUrl"];
172 if (ob != null) return (string) ob;
176 ViewState ["NullImageUrl"] = value;
181 [DefaultValueAttribute (false)]
182 [WebSysDescription ("")]
183 [WebCategoryAttribute ("Behavior")]
184 public virtual bool ReadOnly {
186 object val = ViewState ["ReadOnly"];
187 return val != null ? (bool) val : false;
190 ViewState ["ReadOnly"] = value;
195 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
196 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
198 if ((ReadOnly && !includeReadOnly) || cell.Controls.Count == 0) return;
200 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
201 if (includeReadOnly || editable) {
202 Control control = cell.Controls [0];
203 //TODO: other controls?
204 if (control is Image)
205 dictionary [DataImageUrlField] = ((Image)control).ImageUrl;
207 if (control is TextBox)
208 dictionary [DataImageUrlField] = ((TextBox) control).Text;
212 public override void InitializeCell (DataControlFieldCell cell,
213 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
215 base.InitializeCell (cell, cellType, rowState, rowIndex);
216 if (cellType == DataControlCellType.DataCell) {
217 InitializeDataCell (cell, rowState);
218 if ((rowState & DataControlRowState.Insert) == 0)
219 cell.DataBinding += new EventHandler (OnDataBindField);
223 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
225 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
226 if (editable && !ReadOnly) {
227 TextBox box = new TextBox ();
228 cell.Controls.Add (box);
230 else if (DataImageUrlField.Length > 0) {
231 Image img = new Image ();
232 img.ControlStyle.CopyFrom (ControlStyle);
233 cell.Controls.Add (img);
237 protected virtual string FormatImageUrlValue (object value)
241 else if (DataImageUrlFormatString.Length > 0)
242 return string.Format (DataImageUrlFormatString, value);
244 return value.ToString ();
247 protected virtual string GetFormattedAlternateText (Control controlContainer)
249 if (DataAlternateTextField.Length > 0)
251 if (textProperty == null)
252 textProperty = GetProperty (controlContainer, DataAlternateTextField);
254 object value = GetValue (controlContainer, DataAlternateTextField, ref textProperty);
256 if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
257 return NullDisplayText;
258 else if (DataAlternateTextFormatString.Length > 0)
259 return string.Format (DataAlternateTextFormatString, value);
261 return value.ToString ();
264 return AlternateText;
268 protected virtual object GetValue (Control controlContainer, string fieldName, ref PropertyDescriptor cachedDescriptor)
271 return GetDesignTimeValue ();
273 object dataItem = DataBinder.GetDataItem (controlContainer);
274 if (dataItem == null)
275 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
276 if (fieldName == ThisExpression)
279 if (cachedDescriptor != null) return cachedDescriptor.GetValue (dataItem);
280 PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
281 return prop.GetValue (dataItem);
286 PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
288 IDataItemContainer dic = (IDataItemContainer) controlContainer;
289 PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
291 new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
295 protected virtual string GetDesignTimeValue ()
300 protected virtual void OnDataBindField (object sender, EventArgs e)
302 DataControlFieldCell cell = (DataControlFieldCell) sender;
\r
304 if (cell.Controls.Count == 0)
\r
307 if (imageProperty == null)
308 imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
310 Control c = cell.Controls [0];
312 object val = GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty);
313 ((TextBox)c).Text = val != null ? val.ToString() : "";
315 else if (c is Image) {
316 Image img = (Image)c;
317 string value = FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty));
318 if (value == null || (ConvertEmptyStringToNull && value.ToString ().Length == 0)) {
319 if (NullImageUrl == null || NullImageUrl.Length == 0) {
321 Label label = new Label ();
322 label.Text = NullDisplayText;
323 cell.Controls.Add (label);
326 value = NullImageUrl;
328 img.ImageUrl = value;
329 img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
333 public override void ValidateSupportsCallback ()
337 protected override DataControlField CreateField ()
339 return new ImageField ();
342 protected override void CopyProperties (DataControlField newField)
344 base.CopyProperties (newField);
345 ImageField field = (ImageField) newField;
346 field.AlternateText = AlternateText;
347 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
348 field.DataAlternateTextField = DataAlternateTextField;
349 field.DataAlternateTextFormatString = DataAlternateTextFormatString;
350 field.DataImageUrlField = DataImageUrlField;
351 field.DataImageUrlFormatString = DataImageUrlFormatString;
352 field.NullDisplayText = NullDisplayText;
353 field.NullImageUrl = NullImageUrl;
354 field.ReadOnly = ReadOnly;