2
0

AutoGeneratedField.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Web.UI.WebControls.AutoGeneratedField.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. namespace System.Web.UI.WebControls {
  36. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public sealed class AutoGeneratedField : BoundField
  40. {
  41. Type dataType = typeof(string);
  42. internal AutoGeneratedField ()
  43. {
  44. }
  45. public AutoGeneratedField (string dataField)
  46. {
  47. DataField = dataField;
  48. }
  49. internal AutoGeneratedField (AutoGeneratedFieldProperties fieldProperties)
  50. {
  51. DataField = fieldProperties.DataField;
  52. DataType = fieldProperties.Type;
  53. SortExpression = fieldProperties.Name;
  54. HeaderText = fieldProperties.Name;
  55. ReadOnly = fieldProperties.IsReadOnly;
  56. }
  57. public Type DataType {
  58. get { return dataType; }
  59. set { dataType = value; }
  60. }
  61. public override bool ConvertEmptyStringToNull {
  62. get { return true; }
  63. set { throw new NotSupportedException (); }
  64. }
  65. public override string DataFormatString {
  66. get { return string.Empty; }
  67. set { throw new NotSupportedException (); }
  68. }
  69. public override bool InsertVisible {
  70. get { return true; }
  71. set { throw new NotSupportedException (); }
  72. }
  73. [MonoTODO ("Support other data types")]
  74. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  75. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  76. {
  77. if (dataType == typeof(bool)) {
  78. bool editable = IsEditable (rowState);
  79. if (editable || includeReadOnly) {
  80. CheckBox box = (CheckBox) cell.Controls [0];
  81. dictionary [DataField] = box.Checked;
  82. }
  83. } else
  84. base.ExtractValuesFromCell (dictionary, cell, rowState, includeReadOnly);
  85. }
  86. [MonoTODO ("Support other data types")]
  87. protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  88. {
  89. if (dataType == typeof(bool)) {
  90. bool editable = IsEditable (rowState);
  91. CheckBox box = new CheckBox ();
  92. box.Enabled = editable;
  93. box.ToolTip = HeaderText;
  94. cell.Controls.Add (box);
  95. } else
  96. base.InitializeDataCell (cell, rowState);
  97. }
  98. [MonoTODO ("Support other data types")]
  99. protected override void OnDataBindField (object sender, EventArgs e)
  100. {
  101. DataControlFieldCell cell = (DataControlFieldCell) sender;
  102. if (dataType == typeof(bool)) {
  103. CheckBox box = (CheckBox) cell.Controls [0];
  104. object val = GetValue (cell.BindingContainer);
  105. if (val != null && val != DBNull.Value)
  106. box.Checked = (bool)val;
  107. else
  108. if (string.IsNullOrEmpty (DataField)) {
  109. box.Visible = false;
  110. return;
  111. }
  112. if (!box.Visible)
  113. box.Visible = true;
  114. }
  115. else
  116. base.OnDataBindField (sender, e);
  117. }
  118. public override void ValidateSupportsCallback ()
  119. {
  120. }
  121. protected override DataControlField CreateField ()
  122. {
  123. return new AutoGeneratedField ();
  124. }
  125. protected override void CopyProperties (DataControlField newField)
  126. {
  127. base.CopyProperties (newField);
  128. AutoGeneratedField field = (AutoGeneratedField) newField;
  129. field.DataType = DataType;
  130. }
  131. protected override object GetDesignTimeValue()
  132. {
  133. return base.GetDesignTimeValue ();
  134. }
  135. }
  136. }
  137. #endif