You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.HtmlControls;
  9. using System.Data;
  10. using System.Collections;
  11. namespace OT.Controls
  12. {
  13. [ValidationPropertyAttribute("Text")]
  14. public class AmtTextBox : CompositeControl
  15. {
  16. #region Private Member
  17. private TextBox m_TextBox = new TextBox(); //多狀態文本框
  18. private HiddenField m_HidStats; //檢測狀態
  19. public enum AmtType //枚舉類型Digital(數字)、Letter(字母)、Email(郵件地址)
  20. {
  21. Digital = 0,
  22. Letter = 1,
  23. Email = 2
  24. }
  25. private string stronblur = string.Empty;//onblur事件
  26. #endregion
  27. #region Public property
  28. [Bindable(true), Category("Appearance"), Description("類型選擇"), Localizable(true),]
  29. //定义属性-選擇類型
  30. public virtual AmtType TextBoxType
  31. {
  32. get
  33. {
  34. object obj = ViewState["TextBoxType"];
  35. return (obj == null) ? AmtType.Digital : (AmtType)obj;
  36. }
  37. set { ViewState["TextBoxType"] = value; }
  38. }
  39. [Bindable(true), Description("檢測狀態"), Localizable(true), Browsable(false)]
  40. //定义属性-返回檢測狀態
  41. public string isStats
  42. {
  43. get
  44. {
  45. return (m_HidStats == null) ? "Y" : m_HidStats.Value;
  46. }
  47. set { m_HidStats.Value = value; }
  48. }
  49. //定义属性-返回值
  50. public string Text
  51. {
  52. get
  53. {
  54. return m_TextBox.Text.Replace(",", "");
  55. }
  56. set
  57. {
  58. if (value != "" && TextBoxType == AmtType.Digital)
  59. {
  60. if (!value.Trim().Equals("-"))
  61. {
  62. m_TextBox.Text = Convert.ToDecimal(value).ToString("###,##0");
  63. }
  64. }
  65. else
  66. {
  67. m_TextBox.Text = value;
  68. }
  69. }
  70. }
  71. public string onblur
  72. {
  73. set
  74. {
  75. stronblur = value;
  76. }
  77. }
  78. public override string CssClass
  79. {
  80. get
  81. {
  82. return m_TextBox.CssClass;
  83. }
  84. set
  85. {
  86. m_TextBox.CssClass = value;
  87. }
  88. }
  89. public override Unit Width
  90. {
  91. get
  92. {
  93. return m_TextBox.Width;
  94. }
  95. set
  96. {
  97. m_TextBox.Width = value;
  98. }
  99. }
  100. //最大長度
  101. public int MaxLength
  102. {
  103. get
  104. {
  105. return m_TextBox.MaxLength;
  106. }
  107. set { m_TextBox.MaxLength = value; }
  108. }
  109. //Readonly
  110. public bool ReadOnly
  111. {
  112. get
  113. {
  114. return m_TextBox.ReadOnly;
  115. }
  116. set { m_TextBox.ReadOnly = value; }
  117. }
  118. [Bindable(true), Category("Behavior"), Description("是否可以為空"), Localizable(true)]
  119. public bool isNULL
  120. {
  121. get
  122. {
  123. object obj = ViewState[ClientID + "isNULL"];
  124. return (obj == null) ? true : (bool)obj;
  125. }
  126. set
  127. {
  128. ViewState[ClientID + "isNULL"] = value;
  129. }
  130. }
  131. #endregion
  132. #region Override Function
  133. protected override void OnInit(EventArgs e)
  134. {
  135. if (this.Page != null)
  136. {
  137. //引入內嵌腳本資源
  138. Page.ClientScript.RegisterClientScriptResource(this.GetType(), "OT.Controls.Js.AmtTextBox.js");
  139. }
  140. base.OnInit(e);
  141. }
  142. protected override void OnLoad(EventArgs e)
  143. {
  144. base.OnLoad(e);
  145. if (Page.IsPostBack)
  146. {
  147. }
  148. }
  149. protected override void RecreateChildControls()
  150. {
  151. EnsureChildControls();
  152. }
  153. protected override void CreateChildControls()
  154. {
  155. Controls.Clear();
  156. m_TextBox.ID = "AmtTextBox";
  157. //m_TextBox.Width = this.Width;
  158. //m_TextBox.MaxLength = miLong;
  159. m_HidStats = new HiddenField();
  160. m_HidStats.ID = "AmtHidStats";
  161. if (this.ClientIDMode == System.Web.UI.ClientIDMode.Static)
  162. {
  163. m_TextBox.ID = this.ID;
  164. m_HidStats.ID = this.ID + "_AmtHidStats";
  165. }
  166. this.Controls.Add(m_TextBox);
  167. this.Controls.Add(m_HidStats);
  168. }
  169. protected override void Render(HtmlTextWriter writer)
  170. {
  171. EnsureChildControls();
  172. SetProptyToRender();//控件添加屬性
  173. if (m_TextBox.ID.IndexOf("txtDivExpTotIncome") > -1)
  174. {
  175. }
  176. m_TextBox.RenderControl(writer);
  177. m_HidStats.RenderControl(writer);
  178. }
  179. #endregion
  180. #region Public Function
  181. #endregion
  182. #region Private Function
  183. /// <summary>
  184. /// 在Render之前為子控件添加屬性
  185. /// </summary>
  186. private void SetProptyToRender()
  187. {
  188. switch (TextBoxType)
  189. {
  190. case AmtType.Digital://數字類型
  191. m_TextBox.Attributes.Add("onkeypress", "return KeyPress(this);");
  192. m_TextBox.Attributes.Add("onclick", "clearComma(this.id);");
  193. m_TextBox.Attributes.Add("onfocus", "moveEnd(this);");
  194. m_TextBox.Attributes.Add("onblur", "isMubmer(this.id);" + stronblur);
  195. m_TextBox.Attributes.Add("ismoney","true");
  196. m_TextBox.Style.Add(HtmlTextWriterStyle.Direction, "rtl");
  197. break;
  198. case AmtType.Letter://字母類型
  199. m_TextBox.Attributes.Add("onkeypress", "return inputLetter();");
  200. m_TextBox.Attributes.Add("onblur", "isLetter(this.id);");
  201. break;
  202. case AmtType.Email://Email類型
  203. m_TextBox.Attributes.Add("onblur", "return checkEmail(this.id);");
  204. break;
  205. }
  206. foreach (string strKey in this.Attributes.Keys)
  207. {
  208. m_TextBox.Attributes.Add(strKey, m_TextBox.Attributes[strKey] ?? "" + ";" + this.Attributes[strKey]);
  209. }
  210. m_TextBox.TabIndex = this.TabIndex;
  211. }
  212. #endregion
  213. }
  214. }