UserFormFieldDesigner.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:6k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : UserFormFieldDesigner.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Web.UI;
  13. using System.Web.UI.WebControls;
  14. using System.Web.UI.Design;
  15. using System.Drawing;
  16. namespace Itenso.WebUserForms.Controls
  17. {
  18. // ------------------------------------------------------------------------
  19. public class UserFormFieldDesigner : ControlDesigner
  20. {
  21. // ----------------------------------------------------------------------
  22. public UserFormFieldDesigner()
  23. {
  24. } // UserFormFieldDesigner
  25. // ----------------------------------------------------------------------
  26. protected Control DesignControl
  27. {
  28. get { return base.Component as Control; }
  29. } // DesignControl
  30. // ----------------------------------------------------------------------
  31. protected Color DefaultFieldNameColor
  32. {
  33. get { return this.defaultFieldNameColor; }
  34. set { this.defaultFieldNameColor = value; }
  35. } // DefaultFieldNameColor
  36. // ----------------------------------------------------------------------
  37. protected Color MissingFieldNameColor
  38. {
  39. get { return this.missingFieldNameColor; }
  40. set { this.missingFieldNameColor = value; }
  41. } // MissingFieldNameColor
  42. // ----------------------------------------------------------------------
  43. protected Color InvalidFieldNameColor
  44. {
  45. get { return this.invalidFieldNameColor; }
  46. set { this.invalidFieldNameColor = value; }
  47. } // InvalidFieldNameColor
  48. // ----------------------------------------------------------------------
  49. protected string FieldName
  50. {
  51. get
  52. {
  53. IUserFormField webformField = base.Component as IUserFormField;
  54. if ( webformField == null )
  55. {
  56. throw new InvalidOperationException( "IUserFormField required" );
  57. }
  58. return webformField.FieldName;
  59. }
  60. } // FieldName
  61. // ----------------------------------------------------------------------
  62. protected string FieldValue
  63. {
  64. get
  65. {
  66. IUserFormField webformField = base.Component as IUserFormField;
  67. if ( webformField == null )
  68. {
  69. throw new InvalidOperationException( "IUserFormField required" );
  70. }
  71. return webformField.FieldValue;
  72. }
  73. } // FieldValue
  74. // ----------------------------------------------------------------------
  75. protected bool HasFieldName
  76. {
  77. get { return !string.IsNullOrEmpty( FieldName ); }
  78. } // HasFieldName
  79. // ----------------------------------------------------------------------
  80. protected bool HasFieldValue
  81. {
  82. get { return !string.IsNullOrEmpty( FieldValue ); }
  83. } // HasFieldValue
  84. // ----------------------------------------------------------------------
  85. protected Control ConflictingControl
  86. {
  87. get
  88. {
  89. Control designControl = DesignControl;
  90. if ( designControl == null )
  91. {
  92. return null;
  93. }
  94. Page page = designControl.Page;
  95. if ( page == null )
  96. {
  97. return null;
  98. }
  99. return FindConflictingControl( page.Controls, FieldName );
  100. }
  101. } // ConflictingControl
  102. // ----------------------------------------------------------------------
  103. protected bool HasConfilictingControl
  104. {
  105. get { return ConflictingControl != null; }
  106. } // HasConfilictingControl
  107. // ----------------------------------------------------------------------
  108. public override string GetDesignTimeHtml()
  109. {
  110. Control designControl = DesignControl;
  111. if ( designControl == null )
  112. {
  113. return base.GetDesignTimeHtml();
  114. }
  115. WebControl designWebControl = designControl as WebControl;
  116. if ( designWebControl != null )
  117. {
  118. designWebControl.BackColor = GetStatusColor();
  119. }
  120. return GetRenderHtml( designControl );
  121. } // GetDesignTimeHtml
  122. // ----------------------------------------------------------------------
  123. protected virtual Color GetStatusColor()
  124. {
  125. if ( !HasFieldName )
  126. {
  127. return MissingFieldNameColor;
  128. }
  129. if ( HasConfilictingControl )
  130. {
  131. return InvalidFieldNameColor;
  132. }
  133. return DefaultFieldNameColor;
  134. } // GetStatusColor
  135. // ----------------------------------------------------------------------
  136. protected virtual string GetRenderHtml( Control control )
  137. {
  138. if ( control is WebControl )
  139. {
  140. StringWriter text = new StringWriter();
  141. HtmlTextWriter writer = new HtmlTextWriter( text );
  142. control.RenderControl( writer );
  143. return text.ToString();
  144. }
  145. else
  146. {
  147. StringBuilder sb = new StringBuilder();
  148. string fieldName = FieldName;
  149. if ( !string.IsNullOrEmpty( fieldName ) )
  150. {
  151. sb.Append( fieldName );
  152. }
  153. string fieldValue = FieldValue;
  154. if ( !string.IsNullOrEmpty( fieldValue ) )
  155. {
  156. if ( sb.Length != 0 )
  157. {
  158. sb.Append( " - " );
  159. }
  160. sb.Append( fieldValue );
  161. }
  162. return CreatePlaceHolderDesignTimeHtml( sb.ToString() );
  163. }
  164. } // GetRenderHtml
  165. // ----------------------------------------------------------------------
  166. private Control FindConflictingControl( ControlCollection controls, string fieldName )
  167. {
  168. if ( controls == null || controls.Count == 0 )
  169. {
  170. return null;
  171. }
  172. foreach ( Control control in controls )
  173. {
  174. IUserFormField formField = control as IUserFormField;
  175. if ( formField != null )
  176. {
  177. if ( fieldName.Equals( formField.FieldName ) && control != DesignControl )
  178. {
  179. return control;
  180. }
  181. }
  182. if ( control.Controls.Count > 0 )
  183. {
  184. Control subControl = FindConflictingControl( control.Controls, fieldName );
  185. if ( subControl != null )
  186. {
  187. return subControl;
  188. }
  189. }
  190. }
  191. return null;
  192. } // FindConflictingControl
  193. // ----------------------------------------------------------------------
  194. // members
  195. private Color defaultFieldNameColor = Color.LightGreen;
  196. private Color missingFieldNameColor = Color.Red;
  197. private Color invalidFieldNameColor = Color.DarkSalmon;
  198. } // class UserFormFieldDesigner
  199. } // namespace Itenso.WebUserForms.Controls
  200. // -- EOF -------------------------------------------------------------------