HyperLinkField.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // System.Web.UI.WebControls.HyperLinkField.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 HyperLinkField : DataControlField
  40. {
  41. PropertyDescriptor textProperty;
  42. PropertyDescriptor[] urlProperties;
  43. static string[] emptyFields;
  44. public override bool Initialize (bool sortingEnabled, Control control)
  45. {
  46. return base.Initialize (sortingEnabled, control);
  47. }
  48. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  49. [TypeConverterAttribute (typeof(StringArrayConverter))]
  50. [WebCategoryAttribute ("Data")]
  51. [DefaultValueAttribute ("")]
  52. public virtual string[] DataNavigateUrlFields {
  53. get {
  54. object ob = ViewState ["DataNavigateUrlFields"];
  55. if (ob != null) return (string[]) ob;
  56. if (emptyFields == null) emptyFields = new string[0];
  57. return emptyFields;
  58. }
  59. set {
  60. ViewState ["DataNavigateUrlFields"] = value;
  61. OnFieldChanged ();
  62. }
  63. }
  64. [DefaultValueAttribute ("")]
  65. [WebCategoryAttribute ("Data")]
  66. public virtual string DataNavigateUrlFormatString {
  67. get {
  68. object ob = ViewState ["DataNavigateUrlFormatString"];
  69. if (ob != null) return (string) ob;
  70. return "";
  71. }
  72. set {
  73. ViewState ["DataNavigateUrlFormatString"] = value;
  74. OnFieldChanged ();
  75. }
  76. }
  77. [WebCategoryAttribute ("Data")]
  78. [DefaultValueAttribute ("")]
  79. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  80. public virtual string DataTextField {
  81. get {
  82. object ob = ViewState ["DataTextField"];
  83. if (ob != null) return (string) ob;
  84. return "";
  85. }
  86. set {
  87. ViewState ["DataTextField"] = value;
  88. OnFieldChanged ();
  89. }
  90. }
  91. [DefaultValueAttribute ("")]
  92. [WebCategoryAttribute ("Data")]
  93. public virtual string DataTextFormatString {
  94. get {
  95. object ob = ViewState ["DataTextFormatString"];
  96. if (ob != null) return (string) ob;
  97. return "";
  98. }
  99. set {
  100. ViewState ["DataTextFormatString"] = value;
  101. OnFieldChanged ();
  102. }
  103. }
  104. [DefaultValueAttribute ("")]
  105. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  106. [UrlPropertyAttribute]
  107. [WebCategoryAttribute ("Behavior")]
  108. public virtual string NavigateUrl {
  109. get {
  110. object ob = ViewState ["NavigateUrl"];
  111. if (ob != null) return (string) ob;
  112. return "";
  113. }
  114. set {
  115. ViewState ["NavigateUrl"] = value;
  116. OnFieldChanged ();
  117. }
  118. }
  119. [DefaultValueAttribute ("")]
  120. [WebCategoryAttribute ("Behavior")]
  121. [TypeConverterAttribute (typeof(TargetConverter))]
  122. public virtual string Target {
  123. get {
  124. object ob = ViewState ["Target"];
  125. if (ob != null) return (string) ob;
  126. return "";
  127. }
  128. set {
  129. ViewState ["Target"] = value;
  130. OnFieldChanged ();
  131. }
  132. }
  133. [LocalizableAttribute (true)]
  134. [DefaultValueAttribute ("")]
  135. [WebCategoryAttribute ("Appearance")]
  136. public virtual string Text {
  137. get {
  138. object ob = ViewState ["Text"];
  139. if (ob != null) return (string) ob;
  140. return "";
  141. }
  142. set {
  143. ViewState ["Text"] = value;
  144. OnFieldChanged ();
  145. }
  146. }
  147. public override void InitializeCell (DataControlFieldCell cell,
  148. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  149. {
  150. base.InitializeCell (cell, cellType, rowState, rowIndex);
  151. HyperLink link = new HyperLink ();
  152. bool bind = false;
  153. if (Target.Length > 0)
  154. link.Target = Target;
  155. if (DataTextField.Length > 0)
  156. bind = true;
  157. else
  158. link.Text = Text;
  159. string[] fields = DataNavigateUrlFields;
  160. if (fields.Length > 0)
  161. bind = true;
  162. else
  163. link.NavigateUrl = NavigateUrl;
  164. if (bind && cellType == DataControlCellType.DataCell && (rowState & DataControlRowState.Insert) == 0)
  165. cell.DataBinding += new EventHandler (OnDataBindField);
  166. cell.Controls.Add (link);
  167. }
  168. protected virtual string FormatDataNavigateUrlValue (object[] dataUrlValues)
  169. {
  170. if (dataUrlValues == null || dataUrlValues.Length == 0)
  171. return "";
  172. else if (DataNavigateUrlFormatString.Length > 0)
  173. return string.Format (DataNavigateUrlFormatString, dataUrlValues);
  174. else
  175. return dataUrlValues[0].ToString ();
  176. }
  177. protected virtual string FormatDataTextValue (object dataTextValue)
  178. {
  179. if (DataTextFormatString.Length > 0)
  180. return string.Format (DataTextFormatString, dataTextValue);
  181. else if (dataTextValue == null)
  182. return "";
  183. else
  184. return dataTextValue.ToString ();
  185. }
  186. void OnDataBindField (object sender, EventArgs e)
  187. {
  188. DataControlFieldCell cell = (DataControlFieldCell) sender;
  189. HyperLink link = (HyperLink) cell.Controls [0];
  190. object controlContainer = cell.BindingContainer;
  191. object item = DataBinder.GetDataItem (controlContainer);
  192. if (DataTextField.Length > 0) {
  193. if (textProperty == null) SetupProperties (controlContainer);
  194. link.Text = FormatDataTextValue (textProperty.GetValue (item));
  195. }
  196. string[] urlFields = DataNavigateUrlFields;
  197. if (urlFields.Length > 0) {
  198. if (urlProperties == null) SetupProperties (controlContainer);
  199. object[] dataUrlValues = new object [urlFields.Length];
  200. for (int n=0; n<dataUrlValues.Length; n++)
  201. dataUrlValues [n] = urlProperties [n].GetValue (item);
  202. link.NavigateUrl = FormatDataNavigateUrlValue (dataUrlValues);
  203. }
  204. }
  205. void SetupProperties (object controlContainer)
  206. {
  207. object item = DataBinder.GetDataItem (controlContainer);
  208. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (item);
  209. if (DataTextField.Length > 0) {
  210. textProperty = props [DataTextField];
  211. if (textProperty == null)
  212. new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + item.GetType());
  213. }
  214. string[] urlFields = DataNavigateUrlFields;
  215. if (urlFields.Length > 0) {
  216. urlProperties = new PropertyDescriptor [urlFields.Length];
  217. for (int n=0; n<urlFields.Length; n++) {
  218. PropertyDescriptor prop = props [urlFields [n]];
  219. if (prop == null)
  220. new InvalidOperationException ("Property '" + urlFields [n] + "' not found in object of type " + item.GetType());
  221. urlProperties [n] = prop;
  222. }
  223. }
  224. }
  225. protected override DataControlField CreateField ()
  226. {
  227. return new HyperLinkField ();
  228. }
  229. protected override void CopyProperties (DataControlField newField)
  230. {
  231. base.CopyProperties (newField);
  232. HyperLinkField field = (HyperLinkField) newField;
  233. field.DataNavigateUrlFields = DataNavigateUrlFields;
  234. field.DataNavigateUrlFormatString = DataNavigateUrlFormatString;
  235. field.DataTextField = DataTextField;
  236. field.DataTextFormatString = DataTextFormatString;
  237. field.NavigateUrl = NavigateUrl;
  238. field.Target = Target;
  239. field.Text = Text;
  240. }
  241. public override void ValidateSupportsCallback ()
  242. {
  243. }
  244. }
  245. }
  246. #endif