HtmlControlConverter.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:4k
源码类别:

Ajax

开发平台:

C#

  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.HtmlControls;
  5. using System.IO;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace AjaxPro
  9. {
  10. /// <summary>
  11. /// Provides methods to serialize and deserialize an object that is inherited from HtmlControl.
  12. /// </summary>
  13. public class HtmlControlConverter : IJavaScriptConverter
  14. {
  15. public HtmlControlConverter() : base()
  16. {
  17. }
  18. public override object Deserialize(IJavaScriptObject o, Type t)
  19. {
  20. if(!typeof(HtmlControl).IsAssignableFrom(t) || !(o is JavaScriptString))
  21. throw new NotSupportedException();
  22. return HtmlControlFromString(o.ToString(), t);
  23. }
  24. public override string Serialize(object o)
  25. {
  26. if(!(o is Control))
  27. throw new NotSupportedException();
  28. return HtmlControlToString((HtmlControl)o);
  29. }
  30. public override Type[] SerializableTypes
  31. {
  32. get
  33. {
  34. return new Type[]
  35. {
  36. typeof(HtmlControl),
  37. typeof(HtmlAnchor),
  38. typeof(HtmlButton),
  39. typeof(HtmlImage),
  40. typeof(HtmlInputButton),
  41. typeof(HtmlInputCheckBox),
  42. typeof(HtmlInputRadioButton),
  43. typeof(HtmlInputText),
  44. typeof(HtmlSelect),
  45. typeof(HtmlTableCell),
  46. typeof(HtmlTable),
  47. typeof(HtmlTableRow),
  48. typeof(HtmlTextArea)
  49. };
  50. }
  51. }
  52. public override Type[] DeserializableTypes
  53. {
  54. get
  55. {
  56. return this.SerializableTypes;
  57. }
  58. }
  59. #region Internal Methods
  60. internal static string CorrectAttributes(string input)
  61. {
  62. string s = @"selected=""selected""";
  63. Regex r = new Regex(s, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  64. input =  r.Replace(input, @"selected=""true""");
  65. s = @"multiple=""multiple""";
  66. r = new Regex(s, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  67. input =  r.Replace(input, @"multiple=""true""");
  68. s = @"disabled=""disabled""";
  69. r = new Regex(s, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  70. input =  r.Replace(input, @"disabled=""true""");
  71. return input;
  72. }
  73. internal static string HtmlControlToString(HtmlControl control)
  74. {
  75. StringWriter writer = new StringWriter(new StringBuilder());
  76. control.RenderControl(new HtmlTextWriter(writer));
  77. return JavaScriptSerializer.SerializeString(writer.ToString());    
  78. }
  79. internal static HtmlControl HtmlControlFromString(string html, Type type)
  80. {
  81. if(!typeof(HtmlControl).IsAssignableFrom(type))
  82. throw new InvalidCastException("The target type is not a HtmlControlType");
  83. html = AddRunAtServer(html, (Activator.CreateInstance(type) as HtmlControl).TagName);
  84. if(type.IsAssignableFrom(typeof(HtmlSelect)))
  85. html = CorrectAttributes(html);
  86. Control o = HtmlControlConverterHelper.Parse(html);;
  87. if(o.GetType() == type)
  88. return (o as HtmlControl);
  89. else
  90. {
  91. foreach(Control con in o.Controls)
  92. {
  93. if(con.GetType() == type)
  94. {
  95. return (con as HtmlControl);
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. internal static string AddRunAtServer(string input, string tagName)
  102. {
  103. // <select[^>]*?(?<InsertPos>s*)>
  104. string pattern = "<" + Regex.Escape(tagName) + @"[^>]*?(?<InsertPos>s*)/?>";
  105. Regex regEx = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  106. Match match = regEx.Match(input);
  107. if (match.Success)
  108. {
  109. Group group = match.Groups["InsertPos"];
  110. return input.Insert(group.Index + group.Length, " runat="server"");
  111. }
  112. else
  113. return input;
  114. }
  115. #endregion
  116. }
  117. internal class HtmlControlConverterHelper : TemplateControl
  118. {
  119. internal static Control Parse(string controlString)
  120. {
  121. HtmlControlConverterHelper control = new HtmlControlConverterHelper();
  122. #if(NET20)
  123. control.AppRelativeVirtualPath = "~";
  124. #endif
  125. return control.ParseControl(controlString);
  126. }
  127. }
  128. }