SheetHtmlForm.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:14k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Design;
  7. using System.Collections;
  8. using System.Xml;
  9. using System.Web.UI.WebControls;
  10. namespace OThinker.H3.WorkSheet
  11. {
  12.     [DefaultProperty("Text"), ToolboxData("<{0}:SheetHtmlForm runat=server></{0}:SheetHtmlForm>")]
  13.     public class SheetHtmlForm : System.Web.UI.WebControls.WebControl
  14.     {
  15.         public const string Attr_DataField = "name";
  16.         public SheetHtmlForm()
  17.         {
  18.         }
  19.         private SheetPage SheetPage;
  20.         public void Initialize(SheetPage SheetPage, string Html)
  21.         {
  22.             this.SheetPage = SheetPage;
  23.             this.Parse(Html);
  24.         }
  25.         private string XmlContext;
  26.         private int StartIndex;
  27.         private int PreEndIndex;
  28.         private ArrayList RenderList = new ArrayList();
  29.         private void Parse(string Html)
  30.         {
  31.             // 转换成XML
  32.             string convertedHtml = null;
  33.             if (Html != null)
  34.             {
  35.                 convertedHtml = Html.Replace("rn", "");
  36.                 convertedHtml = System.Web.HttpUtility.HtmlDecode(convertedHtml);
  37.             }
  38.             this.XmlContext = OThinker.Xml.HtmlConvertor.HtmlToXml("<DIV>" + convertedHtml + "</DIV>");
  39.             this.RenderList = new ArrayList();
  40.             this.StartIndex = 0;
  41.             this.PreEndIndex = 0;
  42.             // Xml
  43.             XmlDocument document = new XmlDocument();
  44.             document.LoadXml(this.XmlContext);
  45.             foreach (XmlNode node in document.ChildNodes)
  46.             {
  47.                 Iterate(node);
  48.             }
  49.             // 获得最后一段HTML
  50.             string lastHtml = this.XmlContext.Substring(this.PreEndIndex);
  51.             this.RenderList.Add(lastHtml);
  52.         }
  53.         private void Iterate(XmlNode Node)
  54.         {
  55.             if (!(Node is XmlElement))
  56.             {
  57.                 return;
  58.             }
  59.             string dataField;
  60.             XmlAttribute typeAttr;
  61.             OThinker.H3.Data.WorkflowDataItem dataItem;
  62.             bool accept = false;
  63.             // 要替换的控件
  64.             System.Web.UI.WebControls.WebControl control = null;
  65.             string type;
  66.             // <input type="file" name="aaa" />
  67.             // <input type="checkbox" name="aaa" value="aabbbb" />
  68.             // <textarea name="cccc"></textarea>
  69.             // <input name="eee" type="text" />
  70.             // <select name="aaa"><option value="dddd" selected="selected">dddd</option><option value="eee">eee</option></select>
  71.             if (Node.Name == null)
  72.             {
  73.             }
  74.             else if (Node.Name.ToLower() == "select")
  75.             {
  76.                 if ((dataField = this.GetDataField(Node)) == null || dataField == "")
  77.                 {
  78.                 }
  79.                 else if (this.SheetPage.Enviroment.Workflow == null)
  80.                 {
  81.                 }
  82.                 else if (dataField == SheetInstancePrioritySelector.DataField)
  83.                 {
  84.                     accept = true;
  85.                     SheetInstancePrioritySelector selector = new SheetInstancePrioritySelector();
  86.                     control = selector;
  87.                 }
  88.                 else if ((dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
  89.                 {
  90.                     // 不满足替换的原则
  91.                 }
  92.                 else
  93.                 {
  94.                     accept = true;
  95.                     SheetDropDownList dropDownList = new SheetDropDownList();
  96.                     dropDownList.DataField = dataField;
  97.                     dropDownList.SourceDataField = Node.Attributes["sourcedatafield"] == null ? null : Node.Attributes["sourcedatafield"].InnerText;
  98.                     // 操作每一个Option
  99.                     foreach (XmlElement xmlOption in Node)
  100.                     {
  101.                         // value
  102.                         string itemValue = xmlOption.Attributes["value"].InnerText;
  103.                         // text
  104.                         string itemText = xmlOption.InnerText;
  105.                         ListItem item = new ListItem(itemText, itemValue);
  106.                         // 是否被选中
  107.                         if (xmlOption.Attributes["selected"] != null && xmlOption.Attributes["selected"].InnerText.ToLower() == "selected")
  108.                         {
  109.                             item.Selected = true;
  110.                         }
  111.                         dropDownList.Items.Add(item);
  112.                     }
  113.                     control = dropDownList;
  114.                 }
  115.             }
  116.             else if (Node.Name.ToLower() == "a")
  117.             {
  118.                 if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
  119.                     this.SheetPage.Enviroment.Workflow == null ||
  120.                     (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
  121.                 {
  122.                     // 不满足替换的原则
  123.                 }
  124.                 else
  125.                 {
  126.                     accept = true;
  127.                     SheetHyperlink link = new SheetHyperlink();
  128.                     link.NavigateUrl = dataField;
  129.                     link.Text = Node.InnerText;
  130.                     control = link;
  131.                 }
  132.             }
  133.             else if (Node.Name.ToLower() == "textarea")
  134.             {
  135.                 if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
  136.                     this.SheetPage.Enviroment.Workflow == null ||
  137.                     (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
  138.                 {
  139.                     // 不满足替换的原则
  140.                 }
  141.                 else
  142.                 {
  143.                     accept = true;
  144.                     if (dataItem.LogicType == OThinker.H3.Data.DataLogicType.Discussion)
  145.                     {
  146.                         SheetComment text = new SheetComment();
  147.                         text.DataField = dataField;
  148.                         control = text;
  149.                     }
  150.                     else
  151.                     {
  152.                         SheetTextBox text = new SheetTextBox();
  153.                         text.TextMode = TextBoxMode.MultiLine;
  154.                         text.DataField = dataField;
  155.                         control = text;
  156.                     }
  157.                     if (Node.Attributes["rows"] != null)
  158.                     {
  159.                         try
  160.                         {
  161.                             int rows = int.Parse(Node.Attributes["rows"].InnerText) * 15;
  162.                             control.Height = (Unit)rows;
  163.                         }
  164.                         catch
  165.                         {
  166.                         }
  167.                     }
  168.                     if (Node.Attributes["cols"] != null)
  169.                     {
  170.                         try
  171.                         {
  172.                             int cols = int.Parse(Node.Attributes["cols"].InnerText) * 8;
  173.                             control.Width = (Unit)cols;
  174.                         }
  175.                         catch
  176.                         {
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.             else if (Node.Name.ToLower() == "input")
  182.             {
  183.                 if ((dataField = this.GetDataField(Node)) == null || dataField == "")
  184.                 {
  185.                     // 名称为空
  186.                 }
  187.                 else if ((typeAttr = Node.Attributes["type"]) == null)
  188.                 {
  189.                     // 只能是a, textarea, type类型,这种情况表示不需要替换控件
  190.                 }
  191.                 else if ((type = typeAttr.InnerText) == "text")
  192.                 {
  193.                     // 是TextBox
  194.                     accept = true;
  195.                     // 在这里要根据不同的数据类型将TextBox替换成不同的控件
  196.                     if (dataField == SheetInstanceNameEditor.DataField)
  197.                     {
  198.                         control = new SheetInstanceNameEditor();
  199.                     }
  200.                     else if (OThinker.H3.Data.Keywords.ParserFactory.IsKeyword(dataField))
  201.                     {
  202.                         // 是关键字,使用label
  203.                         SheetLabel label = new OThinker.H3.WorkSheet.SheetLabel();
  204.                         label.DataField = dataField;
  205.                         control = label;
  206.                     }
  207.                     else if (
  208.                         this.SheetPage.Enviroment.Workflow == null ||
  209.                         (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
  210.                     {
  211.                         // TextBox,但是没有对应的数据
  212.                         control = new OThinker.H3.WorkSheet.SheetTextBox();
  213.                     }
  214.                     else if (dataItem.RealType == typeof(System.DateTime))
  215.                     {
  216.                         // 替换成DropDownTime
  217.                         OThinker.H3.WorkSheet.SheetTime dropDownTime = new OThinker.H3.WorkSheet.SheetTime();
  218.                         dropDownTime.DefaultValue = SheetTime.DefaultValueType.CurrentTime;
  219.                         dropDownTime.Modal = DropDownTimeModal.SimplifiedTime;
  220.                         dropDownTime.DataField = dataItem.ItemName;
  221.                         control = dropDownTime;
  222.                     }
  223.                     else if (dataItem.LogicType == OThinker.H3.Data.DataLogicType.Sign)
  224.                     {
  225.                         OThinker.H3.WorkSheet.SheetSign sign = new SheetSign();
  226.                         sign.DataField = dataItem.ItemName;
  227.                         control = sign;
  228.                     }
  229.                     else
  230.                     {
  231.                         // TextBox,但是没有对应的数据
  232.                         OThinker.H3.WorkSheet.SheetTextBox textBox = new OThinker.H3.WorkSheet.SheetTextBox();
  233.                         textBox.DataField = dataItem.ItemName;
  234.                         control = textBox;
  235.                     }
  236.                 }
  237.                 else if (type == "checkbox")
  238.                 {
  239.                     if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
  240.                         this.SheetPage.Enviroment.Workflow == null ||
  241.                         (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
  242.                     {
  243.                         // 不满足替换的原则
  244.                     }
  245.                     else
  246.                     {
  247.                         accept = true;
  248.                         // 是CheckBox
  249.                         OThinker.H3.WorkSheet.SheetCheckBox checkBox = new OThinker.H3.WorkSheet.SheetCheckBox();
  250.                         checkBox.DataField = dataField;
  251.                         control = checkBox;
  252.                     }
  253.                 }
  254.                 else if (type == "file")
  255.                 {
  256.                     accept = true;
  257.                     SheetAttachment attachment = new SheetAttachment();
  258.                     attachment.DataField = dataField;
  259.                     control = attachment;
  260.                 }
  261.                 // 设置宽度
  262.                 if (Node.Attributes["size"] != null)
  263.                 {
  264.                     try
  265.                     {
  266.                         int width = int.Parse(Node.Attributes["size"].InnerText) * 6;
  267.                         control.Width = (Unit)width;
  268.                     }
  269.                     catch
  270.                     {
  271.                     }
  272.                 }
  273.             }
  274.             else if (Node.Name.ToLower() == "table")
  275.             {
  276.                 foreach (XmlNode child in Node.ChildNodes)
  277.                 {
  278.                     if (child.Name.ToLower() == "caption")
  279.                     {
  280.                         accept = true;
  281.                         SheetGridView grid = new SheetGridView();
  282.                         grid.DataField = child.InnerText;
  283.                         control = grid;
  284.                     }
  285.                 }
  286.             }
  287.             // 已经接受表示该HTML块可以被替换成WorkSheet控件
  288.             if (accept)
  289.             {
  290.                 // 更新索引
  291.                 this.StartIndex = this.XmlContext.IndexOf(Node.OuterXml, this.StartIndex);
  292.                 string preXml = this.XmlContext.Substring(this.PreEndIndex, this.StartIndex - this.PreEndIndex);
  293.                 // 将前面的一段HTML添加到Render List中
  294.                 this.RenderList.Add(preXml);
  295.                 this.PreEndIndex = this.StartIndex + Node.OuterXml.Length;
  296.                 // 添加属性
  297.                 foreach (XmlAttribute attr in Node.Attributes)
  298.                 {
  299.                     if (attr.Name != Attr_DataField && attr.Name != "type")
  300.                     {
  301.                         control.Attributes.Add(attr.Name, attr.InnerText);
  302.                     }
  303.                 }
  304.                 // 添加到页面中
  305.                 this.Controls.Add(control);
  306.                 this.RenderList.Add(control);
  307.             }
  308.             else
  309.             {
  310.                 foreach (XmlNode childNode in Node.ChildNodes)
  311.                 {
  312.                     Iterate(childNode);
  313.                 }
  314.             }
  315.         }
  316.         private string GetDataField(XmlNode Node)
  317.         {
  318.             XmlAttribute dataFiledAttr;
  319.             if ((dataFiledAttr = Node.Attributes[Attr_DataField]) == null)
  320.             {
  321.                 return null;
  322.             }
  323.             else
  324.             {
  325.                 return dataFiledAttr.InnerText;
  326.             }
  327.         }
  328.         protected override void Render(HtmlTextWriter writer)
  329.         {
  330.             foreach (object renderObject in this.RenderList)
  331.             {
  332.                 if (renderObject is string)
  333.                 {
  334.                     writer.Write((string)renderObject);
  335.                 }
  336.                 else
  337.                 {
  338.                     ((WebControl)renderObject).RenderControl(writer);
  339.                 }
  340.             }
  341.         }
  342.     }
  343. }