ImageField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. [LocalizableAttribute (true)]
  50. [WebSysDescription ("")]
  51. [WebCategoryAttribute ("Appearance")]
  52. public virtual string AlternateText {
  53. get {
  54. object ob = ViewState ["AlternateText"];
  55. if (ob != null) return (string) ob;
  56. return string.Empty;
  57. }
  58. set {
  59. ViewState ["AlternateText"] = value;
  60. OnFieldChanged ();
  61. }
  62. }
  63. [DefaultValueAttribute (true)]
  64. [WebSysDescription ("")]
  65. [WebCategoryAttribute ("Behavior")]
  66. public virtual bool ConvertEmptyStringToNull {
  67. get {
  68. object ob = ViewState ["ConvertEmptyStringToNull"];
  69. if (ob != null) return (bool) ob;
  70. return true;
  71. }
  72. set {
  73. ViewState ["ConvertEmptyStringToNull"] = value;
  74. OnFieldChanged ();
  75. }
  76. }
  77. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  78. [DefaultValueAttribute ("")]
  79. [WebSysDescription ("")]
  80. [WebCategoryAttribute ("Data")]
  81. public virtual string DataAlternateTextField {
  82. get {
  83. object ob = ViewState ["DataAlternateTextField"];
  84. if (ob != null) return (string) ob;
  85. return "";
  86. }
  87. set {
  88. ViewState ["DataAlternateTextField"] = value;
  89. OnFieldChanged ();
  90. }
  91. }
  92. [DefaultValueAttribute ("")]
  93. [WebSysDescription ("")]
  94. [WebCategoryAttribute ("Data")]
  95. public virtual string DataAlternateTextFormatString {
  96. get {
  97. object ob = ViewState ["DataAlternateTextFormatString"];
  98. if (ob != null) return (string) ob;
  99. return "";
  100. }
  101. set {
  102. ViewState ["DataAlternateTextFormatString"] = value;
  103. OnFieldChanged ();
  104. }
  105. }
  106. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  107. [DefaultValueAttribute ("")]
  108. [WebSysDescription ("")]
  109. [WebCategoryAttribute ("Data")]
  110. public virtual string DataImageUrlField {
  111. get {
  112. object ob = ViewState ["DataImageUrlField"];
  113. if (ob != null) return (string) ob;
  114. return "";
  115. }
  116. set {
  117. ViewState ["DataImageUrlField"] = value;
  118. OnFieldChanged ();
  119. }
  120. }
  121. [DefaultValueAttribute ("")]
  122. [WebSysDescription ("")]
  123. [WebCategoryAttribute ("Data")]
  124. public virtual string DataImageUrlFormatString {
  125. get {
  126. object ob = ViewState ["DataImageUrlFormatString"];
  127. if (ob != null) return (string) ob;
  128. return "";
  129. }
  130. set {
  131. ViewState ["DataImageUrlFormatString"] = value;
  132. OnFieldChanged ();
  133. }
  134. }
  135. [DefaultValueAttribute ("")]
  136. [LocalizableAttribute (true)]
  137. [WebSysDescription ("")]
  138. [WebCategoryAttribute ("Behavior")]
  139. public virtual string NullDisplayText {
  140. get {
  141. object ob = ViewState ["NullDisplayText"];
  142. if (ob != null) return (string) ob;
  143. return "";
  144. }
  145. set {
  146. ViewState ["NullDisplayText"] = value;
  147. OnFieldChanged ();
  148. }
  149. }
  150. [DefaultValueAttribute ("")]
  151. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  152. [UrlPropertyAttribute]
  153. [WebSysDescription ("")]
  154. [WebCategoryAttribute ("Behavior")]
  155. public virtual string NullImageUrl {
  156. get {
  157. object ob = ViewState ["NullImageUrl"];
  158. if (ob != null) return (string) ob;
  159. return "";
  160. }
  161. set {
  162. ViewState ["NullImageUrl"] = value;
  163. OnFieldChanged ();
  164. }
  165. }
  166. [DefaultValueAttribute (false)]
  167. [WebSysDescription ("")]
  168. [WebCategoryAttribute ("Behavior")]
  169. public virtual bool ReadOnly {
  170. get {
  171. object val = ViewState ["ReadOnly"];
  172. return val != null ? (bool) val : false;
  173. }
  174. set {
  175. ViewState ["ReadOnly"] = value;
  176. OnFieldChanged ();
  177. }
  178. }
  179. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  180. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  181. {
  182. if ((ReadOnly && !includeReadOnly) || cell.Controls.Count == 0) return;
  183. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  184. if (includeReadOnly || editable) {
  185. Control control = cell.Controls [0];
  186. //TODO: other controls?
  187. if (control is Image)
  188. dictionary [DataImageUrlField] = ((Image)control).ImageUrl;
  189. else
  190. if (control is TextBox)
  191. dictionary [DataImageUrlField] = ((TextBox) control).Text;
  192. }
  193. }
  194. public override void InitializeCell (DataControlFieldCell cell,
  195. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  196. {
  197. base.InitializeCell (cell, cellType, rowState, rowIndex);
  198. if (cellType == DataControlCellType.DataCell) {
  199. InitializeDataCell (cell, rowState);
  200. if ((rowState & DataControlRowState.Insert) == 0)
  201. cell.DataBinding += new EventHandler (OnDataBindField);
  202. }
  203. }
  204. protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  205. {
  206. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  207. if (editable && !ReadOnly) {
  208. TextBox box = new TextBox ();
  209. cell.Controls.Add (box);
  210. }
  211. else if (DataImageUrlField.Length > 0) {
  212. Image img = new Image ();
  213. img.ControlStyle.CopyFrom (ControlStyle);
  214. cell.Controls.Add (img);
  215. }
  216. }
  217. protected virtual string FormatImageUrlValue (object value)
  218. {
  219. if (value == null)
  220. return null;
  221. else if (DataImageUrlFormatString.Length > 0)
  222. return string.Format (DataImageUrlFormatString, value);
  223. else
  224. return value.ToString ();
  225. }
  226. protected virtual string GetFormattedAlternateText (Control controlContainer)
  227. {
  228. if (DataAlternateTextField.Length > 0)
  229. {
  230. if (textProperty == null)
  231. textProperty = GetProperty (controlContainer, DataAlternateTextField);
  232. object value = GetValue (controlContainer, DataAlternateTextField, ref textProperty);
  233. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  234. return NullDisplayText;
  235. else if (DataAlternateTextFormatString.Length > 0)
  236. return string.Format (DataAlternateTextFormatString, value);
  237. else
  238. return value.ToString ();
  239. }
  240. else
  241. return AlternateText;
  242. }
  243. protected virtual object GetValue (Control controlContainer, string fieldName, ref PropertyDescriptor cachedDescriptor)
  244. {
  245. if (DesignMode)
  246. return GetDesignTimeValue ();
  247. else {
  248. object dataItem = DataBinder.GetDataItem (controlContainer);
  249. if (dataItem == null)
  250. throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
  251. if (fieldName == ThisExpression)
  252. return dataItem.ToString ();
  253. else {
  254. if (cachedDescriptor != null) return cachedDescriptor.GetValue (dataItem);
  255. PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
  256. return prop.GetValue (dataItem);
  257. }
  258. }
  259. }
  260. PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
  261. {
  262. IDataItemContainer dic = (IDataItemContainer) controlContainer;
  263. PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
  264. if (prop == null)
  265. new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
  266. return prop;
  267. }
  268. protected virtual string GetDesignTimeValue ()
  269. {
  270. return "Databound";
  271. }
  272. protected virtual void OnDataBindField (object sender, EventArgs e)
  273. {
  274. DataControlFieldCell cell = (DataControlFieldCell) sender;
  275. if (cell.Controls.Count == 0)
  276. return;
  277. if (imageProperty == null)
  278. imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
  279. Control c = cell.Controls [0];
  280. if (c is TextBox) {
  281. object val = GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty);
  282. ((TextBox)c).Text = val != null ? val.ToString() : "";
  283. }
  284. else if (c is Image) {
  285. Image img = (Image)c;
  286. string value = FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty));
  287. if (value == null || (ConvertEmptyStringToNull && value.ToString ().Length == 0)) {
  288. if (NullImageUrl == null || NullImageUrl.Length == 0) {
  289. c.Visible = false;
  290. Label label = new Label ();
  291. label.Text = NullDisplayText;
  292. cell.Controls.Add (label);
  293. }
  294. else
  295. value = NullImageUrl;
  296. }
  297. img.ImageUrl = value;
  298. img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
  299. }
  300. }
  301. public override void ValidateSupportsCallback ()
  302. {
  303. }
  304. protected override DataControlField CreateField ()
  305. {
  306. return new ImageField ();
  307. }
  308. protected override void CopyProperties (DataControlField newField)
  309. {
  310. base.CopyProperties (newField);
  311. ImageField field = (ImageField) newField;
  312. field.AlternateText = AlternateText;
  313. field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  314. field.DataAlternateTextField = DataAlternateTextField;
  315. field.DataAlternateTextFormatString = DataAlternateTextFormatString;
  316. field.DataImageUrlField = DataImageUrlField;
  317. field.DataImageUrlFormatString = DataImageUrlFormatString;
  318. field.NullDisplayText = NullDisplayText;
  319. field.NullImageUrl = NullImageUrl;
  320. field.ReadOnly = ReadOnly;
  321. }
  322. }
  323. }
  324. #endif