HyperLinkColumn.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // System.Web.UI.WebControls.HyperLinkColumn.cs
  3. //
  4. // Author: Duncan Mak ([email protected])
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  26. //
  27. using System.ComponentModel;
  28. using System.Data;
  29. namespace System.Web.UI.WebControls
  30. {
  31. public class HyperLinkColumn : DataGridColumn
  32. {
  33. public HyperLinkColumn ()
  34. {
  35. }
  36. [DefaultValue("")]
  37. [WebSysDescription ("")]
  38. [WebCategory ("Misc")]
  39. public virtual string DataNavigateUrlField {
  40. get {
  41. return ViewState.GetString ("DataNavigateUrlField", String.Empty);
  42. }
  43. set { ViewState ["DataNavigateUrlField"] = value; }
  44. }
  45. [DefaultValue("")]
  46. [Description("The formatting applied to the value bound to the NavigateUrl property.")]
  47. [WebCategory ("Misc")]
  48. public virtual string DataNavigateUrlFormatString {
  49. get {
  50. return ViewState.GetString ("DataNavigateUrlFormatString", String.Empty);
  51. }
  52. set { ViewState ["DataNavigateUrlFormatString"] = value; }
  53. }
  54. [DefaultValue("")]
  55. [WebSysDescription ("")]
  56. [WebCategory ("Misc")]
  57. public virtual string DataTextField {
  58. get {
  59. return ViewState.GetString ("DataTextField", String.Empty);
  60. }
  61. set { ViewState ["DataTextField"] = value; }
  62. }
  63. [Description("The formatting applied to the value bound to the Text property.")]
  64. [DefaultValue("")]
  65. [WebCategory ("Misc")]
  66. public virtual string DataTextFormatString {
  67. get {
  68. return ViewState.GetString ("DataTextFormatString", String.Empty);
  69. }
  70. set { ViewState ["DataTextFormatString"] = value; }
  71. }
  72. [DefaultValue("")]
  73. [WebSysDescription ("")]
  74. [WebCategory ("Misc")]
  75. public virtual string NavigateUrl {
  76. get {
  77. return ViewState.GetString ("NavigateUrl", String.Empty);
  78. }
  79. set { ViewState ["NavigateUrl"] = value; }
  80. }
  81. [DefaultValue("")]
  82. [WebSysDescription ("")]
  83. [WebCategory ("Misc")]
  84. public virtual string Target {
  85. get {
  86. return ViewState.GetString ("Target", String.Empty);
  87. }
  88. set { ViewState ["Target"] = value; }
  89. }
  90. [DefaultValue("")]
  91. [WebSysDescription ("")]
  92. [WebCategory ("Misc")]
  93. public virtual string Text {
  94. get {
  95. return ViewState.GetString ("Text", String.Empty);
  96. }
  97. set { ViewState ["Text"] = value; }
  98. }
  99. protected virtual string FormatDataNavigateUrlValue (object value)
  100. {
  101. string format = DataNavigateUrlFormatString;
  102. if (format == "")
  103. format = null;
  104. return DataBinder.FormatResult (value, format);
  105. }
  106. protected virtual string FormatDataTextValue (object value)
  107. {
  108. string format = DataTextFormatString;
  109. if (format == "")
  110. format = null;
  111. return DataBinder.FormatResult (value, format);
  112. }
  113. public override void Initialize ()
  114. {
  115. base.Initialize ();
  116. }
  117. void ItemDataBinding (object sender, EventArgs args)
  118. {
  119. TableCell cell = (TableCell)sender;
  120. HyperLink ctrl = (HyperLink)cell.Controls[0];
  121. DataGridItem item = (DataGridItem)cell.NamingContainer;
  122. if (DataNavigateUrlField != "")
  123. ctrl.NavigateUrl = FormatDataNavigateUrlValue (DataBinder.Eval (item.DataItem, DataNavigateUrlField));
  124. else
  125. ctrl.NavigateUrl = NavigateUrl;
  126. if (DataTextField != "")
  127. ctrl.Text = FormatDataTextValue (DataBinder.Eval (item.DataItem, DataTextField));
  128. else
  129. ctrl.Text = Text;
  130. ctrl.Target = Target;
  131. }
  132. public override void InitializeCell (TableCell cell, int column_index, ListItemType item_type)
  133. {
  134. base.InitializeCell (cell, column_index, item_type);
  135. switch (item_type)
  136. {
  137. case ListItemType.Separator:
  138. case ListItemType.Pager:
  139. case ListItemType.Footer:
  140. case ListItemType.Header: {
  141. // Base handles header and footer, dunno about the others
  142. return;
  143. }
  144. case ListItemType.Item:
  145. case ListItemType.EditItem:
  146. case ListItemType.AlternatingItem:
  147. cell.DataBinding += ItemDataBinding;
  148. cell.Controls.Add (new HyperLink ());
  149. break;
  150. }
  151. }
  152. }
  153. }