PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wojilu/Web/Mvc/Html.cs

https://bitbucket.org/kingshine/wojilu
C# | 391 lines | 229 code | 68 blank | 94 comment | 25 complexity | 944b492ee607352d3be8d32f5171841e MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright 2010 www.wojilu.com
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections;
  18. using System.Text;
  19. using System.Web;
  20. using wojilu.Reflection;
  21. using System.Collections.Generic;
  22. using wojilu.Web.Context;
  23. namespace wojilu.Web.Mvc {
  24. /// <summary>
  25. /// 提供常用的 html 控件,比如单选列表、多选列表、下拉列表等
  26. /// </summary>
  27. public class Html {
  28. /// <summary>
  29. /// 验证码控件(包括一个input + 右侧的一个验证码 + 点击刷新机制)
  30. /// </summary>
  31. public static Captcha Captcha {
  32. get { return new wojilu.Web.Mvc.Captcha(); }
  33. }
  34. /// <summary>
  35. /// 多选框(用数组填充)
  36. /// </summary>
  37. /// <param name="items">填充列表的字符数组</param>
  38. /// <param name="chkName">控件名称</param>
  39. /// <param name="sValue">选定的值,多个选值之间用英文逗号分开,比如 "2, 6, 13"</param>
  40. /// <returns></returns>
  41. public static String CheckBoxList( String[] items, String chkName, Object sValue ) {
  42. String selectValue = cvt.ToNotNull( sValue );
  43. String[] arrSelectValue = getSelectValueArray( chkName, selectValue );
  44. StringBuilder builder = new StringBuilder();
  45. for (int i = 0; i < items.Length; i++) {
  46. Boolean isChk = IsValueChecked( arrSelectValue, items[i] );
  47. builder.AppendFormat( "<label><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2}/>{1}</label> ", chkName, items[i], isChk ? "checked" : "" );
  48. }
  49. return builder.ToString();
  50. }
  51. /// <summary>
  52. /// 多选框(用 Dictionary 填充)
  53. /// </summary>
  54. /// <param name="dic">填充列表的 Dictionary</param>
  55. /// <param name="chkName">控件名称</param>
  56. /// <param name="sValue">选定的值,多个选值之间用英文逗号分开,比如 "2, 6, 13"</param>
  57. /// <returns></returns>
  58. public static String CheckBoxList( Dictionary<String, string> dic, String chkName, Object sValue ) {
  59. String selectValue = cvt.ToNotNull( sValue );
  60. String[] arrSelectValue = getSelectValueArray( chkName, selectValue );
  61. StringBuilder builder = new StringBuilder();
  62. foreach (KeyValuePair<String, String> kv in dic) {
  63. Boolean isChk = IsValueChecked( arrSelectValue, kv.Value );
  64. String strchk = isChk ? "checked" : "";
  65. builder.AppendFormat( "<label><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2}/>{3}</label> ", chkName, kv.Value, strchk, kv.Key );
  66. }
  67. return builder.ToString();
  68. }
  69. /// <summary>
  70. /// 多选框(用对象列表填充)
  71. /// </summary>
  72. /// <param name="list">填充多选列表的对象列表</param>
  73. /// <param name="chkName">控件名称</param>
  74. /// <param name="textField">对象的属性名称(用于选框中文本部分)</param>
  75. /// <param name="valueField">对象的属性名称(用于选框中值)</param>
  76. /// <param name="sValue">选定的值,多个选值之间用英文逗号分开,比如 "2, 6, 13"</param>
  77. /// <returns></returns>
  78. public static String CheckBoxList( IList list, String chkName, String textField, String valueField, Object sValue ) {
  79. String selectValue = cvt.ToNotNull( sValue );
  80. String[] arrSelectValue = getSelectValueArray( chkName, selectValue );
  81. StringBuilder builder = new StringBuilder();
  82. for (int i = 0; i < list.Count; i++) {
  83. String txt = ReflectionUtil.GetPropertyValue( list[i], textField ).ToString();
  84. String val = ReflectionUtil.GetPropertyValue( list[i], valueField ).ToString();
  85. Boolean ischk = IsValueChecked( arrSelectValue, val );
  86. builder.AppendFormat( "<label><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {3}/>{2}</label> ", chkName, val, txt, ischk ? "checked" : "" );
  87. }
  88. return builder.ToString();
  89. }
  90. //-----------------------------------------------------------------------------------------------------------------
  91. /// <summary>
  92. /// 下拉控件(用数组填充)
  93. /// </summary>
  94. /// <param name="items">填充下拉框的字符数组</param>
  95. /// <param name="dropName">控件名称</param>
  96. /// <param name="val">选定的值</param>
  97. /// <returns></returns>
  98. public static String DropList( String[] items, String dropName, Object val ) {
  99. StringBuilder builder = new StringBuilder();
  100. builder.AppendFormat( "<select name=\"{0}\" id=\"{0}\">", dropName );
  101. String selVal = getSelectedValue( dropName, val );
  102. for (int i = 0; i < items.Length; i++) {
  103. String strchk = String.Empty;
  104. if (string.Compare( items[i], selVal, true ) == 0) {
  105. strchk = "selected";
  106. }
  107. builder.AppendFormat( "<option value=\"{0}\" {1}>{0}</option>", items[i], strchk );
  108. }
  109. builder.Append( "</select>" );
  110. return builder.ToString();
  111. }
  112. /// <summary>
  113. /// 下拉控件(用 Dictionary 填充)
  114. /// </summary>
  115. /// <param name="dic">填充下拉框的Dictionary</param>
  116. /// <param name="dropName">控件名称</param>
  117. /// <param name="val">选定的值</param>
  118. /// <returns></returns>
  119. public static String DropList( Dictionary<String, string> dic, String dropName, Object val ) {
  120. StringBuilder builder = new StringBuilder();
  121. builder.AppendFormat( "<select name=\"{0}\" id=\"{0}\">", dropName );
  122. String selval = getSelectedValue( dropName, val );
  123. foreach (KeyValuePair<String, string> kv in dic) {
  124. String strchk = kv.Value.Equals( selval ) ? "selected" : String.Empty;
  125. builder.AppendFormat( "<option value=\"{0}\" {1}>{2}</option>", kv.Value, strchk, kv.Key );
  126. }
  127. builder.Append( "</select>" );
  128. return builder.ToString();
  129. }
  130. /// <summary>
  131. /// 下拉控件(用对象列表填充)
  132. /// </summary>
  133. /// <param name="list">填充下拉框的对象列表</param>
  134. /// <param name="dropName">控件名称</param>
  135. /// <param name="textField">对象的属性名称(用于选框中文本部分)</param>
  136. /// <param name="valueField">对象的属性名称(用于选框中值)</param>
  137. /// <param name="val">选定的值</param>
  138. /// <returns></returns>
  139. public static String DropList( IList list, String dropName, String textField, String valueField, Object val ) {
  140. StringBuilder builder = new StringBuilder();
  141. builder.AppendFormat( "<select name=\"{0}\" id=\"{0}\">", dropName );
  142. String selval = getSelectedValue( dropName, val );
  143. for (int i = 0; i < list.Count; i++) {
  144. String txt = ReflectionUtil.GetPropertyValue( list[i], textField ).ToString();
  145. String fval = ReflectionUtil.GetPropertyValue( list[i], valueField ).ToString();
  146. String strchk = String.Empty;
  147. if (string.Compare( fval, selval, true ) == 0) {
  148. strchk = "selected";
  149. }
  150. builder.AppendFormat( "<option value=\"{0}\" {1}>{2}</option>", fval, strchk, txt );
  151. }
  152. builder.Append( "</select>" );
  153. return builder.ToString();
  154. }
  155. //-----------------------------------------------------------------------------------------------------------------
  156. /// <summary>
  157. /// 多个单选的列表(用字符数组填充)
  158. /// </summary>
  159. /// <param name="items">填充列表的字符数组</param>
  160. /// <param name="radioName">控件名称</param>
  161. /// <param name="val">选定的值</param>
  162. /// <returns></returns>
  163. public static String RadioList( String[] items, String radioName, Object val ) {
  164. StringBuilder builder = new StringBuilder();
  165. String str = getSelectedValue( radioName, val );
  166. for (int i = 0; i < items.Length; i++) {
  167. String strchk = String.Empty;
  168. if (string.Compare( items[i], str.ToString(), true ) == 0) {
  169. strchk = "checked=\"checked\"";
  170. }
  171. builder.AppendFormat( "<label><input type=\"radio\" id=\"{0}{3}\" name=\"{0}\" value=\"{1}\" {2}/>{1}</label> ", radioName, items[i], strchk, i );
  172. }
  173. return builder.ToString();
  174. }
  175. /// <summary>
  176. /// 多个单选的列表(用 Dictionary 填充)
  177. /// </summary>
  178. /// <param name="dic">填充列表的 Dictionary</param>
  179. /// <param name="radioName">控件名称</param>
  180. /// <param name="val">选定的值</param>
  181. /// <returns></returns>
  182. public static String RadioList( Dictionary<String, String> dic, String radioName, Object val ) {
  183. StringBuilder builder = new StringBuilder();
  184. String selval = getSelectedValue( radioName, val );
  185. int i = 0;
  186. foreach (KeyValuePair<String, String> kv in dic) {
  187. String strchk = kv.Value.Equals( selval ) ? "checked=\"checked\"" : String.Empty;
  188. builder.AppendFormat( "<label><input type=\"radio\" id=\"{0}{4}\" name=\"{0}\" value=\"{1}\" {2}/>{3}</label> ", radioName, kv.Value, strchk, kv.Key, i );
  189. i++;
  190. }
  191. return builder.ToString();
  192. }
  193. /// <summary>
  194. /// 多个单选的列表(用对象列表填充)
  195. /// </summary>
  196. /// <param name="list">填充单选列表的对象列表</param>
  197. /// <param name="radioName">控件名称</param>
  198. /// <param name="textField">对象的属性名称(用于选框中文本部分)</param>
  199. /// <param name="valueField">对象的属性名称(用于选框中值)</param>
  200. /// <param name="val">选定的值</param>
  201. /// <returns></returns>
  202. public static String RadioList( IList list, String radioName, String textField, String valueField, Object val ) {
  203. StringBuilder builder = new StringBuilder();
  204. String str = getSelectedValue( radioName, val );
  205. for (int i = 0; i < list.Count; i++) {
  206. String txt = ReflectionUtil.GetPropertyValue( list[i], textField ).ToString();
  207. String fval = ReflectionUtil.GetPropertyValue( list[i], valueField ).ToString();
  208. String strchk = String.Empty;
  209. if (string.Compare( fval, str.ToString(), true ) == 0) {
  210. strchk = "checked=\"checked\"";
  211. }
  212. builder.AppendFormat( "<label><input type=\"radio\" id=\"{0}{4}\" name=\"{0}\" value=\"{1}\" {3}/>{2}</label> ", radioName, fval, txt, strchk, i );
  213. }
  214. return builder.ToString();
  215. }
  216. //-----------------------------------------------------------------------------------------------------------------
  217. private StringBuilder sb = new StringBuilder();
  218. public void FormBegin( String actionUrl ) {
  219. this.sb.Append( GetFormBegin( actionUrl ) );
  220. }
  221. public void FormEnd() {
  222. this.sb.Append( GetFormEnd() );
  223. }
  224. public void Button( String id, String text ) {
  225. this.sb.Append( GetButton( id, text ) );
  226. }
  227. public void Code( String htmlCode ) {
  228. this.sb.Append( htmlCode );
  229. }
  230. public void CheckBox( String name, String val, String text ) {
  231. this.sb.Append( GetCheckBox( name, val, text ) );
  232. }
  233. public void HiddenInput( String name, String val ) {
  234. this.sb.Append( InputHidden( name, val ) );
  235. }
  236. public void Radio( String name, String val, String text ) {
  237. this.sb.Append( GetRadio( name, val, text ) );
  238. }
  239. public void Submit( String text ) {
  240. this.sb.Append( GetSubmit( text ) );
  241. }
  242. public override String ToString() {
  243. return this.sb.ToString();
  244. }
  245. //-----------------------------------------------------------------------------------------------------------------
  246. public static String CheckBox( String name, String text, String val, Boolean isChecked ) {
  247. String str = "";
  248. if (isChecked) {
  249. str = "checked";
  250. }
  251. return String.Format( "<label><input type=\"checkbox\" name=\"{0}\" value=\"{1}\" {2} /> {3}</label>", name, val, str, text );
  252. }
  253. private static String GetButton( String id, String text ) {
  254. return String.Format( "<input type=\"button\" id=\"{0}\" value=\"{1}\" />", id, text );
  255. }
  256. private static String GetCheckBox( String name, String val, String text ) {
  257. return String.Format( "<label><input type=\"checkbox\" name=\"{0}\" id=\"{3}\" value=\"{1}\" /> {2}</label>", name, val, text, name + val );
  258. }
  259. private static String GetFormBegin( String actionUrl ) {
  260. return String.Format( "<form method=\"post\" action=\"{0}\">", actionUrl );
  261. }
  262. private static String GetFormEnd() {
  263. return "</form>";
  264. }
  265. private static String GetRadio( String name, String val, String text ) {
  266. return String.Format( "<label><input type=\"radio\" name=\"{0}\" id=\"{3}\" value=\"{1}\" /> {2}</label> ", name, val, text, name + val );
  267. }
  268. private static String getSelectedValue( String dropName, Object val ) {
  269. String str = cvt.ToNotNull( val );
  270. if (strUtil.IsNullOrEmpty( str ) && CurrentRequest.getHttpMethod().Equals( "POST" )) {
  271. str = CurrentRequest.getForm( dropName );
  272. }
  273. str = cvt.ToNotNull( str );
  274. return str;
  275. }
  276. private static String[] getSelectValueArray( String chkName, String selectValue ) {
  277. if (CurrentRequest.getHttpMethod().Equals( "POST" )) {
  278. selectValue = CurrentRequest.getForm( chkName );
  279. }
  280. if (strUtil.HasText( selectValue )) {
  281. return selectValue.Split( new char[] { ',' } );
  282. }
  283. return null;
  284. }
  285. private static String GetSubmit( String text ) {
  286. return String.Format( "<input type=\"submit\" value=\"{0}\" />", text );
  287. }
  288. public static String InputHidden( String name, String value ) {
  289. return String.Format( "<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", name, value );
  290. }
  291. private static Boolean IsValueChecked( String[] arrSelectValue, String val ) {
  292. if (arrSelectValue == null) return false;
  293. foreach (String str in arrSelectValue) {
  294. if (strUtil.EqualsIgnoreCase( val.Trim(), str.Trim() )) return true;
  295. }
  296. return false;
  297. }
  298. public static String TextArea( String name, String value, String style ) {
  299. return String.Format( "<textarea name=\"{0}\" style=\"{2}\">{1}</textarea>", name, value, style );
  300. }
  301. public static String TextInput( String name, String value ) {
  302. return String.Format( "<input type=\"text\" name=\"{0}\" value=\"{1}\" />", name, value );
  303. }
  304. public static String TextInput( String name, String value, String style ) {
  305. return String.Format( "<input type=\"text\" name=\"{0}\" value=\"{1}\" style=\"{2}\" />", name, value, style );
  306. }
  307. public static String Tree( IList list, GetNodeString getItemString ) {
  308. return getChildren( list, 0, 0, getItemString );
  309. }
  310. private static String getChildren( IList list, int parentId, int depth, GetNodeString getItemString ) {
  311. String result = "";
  312. foreach (INode node in list) {
  313. if (node.ParentId != parentId) continue;
  314. String thisItemString = getItemString( node, depth );
  315. String childrenString = getChildren( list, node.Id, (depth + 1), getItemString );
  316. result += thisItemString + Environment.NewLine;
  317. if (strUtil.HasText( childrenString )) result += childrenString + Environment.NewLine;
  318. }
  319. return result;
  320. }
  321. }
  322. public delegate String GetNodeString( INode node, int depth );
  323. }