RichText.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  25. protected IDictionary<string, string> GetPropertyList()
  26. {
  27. var dictPropertyList = new Dictionary<string, string>();
  28. dictPropertyList.Add("class", string.Format("easyui-richtext {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  29. //dictPropertyList.Add("type", "text/plain");
  30. if (!string.IsNullOrEmpty(this.ID))
  31. dictPropertyList.Add("id", this.ID);
  32. if (!string.IsNullOrEmpty(this.Name))
  33. dictPropertyList.Add("name", this.Name);
  34. if (!string.IsNullOrEmpty(this.Title))
  35. dictPropertyList.Add("title", this.Title);
  36. if (this.IsEnabled.HasValue && !this.IsEnabled.Value)
  37. dictPropertyList.Add("disabled", "true");
  38. if (this.IsShow.HasValue && !this.IsShow.Value)
  39. {
  40. if (dictPropertyList.Keys.Contains("style"))
  41. dictPropertyList["style"] += "display:none;";
  42. else
  43. dictPropertyList.Add("style", "display:none;");
  44. }
  45. if (this.Width.HasValue && this.Width > 0)
  46. {
  47. string widthString = string.Format("width:{0};", (Width > 1 ? (Width.ToString() + "px") : ((Width * 100).ToString() + "%")));
  48. if (dictPropertyList.Keys.Contains("style"))
  49. dictPropertyList["style"] += widthString;
  50. else
  51. dictPropertyList.Add("style", widthString);
  52. }
  53. if (this.Height.HasValue && this.Height > 0)
  54. {
  55. string heightString = string.Format("height:{0};", (Height > 1 ? (Height.ToString() + "px") : ((Height * 100).ToString() + "%")));
  56. if (dictPropertyList.Keys.Contains("style"))
  57. dictPropertyList["style"] += heightString;
  58. else
  59. dictPropertyList.Add("style", heightString);
  60. }
  61. return dictPropertyList;
  62. }
  63. public static BaseControl CreateControl(RichTextOptions richTextOptions, IDictionary<string, string> attributes = null)
  64. {
  65. RichText txtBox = new RichText();
  66. txtBox.CssClass = richTextOptions.CssClass;
  67. txtBox.ID = richTextOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6);
  68. txtBox.Name = richTextOptions.Name;
  69. txtBox.Title = richTextOptions.Title;
  70. txtBox.Value = richTextOptions.Value;
  71. txtBox.IsEnabled = richTextOptions.IsEnabled;
  72. txtBox.IsShow = richTextOptions.IsShow;
  73. txtBox.IsRequired = richTextOptions.IsRequired;
  74. txtBox.Validator = richTextOptions.Validator;
  75. txtBox.Width = richTextOptions.Width;
  76. txtBox.Height = richTextOptions.Height;
  77. if (attributes != null)
  78. {
  79. txtBox.Attributes = attributes;
  80. }
  81. return txtBox;
  82. }
  83. }
  84. }