HyperLinkColumn.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. using System;
  14. using System.ComponentModel;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class HyperLinkColumn: DataGridColumn
  20. {
  21. PropertyDescriptor textFieldDescriptor;
  22. PropertyDescriptor urlFieldDescriptor;
  23. public HyperLinkColumn ()
  24. {
  25. }
  26. [DefaultValue (""), WebCategory ("Misc")]
  27. [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
  28. public virtual string DataNavigateUrlField {
  29. get {
  30. object o = ViewState ["DataNavigateUrlField"];
  31. if (o != null)
  32. return (string) o;
  33. return String.Empty;
  34. }
  35. set {
  36. ViewState ["DataNavigateUrlField"] = value;
  37. OnColumnChanged ();
  38. }
  39. }
  40. // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
  41. [DefaultValue (""), WebCategory ("Misc")]
  42. [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
  43. public virtual string DataNavigateUrlFormatString {
  44. get {
  45. object o = ViewState ["DataNavigateUrlFormatString"];
  46. if (o != null)
  47. return (string) o;
  48. return String.Empty;
  49. }
  50. set {
  51. ViewState ["DataNavigateUrlFormatString"] = value;
  52. OnColumnChanged ();
  53. }
  54. }
  55. [DefaultValue (""), WebCategory ("Misc")]
  56. [WebSysDescription ("The field that gets data-bound to the Text property.")]
  57. public virtual string DataTextField {
  58. get {
  59. object o = ViewState ["DataTextField"];
  60. if (o != null)
  61. return (string) o;
  62. return String.Empty;
  63. }
  64. set {
  65. ViewState ["DataTextField"] = value;
  66. OnColumnChanged ();
  67. }
  68. }
  69. // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
  70. [DefaultValue (""), WebCategory ("Misc")]
  71. [Description ("The formatting rule for the text content that gets data-bound to the Text property.")]
  72. public virtual string DataTextFormatString {
  73. get {
  74. object o = ViewState ["DataTextFormatString"];
  75. if (o != null)
  76. return (string) o;
  77. return String.Empty;
  78. }
  79. set {
  80. ViewState ["DataTextFormatString"] = value;
  81. OnColumnChanged ();
  82. }
  83. }
  84. [DefaultValue (""), WebCategory ("Misc")]
  85. [WebSysDescription ("The URL that this hyperlink links to.")]
  86. public virtual string NavigateUrl {
  87. get {
  88. object o = ViewState ["NavigateUrl"];
  89. if (o != null)
  90. return (string) o;
  91. return String.Empty;
  92. }
  93. set {
  94. ViewState ["NavigateUrl"] = value;
  95. OnColumnChanged ();
  96. }
  97. }
  98. [DefaultValue (""), WebCategory ("Misc")]
  99. [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
  100. public virtual string Target {
  101. get {
  102. object o = ViewState ["Target"];
  103. if (o != null)
  104. return (string) o;
  105. return String.Empty;
  106. }
  107. set {
  108. ViewState ["Target"] = value;
  109. OnColumnChanged ();
  110. }
  111. }
  112. [DefaultValue (""), WebCategory ("Misc")]
  113. [WebSysDescription ("The Text for the hyperlink.")]
  114. public virtual string Text {
  115. get {
  116. object o = ViewState ["Text"];
  117. if (o != null)
  118. return (string) o;
  119. return String.Empty;
  120. }
  121. set {
  122. ViewState ["Text"] = value;
  123. OnColumnChanged ();
  124. }
  125. }
  126. public override void Initialize ()
  127. {
  128. textFieldDescriptor = null;
  129. urlFieldDescriptor = null;
  130. base.Initialize ();
  131. }
  132. public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
  133. {
  134. base.InitializeCell (cell, columnIndex, itemType);
  135. if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
  136. HyperLink toDisplay = new HyperLink ();
  137. toDisplay.Text = Text;
  138. toDisplay.NavigateUrl = NavigateUrl;
  139. toDisplay.Target = Target;
  140. if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)
  141. toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);
  142. cell.Controls.Add (toDisplay);
  143. }
  144. }
  145. private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
  146. {
  147. HyperLink link = (HyperLink) sender;
  148. object item = ((DataGridItem) link.NamingContainer).DataItem;
  149. if (textFieldDescriptor == null && urlFieldDescriptor == null) {
  150. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
  151. textFieldDescriptor = properties.Find (DataTextField, true);
  152. if (textFieldDescriptor == null && !DesignMode)
  153. throw new HttpException (HttpRuntime.FormatResourceString (
  154. "Field_Not_Found", DataTextField));
  155. urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
  156. if (urlFieldDescriptor == null && !DesignMode)
  157. throw new HttpException (HttpRuntime.FormatResourceString (
  158. "Field_Not_Found", DataNavigateUrlField));
  159. }
  160. if (textFieldDescriptor != null) {
  161. link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
  162. } else {
  163. link.Text = "Sample_DataBound_Text";
  164. }
  165. if (urlFieldDescriptor != null) {
  166. link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
  167. } else {
  168. link.NavigateUrl = "url";
  169. }
  170. }
  171. protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)
  172. {
  173. if (dataUrlValue == null)
  174. return String.Empty;
  175. string retVal;
  176. if (DataNavigateUrlFormatString.Length > 0) {
  177. retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);
  178. } else {
  179. retVal = dataUrlValue.ToString ();
  180. }
  181. return retVal;
  182. }
  183. protected virtual string FormatDataTextValue (object dataTextValue)
  184. {
  185. if (dataTextValue == null)
  186. return String.Empty;
  187. string retVal;
  188. if (DataTextFormatString.Length > 0) {
  189. retVal = String.Format (DataTextFormatString, dataTextValue);
  190. } else {
  191. retVal = dataTextValue.ToString ();
  192. }
  193. return retVal;
  194. }
  195. }
  196. }