ImageField.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // System.Web.UI.WebControls.ImageField.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Web.UI;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. using System.Reflection;
  36. namespace System.Web.UI.WebControls {
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class ImageField : DataControlField
  40. {
  41. public static readonly string ThisExpression = "!";
  42. PropertyDescriptor imageProperty;
  43. PropertyDescriptor textProperty;
  44. public override bool Initialize (bool sortingEnabled, Control control)
  45. {
  46. return base.Initialize (sortingEnabled, control);
  47. }
  48. [DefaultValueAttribute ("")]
  49. [WebCategoryAttribute ("Appearance")]
  50. [LocalizableAttribute (true)]
  51. public virtual string AlternateText {
  52. get {
  53. object ob = ViewState ["AlternateText"];
  54. if (ob != null) return (string) ob;
  55. return string.Empty;
  56. }
  57. set {
  58. ViewState ["AlternateText"] = value;
  59. OnFieldChanged ();
  60. }
  61. }
  62. [DefaultValueAttribute (true)]
  63. [WebCategoryAttribute ("Behavior")]
  64. public virtual bool ConvertEmptyStringToNull {
  65. get {
  66. object ob = ViewState ["ConvertEmptyStringToNull"];
  67. if (ob != null) return (bool) ob;
  68. return true;
  69. }
  70. set {
  71. ViewState ["ConvertEmptyStringToNull"] = value;
  72. OnFieldChanged ();
  73. }
  74. }
  75. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  76. [WebCategoryAttribute ("Data")]
  77. [DefaultValueAttribute ("")]
  78. public virtual string DataAlternateTextField {
  79. get {
  80. object ob = ViewState ["DataAlternateTextField"];
  81. if (ob != null) return (string) ob;
  82. return "";
  83. }
  84. set {
  85. ViewState ["DataAlternateTextField"] = value;
  86. OnFieldChanged ();
  87. }
  88. }
  89. [WebCategoryAttribute ("Data")]
  90. [DefaultValueAttribute ("")]
  91. public virtual string DataAlternateTextFormatString {
  92. get {
  93. object ob = ViewState ["DataAlternateTextFormatString"];
  94. if (ob != null) return (string) ob;
  95. return "";
  96. }
  97. set {
  98. ViewState ["DataAlternateTextFormatString"] = value;
  99. OnFieldChanged ();
  100. }
  101. }
  102. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  103. [WebCategoryAttribute ("Data")]
  104. [DefaultValueAttribute ("")]
  105. public virtual string DataImageUrlField {
  106. get {
  107. object ob = ViewState ["DataImageUrlField"];
  108. if (ob != null) return (string) ob;
  109. return "";
  110. }
  111. set {
  112. ViewState ["DataImageUrlField"] = value;
  113. OnFieldChanged ();
  114. }
  115. }
  116. [WebCategoryAttribute ("Data")]
  117. [DefaultValueAttribute ("")]
  118. public virtual string DataImageUrlFormatString {
  119. get {
  120. object ob = ViewState ["DataImageUrlFormatString"];
  121. if (ob != null) return (string) ob;
  122. return "";
  123. }
  124. set {
  125. ViewState ["DataImageUrlFormatString"] = value;
  126. OnFieldChanged ();
  127. }
  128. }
  129. [DefaultValueAttribute ("")]
  130. [WebCategoryAttribute ("Behavior")]
  131. [LocalizableAttribute (true)]
  132. public virtual string NullDisplayText {
  133. get {
  134. object ob = ViewState ["NullDisplayText"];
  135. if (ob != null) return (string) ob;
  136. return "";
  137. }
  138. set {
  139. ViewState ["NullDisplayText"] = value;
  140. OnFieldChanged ();
  141. }
  142. }
  143. [DefaultValueAttribute ("")]
  144. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  145. [UrlPropertyAttribute]
  146. [WebCategoryAttribute ("Behavior")]
  147. public virtual string NullImageUrl {
  148. get {
  149. object ob = ViewState ["NullImageUrl"];
  150. if (ob != null) return (string) ob;
  151. return "";
  152. }
  153. set {
  154. ViewState ["NullImageUrl"] = value;
  155. OnFieldChanged ();
  156. }
  157. }
  158. [WebCategoryAttribute ("Behavior")]
  159. [DefaultValueAttribute (false)]
  160. public bool ReadOnly {
  161. get {
  162. object val = ViewState ["ReadOnly"];
  163. return val != null ? (bool) val : false;
  164. }
  165. set {
  166. ViewState ["ReadOnly"] = value;
  167. OnFieldChanged ();
  168. }
  169. }
  170. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  171. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  172. {
  173. if ((ReadOnly && !includeReadOnly) || cell.Controls.Count == 0) return;
  174. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  175. if (editable && !ReadOnly) {
  176. TextBox box = cell.Controls [0] as TextBox;
  177. dictionary [DataImageUrlField] = box.Text;
  178. } else if (includeReadOnly) {
  179. Image img = cell.Controls [0] as Image;
  180. dictionary [DataImageUrlField] = img.ImageUrl;
  181. }
  182. }
  183. public override void InitializeCell (DataControlFieldCell cell,
  184. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  185. {
  186. base.InitializeCell (cell, cellType, rowState, rowIndex);
  187. if (cellType == DataControlCellType.DataCell) {
  188. InitializeDataCell (cell, rowState);
  189. if ((rowState & DataControlRowState.Insert) == 0)
  190. cell.DataBinding += new EventHandler (OnDataBindField);
  191. }
  192. }
  193. public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  194. {
  195. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  196. if (editable && !ReadOnly) {
  197. TextBox box = new TextBox ();
  198. cell.Controls.Add (box);
  199. } else {
  200. Image img = new Image ();
  201. cell.Controls.Add (img);
  202. }
  203. }
  204. protected virtual string FormatImageUrlValue (object value)
  205. {
  206. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  207. return NullImageUrl;
  208. else if (DataImageUrlFormatString.Length > 0)
  209. return string.Format (DataImageUrlFormatString, value);
  210. else
  211. return value.ToString ();
  212. }
  213. protected virtual string GetFormattedAlternateText (Control controlContainer)
  214. {
  215. if (DataAlternateTextField.Length > 0)
  216. {
  217. if (textProperty == null)
  218. textProperty = GetProperty (controlContainer, DataAlternateTextField);
  219. object value = GetValue (controlContainer, DataAlternateTextField, ref textProperty);
  220. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  221. return NullDisplayText;
  222. else if (DataAlternateTextFormatString.Length > 0)
  223. return string.Format (DataAlternateTextFormatString, value);
  224. else
  225. return value.ToString ();
  226. }
  227. else
  228. return AlternateText;
  229. }
  230. protected virtual object GetValue (Control controlContainer, string fieldName, ref PropertyDescriptor cachedDescriptor)
  231. {
  232. if (DesignMode)
  233. return GetDesignTimeValue ();
  234. else {
  235. if (fieldName == ThisExpression)
  236. return controlContainer.ToString ();
  237. else {
  238. IDataItemContainer dic = (IDataItemContainer) controlContainer;
  239. if (cachedDescriptor != null) return cachedDescriptor.GetValue (dic.DataItem);
  240. PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
  241. return prop.GetValue (dic.DataItem);
  242. }
  243. }
  244. }
  245. PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
  246. {
  247. IDataItemContainer dic = (IDataItemContainer) controlContainer;
  248. PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
  249. if (prop == null)
  250. new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
  251. return prop;
  252. }
  253. protected virtual string GetDesignTimeValue ()
  254. {
  255. return "Databound";
  256. }
  257. protected virtual void OnDataBindField (object sender, EventArgs e)
  258. {
  259. DataControlFieldCell cell = (DataControlFieldCell) sender;
  260. if (imageProperty == null)
  261. imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
  262. Control c = cell.Controls [0];
  263. if (c is TextBox) {
  264. object val = GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty);
  265. ((TextBox)c).Text = val != null ? val.ToString() : "";
  266. }
  267. else if (c is Image) {
  268. Image img = (Image)c;
  269. img.ImageUrl = FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty));
  270. img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
  271. }
  272. }
  273. public override void ValidateSupportsCallback ()
  274. {
  275. }
  276. protected override DataControlField CreateField ()
  277. {
  278. return new ImageField ();
  279. }
  280. protected override void CopyProperties (DataControlField newField)
  281. {
  282. base.CopyProperties (newField);
  283. ImageField field = (ImageField) newField;
  284. field.AlternateText = AlternateText;
  285. field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  286. field.DataAlternateTextField = DataAlternateTextField;
  287. field.DataAlternateTextFormatString = DataAlternateTextFormatString;
  288. field.DataImageUrlField = DataImageUrlField;
  289. field.DataImageUrlFormatString = DataImageUrlFormatString;
  290. field.NullDisplayText = NullDisplayText;
  291. field.NullImageUrl = NullImageUrl;
  292. field.ReadOnly = ReadOnly;
  293. }
  294. }
  295. }
  296. #endif