SheetHtmlForm.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:14k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Web;
- using System.Web.UI;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Design;
- using System.Collections;
- using System.Xml;
- using System.Web.UI.WebControls;
- namespace OThinker.H3.WorkSheet
- {
- [DefaultProperty("Text"), ToolboxData("<{0}:SheetHtmlForm runat=server></{0}:SheetHtmlForm>")]
- public class SheetHtmlForm : System.Web.UI.WebControls.WebControl
- {
- public const string Attr_DataField = "name";
- public SheetHtmlForm()
- {
- }
- private SheetPage SheetPage;
- public void Initialize(SheetPage SheetPage, string Html)
- {
- this.SheetPage = SheetPage;
- this.Parse(Html);
- }
- private string XmlContext;
- private int StartIndex;
- private int PreEndIndex;
- private ArrayList RenderList = new ArrayList();
- private void Parse(string Html)
- {
- // 转换成XML
- string convertedHtml = null;
- if (Html != null)
- {
- convertedHtml = Html.Replace("rn", "");
- convertedHtml = System.Web.HttpUtility.HtmlDecode(convertedHtml);
- }
- this.XmlContext = OThinker.Xml.HtmlConvertor.HtmlToXml("<DIV>" + convertedHtml + "</DIV>");
- this.RenderList = new ArrayList();
- this.StartIndex = 0;
- this.PreEndIndex = 0;
- // Xml
- XmlDocument document = new XmlDocument();
- document.LoadXml(this.XmlContext);
- foreach (XmlNode node in document.ChildNodes)
- {
- Iterate(node);
- }
- // 获得最后一段HTML
- string lastHtml = this.XmlContext.Substring(this.PreEndIndex);
- this.RenderList.Add(lastHtml);
- }
- private void Iterate(XmlNode Node)
- {
- if (!(Node is XmlElement))
- {
- return;
- }
- string dataField;
- XmlAttribute typeAttr;
- OThinker.H3.Data.WorkflowDataItem dataItem;
- bool accept = false;
- // 要替换的控件
- System.Web.UI.WebControls.WebControl control = null;
- string type;
- // <input type="file" name="aaa" />
- // <input type="checkbox" name="aaa" value="aabbbb" />
- // <textarea name="cccc"></textarea>
- // <input name="eee" type="text" />
- // <select name="aaa"><option value="dddd" selected="selected">dddd</option><option value="eee">eee</option></select>
- if (Node.Name == null)
- {
- }
- else if (Node.Name.ToLower() == "select")
- {
- if ((dataField = this.GetDataField(Node)) == null || dataField == "")
- {
- }
- else if (this.SheetPage.Enviroment.Workflow == null)
- {
- }
- else if (dataField == SheetInstancePrioritySelector.DataField)
- {
- accept = true;
- SheetInstancePrioritySelector selector = new SheetInstancePrioritySelector();
- control = selector;
- }
- else if ((dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
- {
- // 不满足替换的原则
- }
- else
- {
- accept = true;
- SheetDropDownList dropDownList = new SheetDropDownList();
- dropDownList.DataField = dataField;
- dropDownList.SourceDataField = Node.Attributes["sourcedatafield"] == null ? null : Node.Attributes["sourcedatafield"].InnerText;
- // 操作每一个Option
- foreach (XmlElement xmlOption in Node)
- {
- // value
- string itemValue = xmlOption.Attributes["value"].InnerText;
- // text
- string itemText = xmlOption.InnerText;
- ListItem item = new ListItem(itemText, itemValue);
- // 是否被选中
- if (xmlOption.Attributes["selected"] != null && xmlOption.Attributes["selected"].InnerText.ToLower() == "selected")
- {
- item.Selected = true;
- }
- dropDownList.Items.Add(item);
- }
- control = dropDownList;
- }
- }
- else if (Node.Name.ToLower() == "a")
- {
- if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
- this.SheetPage.Enviroment.Workflow == null ||
- (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
- {
- // 不满足替换的原则
- }
- else
- {
- accept = true;
- SheetHyperlink link = new SheetHyperlink();
- link.NavigateUrl = dataField;
- link.Text = Node.InnerText;
- control = link;
- }
- }
- else if (Node.Name.ToLower() == "textarea")
- {
- if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
- this.SheetPage.Enviroment.Workflow == null ||
- (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
- {
- // 不满足替换的原则
- }
- else
- {
- accept = true;
- if (dataItem.LogicType == OThinker.H3.Data.DataLogicType.Discussion)
- {
- SheetComment text = new SheetComment();
- text.DataField = dataField;
- control = text;
- }
- else
- {
- SheetTextBox text = new SheetTextBox();
- text.TextMode = TextBoxMode.MultiLine;
- text.DataField = dataField;
- control = text;
- }
- if (Node.Attributes["rows"] != null)
- {
- try
- {
- int rows = int.Parse(Node.Attributes["rows"].InnerText) * 15;
- control.Height = (Unit)rows;
- }
- catch
- {
- }
- }
- if (Node.Attributes["cols"] != null)
- {
- try
- {
- int cols = int.Parse(Node.Attributes["cols"].InnerText) * 8;
- control.Width = (Unit)cols;
- }
- catch
- {
- }
- }
- }
- }
- else if (Node.Name.ToLower() == "input")
- {
- if ((dataField = this.GetDataField(Node)) == null || dataField == "")
- {
- // 名称为空
- }
- else if ((typeAttr = Node.Attributes["type"]) == null)
- {
- // 只能是a, textarea, type类型,这种情况表示不需要替换控件
- }
- else if ((type = typeAttr.InnerText) == "text")
- {
- // 是TextBox
- accept = true;
- // 在这里要根据不同的数据类型将TextBox替换成不同的控件
- if (dataField == SheetInstanceNameEditor.DataField)
- {
- control = new SheetInstanceNameEditor();
- }
- else if (OThinker.H3.Data.Keywords.ParserFactory.IsKeyword(dataField))
- {
- // 是关键字,使用label
- SheetLabel label = new OThinker.H3.WorkSheet.SheetLabel();
- label.DataField = dataField;
- control = label;
- }
- else if (
- this.SheetPage.Enviroment.Workflow == null ||
- (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
- {
- // TextBox,但是没有对应的数据
- control = new OThinker.H3.WorkSheet.SheetTextBox();
- }
- else if (dataItem.RealType == typeof(System.DateTime))
- {
- // 替换成DropDownTime
- OThinker.H3.WorkSheet.SheetTime dropDownTime = new OThinker.H3.WorkSheet.SheetTime();
- dropDownTime.DefaultValue = SheetTime.DefaultValueType.CurrentTime;
- dropDownTime.Modal = DropDownTimeModal.SimplifiedTime;
- dropDownTime.DataField = dataItem.ItemName;
- control = dropDownTime;
- }
- else if (dataItem.LogicType == OThinker.H3.Data.DataLogicType.Sign)
- {
- OThinker.H3.WorkSheet.SheetSign sign = new SheetSign();
- sign.DataField = dataItem.ItemName;
- control = sign;
- }
- else
- {
- // TextBox,但是没有对应的数据
- OThinker.H3.WorkSheet.SheetTextBox textBox = new OThinker.H3.WorkSheet.SheetTextBox();
- textBox.DataField = dataItem.ItemName;
- control = textBox;
- }
- }
- else if (type == "checkbox")
- {
- if ((dataField = this.GetDataField(Node)) == null || dataField == "" ||
- this.SheetPage.Enviroment.Workflow == null ||
- (dataItem = this.SheetPage.Enviroment.Workflow.GetDataItem(dataField)) == null)
- {
- // 不满足替换的原则
- }
- else
- {
- accept = true;
- // 是CheckBox
- OThinker.H3.WorkSheet.SheetCheckBox checkBox = new OThinker.H3.WorkSheet.SheetCheckBox();
- checkBox.DataField = dataField;
- control = checkBox;
- }
- }
- else if (type == "file")
- {
- accept = true;
- SheetAttachment attachment = new SheetAttachment();
- attachment.DataField = dataField;
- control = attachment;
- }
- // 设置宽度
- if (Node.Attributes["size"] != null)
- {
- try
- {
- int width = int.Parse(Node.Attributes["size"].InnerText) * 6;
- control.Width = (Unit)width;
- }
- catch
- {
- }
- }
- }
- else if (Node.Name.ToLower() == "table")
- {
- foreach (XmlNode child in Node.ChildNodes)
- {
- if (child.Name.ToLower() == "caption")
- {
- accept = true;
- SheetGridView grid = new SheetGridView();
- grid.DataField = child.InnerText;
- control = grid;
- }
- }
- }
- // 已经接受表示该HTML块可以被替换成WorkSheet控件
- if (accept)
- {
- // 更新索引
- this.StartIndex = this.XmlContext.IndexOf(Node.OuterXml, this.StartIndex);
- string preXml = this.XmlContext.Substring(this.PreEndIndex, this.StartIndex - this.PreEndIndex);
- // 将前面的一段HTML添加到Render List中
- this.RenderList.Add(preXml);
- this.PreEndIndex = this.StartIndex + Node.OuterXml.Length;
- // 添加属性
- foreach (XmlAttribute attr in Node.Attributes)
- {
- if (attr.Name != Attr_DataField && attr.Name != "type")
- {
- control.Attributes.Add(attr.Name, attr.InnerText);
- }
- }
- // 添加到页面中
- this.Controls.Add(control);
- this.RenderList.Add(control);
- }
- else
- {
- foreach (XmlNode childNode in Node.ChildNodes)
- {
- Iterate(childNode);
- }
- }
- }
- private string GetDataField(XmlNode Node)
- {
- XmlAttribute dataFiledAttr;
- if ((dataFiledAttr = Node.Attributes[Attr_DataField]) == null)
- {
- return null;
- }
- else
- {
- return dataFiledAttr.InnerText;
- }
- }
- protected override void Render(HtmlTextWriter writer)
- {
- foreach (object renderObject in this.RenderList)
- {
- if (renderObject is string)
- {
- writer.Write((string)renderObject);
- }
- else
- {
- ((WebControl)renderObject).RenderControl(writer);
- }
- }
- }
- }
- }