RichText.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Web;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public class RichText : BaseFormControl
  10. {
  11. public int? Width { get; set; }
  12. public int? Height { get; set; }
  13. public string Value { get; set; }
  14. public override string Render()
  15. {
  16. //TagBuilder tabBuilder = new TagBuilder("script");
  17. //tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  18. //if (this.Attributes != null)
  19. //{
  20. // tabBuilder.MergeAttributes<string, string>(this.Attributes);
  21. //}
  22. //tabBuilder.InnerHtml = HttpContext.Current.Server.HtmlDecode(this.Value);
  23. //return tabBuilder.ToString(TagRenderMode.Normal);
  24. TagBuilder tabBuilder = new TagBuilder("textarea");
  25. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  26. if (this.Attributes != null)
  27. {
  28. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  29. }
  30. tabBuilder.InnerHtml = HttpContext.Current.Server.HtmlDecode(this.Value);
  31. return tabBuilder.ToString(TagRenderMode.Normal);
  32. }
  33. protected IDictionary<string, string> GetPropertyList()
  34. {
  35. var dictPropertyList = new Dictionary<string, string>();
  36. dictPropertyList.Add("class", string.Format("easyui-richtext {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  37. //dictPropertyList.Add("type", "text/plain");
  38. if (!string.IsNullOrEmpty(this.ID))
  39. dictPropertyList.Add("id", this.ID);
  40. if (!string.IsNullOrEmpty(this.Name))
  41. dictPropertyList.Add("name", this.Name);
  42. if (!string.IsNullOrEmpty(this.Title))
  43. dictPropertyList.Add("title", this.Title);
  44. if (this.IsEnabled.HasValue && !this.IsEnabled.Value)
  45. dictPropertyList.Add("disabled", "true");
  46. if (this.IsShow.HasValue && !this.IsShow.Value)
  47. {
  48. if (dictPropertyList.Keys.Contains("style"))
  49. dictPropertyList["style"] += "display:none;";
  50. else
  51. dictPropertyList.Add("style", "display:none;");
  52. }
  53. if (this.Width.HasValue && this.Width > 0)
  54. {
  55. string widthString = string.Format("width:{0};", (Width > 1 ? (Width.ToString() + "px") : ((Width * 100).ToString() + "%")));
  56. if (dictPropertyList.Keys.Contains("style"))
  57. dictPropertyList["style"] += widthString;
  58. else
  59. dictPropertyList.Add("style", widthString);
  60. }
  61. if (this.Height.HasValue && this.Height > 0)
  62. {
  63. string heightString = string.Format("height:{0};", (Height > 1 ? (Height.ToString() + "px") : ((Height * 100).ToString() + "%")));
  64. if (dictPropertyList.Keys.Contains("style"))
  65. dictPropertyList["style"] += heightString;
  66. else
  67. dictPropertyList.Add("style", heightString);
  68. }
  69. return dictPropertyList;
  70. }
  71. public static BaseControl CreateControl(RichTextOptions richTextOptions, IDictionary<string, string> attributes = null)
  72. {
  73. RichText txtBox = new RichText();
  74. txtBox.CssClass = richTextOptions.CssClass;
  75. txtBox.ID = richTextOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6);
  76. txtBox.Name = richTextOptions.Name;
  77. txtBox.Title = richTextOptions.Title;
  78. txtBox.Value = richTextOptions.Value;
  79. txtBox.IsEnabled = richTextOptions.IsEnabled;
  80. txtBox.IsShow = richTextOptions.IsShow;
  81. txtBox.IsRequired = richTextOptions.IsRequired;
  82. txtBox.Validator = richTextOptions.Validator;
  83. txtBox.Width = richTextOptions.Width;
  84. txtBox.Height = richTextOptions.Height;
  85. if (attributes != null)
  86. {
  87. txtBox.Attributes = attributes;
  88. }
  89. return txtBox;
  90. }
  91. }
  92. }