TemplateColumn.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // System.Web.UI.WebControls.TemplateColumn.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.UI.WebControls
  36. {
  37. public class TemplateColumn : DataGridColumn
  38. {
  39. private ITemplate editItemTemplate;
  40. private ITemplate footerTemplate;
  41. private ITemplate headerTemplate;
  42. private ITemplate itemTemplate;
  43. public TemplateColumn(): base()
  44. {
  45. }
  46. [DefaultValue (null), Browsable (false)]
  47. [PersistenceMode (PersistenceMode.InnerProperty), TemplateContainer (typeof (DataGridItem))]
  48. [WebSysDescription ("The template that is used to build that are being edited rows for this column.")]
  49. public virtual ITemplate EditItemTemplate
  50. {
  51. get
  52. {
  53. return editItemTemplate;
  54. }
  55. set
  56. {
  57. editItemTemplate = value;
  58. OnColumnChanged();
  59. }
  60. }
  61. [DefaultValue (null), Browsable (false)]
  62. [PersistenceMode (PersistenceMode.InnerProperty), TemplateContainer (typeof (DataGridItem))]
  63. [WebSysDescription ("The template that is used to build the footer for this column.")]
  64. public virtual ITemplate FooterTemplate
  65. {
  66. get
  67. {
  68. return footerTemplate;
  69. }
  70. set
  71. {
  72. footerTemplate = value;
  73. OnColumnChanged();
  74. }
  75. }
  76. [DefaultValue (null), Browsable (false)]
  77. [PersistenceMode (PersistenceMode.InnerProperty), TemplateContainer (typeof (DataGridItem))]
  78. [WebSysDescription ("The template that is used to build the header for this column.")]
  79. public virtual ITemplate HeaderTemplate
  80. {
  81. get
  82. {
  83. return headerTemplate;
  84. }
  85. set
  86. {
  87. headerTemplate = value;
  88. OnColumnChanged();
  89. }
  90. }
  91. [DefaultValue (null), Browsable (false)]
  92. [PersistenceMode (PersistenceMode.InnerProperty), TemplateContainer (typeof (DataGridItem))]
  93. [WebSysDescription ("The template that is used to build rows for this column.")]
  94. public virtual ITemplate ItemTemplate
  95. {
  96. get
  97. {
  98. return itemTemplate;
  99. }
  100. set
  101. {
  102. itemTemplate = value;
  103. OnColumnChanged();
  104. }
  105. }
  106. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  107. {
  108. base.InitializeCell(cell, columnIndex, itemType);
  109. ITemplate toRender = null;
  110. switch(itemType)
  111. {
  112. case ListItemType.Header: toRender = headerTemplate;
  113. break;
  114. case ListItemType.Footer: toRender = footerTemplate;
  115. break;
  116. case ListItemType.SelectedItem:
  117. case ListItemType.AlternatingItem:
  118. case ListItemType.Item: toRender = itemTemplate;
  119. break;
  120. case ListItemType.EditItem:
  121. toRender = (editItemTemplate != null ? editItemTemplate : itemTemplate);
  122. break;
  123. }
  124. if(toRender != null)
  125. {
  126. cell.Text = String.Empty;
  127. toRender.InstantiateIn(cell);
  128. }
  129. }
  130. }
  131. }