HyperLinkColumn.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // System.Web.UI.WebControls.HyperLinkColumn.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. // (C) Gaurav Vaish (2002)
  11. // (C) 2003 Andreas Nahr
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.ComponentModel;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. public class HyperLinkColumn: DataGridColumn
  40. {
  41. PropertyDescriptor textFieldDescriptor;
  42. PropertyDescriptor urlFieldDescriptor;
  43. public HyperLinkColumn ()
  44. {
  45. }
  46. [DefaultValue (""), WebCategory ("Misc")]
  47. [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
  48. public virtual string DataNavigateUrlField {
  49. get {
  50. object o = ViewState ["DataNavigateUrlField"];
  51. if (o != null)
  52. return (string) o;
  53. return String.Empty;
  54. }
  55. set {
  56. ViewState ["DataNavigateUrlField"] = value;
  57. OnColumnChanged ();
  58. }
  59. }
  60. // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
  61. [DefaultValue (""), WebCategory ("Misc")]
  62. [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
  63. public virtual string DataNavigateUrlFormatString {
  64. get {
  65. object o = ViewState ["DataNavigateUrlFormatString"];
  66. if (o != null)
  67. return (string) o;
  68. return String.Empty;
  69. }
  70. set {
  71. ViewState ["DataNavigateUrlFormatString"] = value;
  72. OnColumnChanged ();
  73. }
  74. }
  75. [DefaultValue (""), WebCategory ("Misc")]
  76. [WebSysDescription ("The field that gets data-bound to the Text property.")]
  77. public virtual string DataTextField {
  78. get {
  79. object o = ViewState ["DataTextField"];
  80. if (o != null)
  81. return (string) o;
  82. return String.Empty;
  83. }
  84. set {
  85. ViewState ["DataTextField"] = value;
  86. OnColumnChanged ();
  87. }
  88. }
  89. // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
  90. [DefaultValue (""), WebCategory ("Misc")]
  91. [Description ("The formatting rule for the text content that gets data-bound to the Text property.")]
  92. public virtual string DataTextFormatString {
  93. get {
  94. object o = ViewState ["DataTextFormatString"];
  95. if (o != null)
  96. return (string) o;
  97. return String.Empty;
  98. }
  99. set {
  100. ViewState ["DataTextFormatString"] = value;
  101. OnColumnChanged ();
  102. }
  103. }
  104. [DefaultValue (""), WebCategory ("Misc")]
  105. [WebSysDescription ("The URL that this hyperlink links to.")]
  106. public virtual string NavigateUrl {
  107. get {
  108. object o = ViewState ["NavigateUrl"];
  109. if (o != null)
  110. return (string) o;
  111. return String.Empty;
  112. }
  113. set {
  114. ViewState ["NavigateUrl"] = value;
  115. OnColumnChanged ();
  116. }
  117. }
  118. [DefaultValue (""), WebCategory ("Misc")]
  119. [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
  120. public virtual string Target {
  121. get {
  122. object o = ViewState ["Target"];
  123. if (o != null)
  124. return (string) o;
  125. return String.Empty;
  126. }
  127. set {
  128. ViewState ["Target"] = value;
  129. OnColumnChanged ();
  130. }
  131. }
  132. [DefaultValue (""), WebCategory ("Misc")]
  133. [WebSysDescription ("The Text for the hyperlink.")]
  134. public virtual string Text {
  135. get {
  136. object o = ViewState ["Text"];
  137. if (o != null)
  138. return (string) o;
  139. return String.Empty;
  140. }
  141. set {
  142. ViewState ["Text"] = value;
  143. OnColumnChanged ();
  144. }
  145. }
  146. public override void Initialize ()
  147. {
  148. textFieldDescriptor = null;
  149. urlFieldDescriptor = null;
  150. base.Initialize ();
  151. }
  152. public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
  153. {
  154. base.InitializeCell (cell, columnIndex, itemType);
  155. if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
  156. HyperLink toDisplay = new HyperLink ();
  157. toDisplay.Text = Text;
  158. toDisplay.NavigateUrl = NavigateUrl;
  159. toDisplay.Target = Target;
  160. if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)
  161. toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);
  162. cell.Controls.Add (toDisplay);
  163. }
  164. }
  165. private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
  166. {
  167. HyperLink link = (HyperLink) sender;
  168. object item = ((DataGridItem) link.NamingContainer).DataItem;
  169. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
  170. if (textFieldDescriptor == null)
  171. textFieldDescriptor = properties.Find (DataTextField, true);
  172. if (urlFieldDescriptor == null)
  173. urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
  174. if (DataTextField.Length > 0 && textFieldDescriptor == null && !DesignMode)
  175. throw new HttpException (HttpRuntime.FormatResourceString (
  176. "Field_Not_Found", DataTextField));
  177. if (DataNavigateUrlField.Length > 0 && urlFieldDescriptor == null && !DesignMode)
  178. throw new HttpException (HttpRuntime.FormatResourceString (
  179. "Field_Not_Found", DataNavigateUrlField));
  180. if (textFieldDescriptor != null) {
  181. link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
  182. } else {
  183. link.Text = Text;
  184. }
  185. if (urlFieldDescriptor != null) {
  186. link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
  187. return;
  188. }
  189. if (DataNavigateUrlField.Length != 0 && DesignMode)
  190. link.NavigateUrl = "url";
  191. }
  192. protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)
  193. {
  194. if (dataUrlValue == null)
  195. return String.Empty;
  196. string retVal;
  197. if (DataNavigateUrlFormatString.Length > 0) {
  198. retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);
  199. } else {
  200. retVal = dataUrlValue.ToString ();
  201. }
  202. return retVal;
  203. }
  204. protected virtual string FormatDataTextValue (object dataTextValue)
  205. {
  206. if (dataTextValue == null)
  207. return String.Empty;
  208. string retVal;
  209. if (DataTextFormatString.Length > 0) {
  210. retVal = String.Format (DataTextFormatString, dataTextValue);
  211. } else {
  212. retVal = dataTextValue.ToString ();
  213. }
  214. return retVal;
  215. }
  216. }
  217. }