首页 > 代码库 > SheetForm.cs

SheetForm.cs

using System.Collections;
using System.Text;

namespace CMSC.Class.CsBoxs
{
    public class SheetForm
    {
        //定义变量
        private int toprows = 0;
        private int bottomrows = 0;
        private StringBuilder formhtml = new StringBuilder();
        private StringBuilder bottomhtml = new StringBuilder();
        private StringBuilder btnhtml = new StringBuilder();

        #region 定义属性

        private string _id = "";
        private string _name = "";
        private string _method = "";
        private string _action = "";
        private string _target = "";
        private int _subflag = 0;
        private ArrayList _formrows = new ArrayList();
        private ArrayList _formcontrols = new ArrayList();
        private ArrayList _editmenusbar = new ArrayList();
        private StringBuilder _formtables = new StringBuilder();
        private bool _formcheck = false;
        private bool _isformtables = true;

        public string Id { set { _id = value; } get { return _id; } }
        public string Name { set { _name = value; } get { return _name; } }
        public string Method { set { _method = value; } get { return _method; } }
        public string Action { set { _action = value; } get { return _action; } }
        public string Target { set { _target = value; } get { return _target; } }
        public int SubFlag { set { _subflag = value; } get { return _subflag; } }
        public ArrayList FormRows { set { _formrows = value; } get { return _formrows; } }
        public ArrayList FormControls { set { _formcontrols = value; } get { return _formcontrols; } }
        public ArrayList EditMenusBar { set { _editmenusbar = value; } get { return _editmenusbar; } }
        public StringBuilder FormTables { set { _formtables = value; } get { return _formtables; } }
        public bool FormCheck { set { _formcheck = value; } get { return _formcheck; } }
        public bool IsFormTables { set { _isformtables = value; } get { return _isformtables; } }

        #endregion

        #region 构造函数

        public SheetForm(string id)
        {
            this.Id = id;
        }

        public SheetForm(string name, int subflag)
        {
            this.Id = name;
            SetAttribute(subflag, name, "post", true);
        }

        public SheetForm(int subflag, string name, string method)
        {
            this.Id = name;
            SetAttribute(subflag, name, method, true);
        }

        public SheetForm(int subflag, string name, bool formcheck)
        {
            this.Id = name;
            SetAttribute(subflag, name, "post", formcheck);
        }

        public SheetForm(int subflag, string name, string method, bool formcheck)
        {
            this.Id = name;
            SetAttribute(subflag, name, method, formcheck);
        }

        public void SetAttribute(int subflag, string name, string method, bool formcheck)
        {
            SubFlag = subflag;
            Name = name;
            Method = method;
            FormCheck = formcheck;
        }

        #endregion

        #region 重写方法

        private StringBuilder SetTopRowHtml(string valign)
        {
            StringBuilder html = new StringBuilder();
            StringBuilder rowshtml = new StringBuilder();
            foreach (object[] rowobj in FormRows)
            {
                string rowvalign = rowobj[1].ToString().Trim();
                if (rowvalign == valign)
                {
                    int i = 0;
                    int row = (int)rowobj[0];
                    StringBuilder rowlefthtml = new StringBuilder();
                    StringBuilder rowrighthtml = new StringBuilder();
                    foreach (object[] ctrlinfo in FormControls)
                    {
                        if (row == (int)ctrlinfo[2])
                        {
                            Control ctrl = (Control)ctrlinfo[1];
                            if (ctrlinfo[3].ToString().Trim() == "left")
                            {
                                if (i > 0) rowlefthtml.Append(@"<div class=""fl"">&nbsp;</div>");
                                rowlefthtml.Append(@"<div class=""fl"">" + ctrl.BoxHtml() + @"</div>");
                                toprows++;
                            }
                            if (ctrlinfo[3].ToString().Trim() == "right")
                            {
                                if (i > 0) rowrighthtml.Append(@"<div class=""fl"">&nbsp;</div>");
                                rowrighthtml.Append(@"<div class=""fl"">" + ctrl.BoxHtml() + @"</div>");
                                bottomrows++;
                            }
                            bottomhtml.Append(ctrl.BoxEndHtml());
                            i++;
                        }
                    }
                    rowshtml.Append(@"<div id=""FormRows" + row + @""">");
                    rowshtml.Append(@"<div class=""fl"">" + rowlefthtml + @"<div class=""clear""></div></div>");
                    rowshtml.Append(@"<div class=""fr"">" + rowrighthtml + @"<div class=""clear""></div></div>");
                    rowshtml.Append(@"<div class=""clear""></div>");
                    rowshtml.Append(@"</div>");
                }
            }

            html.Append(@"<div id=""FormRows"">");
            html.Append(rowshtml);
            html.Append(@"</div>");
            return html;
        }

        private StringBuilder SetButtonHtml()
        {
            StringBuilder html = new StringBuilder();
            html.Append(@"<div class=""buttons"">");
            html.Append(btnhtml);
            html.Append(@"<div class=""fl acccrow"">");
            html.Append(@"<div id=""" + Id + @"_Msg"" class=""fl comment""></div>");
            html.Append(@"</div>");
            html.Append(@"<div class=""clear""></div>");
            html.Append(@"</div>");
            return html;
        }

        public StringBuilder BoxHtml()
        {
            StringBuilder html = new StringBuilder();

            SetButtonHtml();

            html.Append(@"<form id=""" + Id + @""" name=""" + Name + @""" class=""pageform"" method=""" + Method + @""" ");
            if (Action.Trim() != "") html.Append(@"action=""" + Action + @""" ");
            if (Target.Trim() != "") html.Append(@"target=""" + Target + @""" ");
            if (FormCheck) html.Append(@"onsubmit=""return chkform();""");
            html.Append(@">");
            html.Append(@"<input type=""hidden"" name=""subflag"" value=""" + SubFlag + @""" />");
            html.Append(formhtml);

            html.Append(@"<table width=""100%"" height=""100%"" cellpadding=""0"" cellspacing=""0"" border=""0"" align=""center"">");
            html.Append(@"<tr height=""5""><td></td></tr>");
            html.Append(@"<tr><td>");
            html.Append(SetTopRowHtml("top"));
            html.Append(@"</td></tr>");

            if (IsFormTables)
            {
                html.Append(@"<tr height=""5""><td></td></tr>");

                html.Append(@"<tr height=""1""><td class=""dotline""></td></tr>");
                if (EditMenusBar.Count > 0)
                {
                    html.Append(@"<tr height=""32""><td valign=""top"" align=""left"">");
                    html.Append(SetEditMenusBar());
                    html.Append(@"</td></tr>");
                }
                else
                {
                    html.Append(@"<tr height=""5""><td></td></tr>");
                }
                html.Append(@"<tr height=""180""><td valign=""top"" align=""left"" class=""datframe"">");
                html.Append(FormTables);
                html.Append(@"</td></tr>");
                html.Append(@"<tr height=""5""><td></td></tr>");
            }

            html.Append(@"<tr><td>");
            html.Append(SetTopRowHtml("bottom"));
            html.Append(@"</td></tr>");
            html.Append(@"<tr height=""50""><td valign=""top"">");
            html.Append(SetButtonHtml());
            html.Append(@"</td></tr>");
            html.Append(@"</table>");

            html.Append(@"</form>");

            return html;
        }

        private StringBuilder SetEditMenusBar()
        {
            #region SetEditMenusBar
            StringBuilder html = new StringBuilder();

            html.Append(@"<div class=""infosbar"">");
            if (EditMenusBar.Count > 0)
            {
                string leftString = "", rightString = "";
                foreach (string[] item in EditMenusBar)
                {
                    if (item.Length >= 4)
                    {
                        string itemstr = "";
                        string itempos = item[0].ToString().Trim();
                        string itemtype = item[1].ToString().Trim();
                        string itemname = item[2].ToString().Trim();
                        string itemlink = item[3].ToString().Trim();
                        if (itemtype == "ico") itemstr = @"<div class=""fl icos""><div class=""ico"" style=""background:url(/images/ico/" + itemname + @") no-repeat;""></div></div>";
                        if (itemtype == "split") itemstr = @"<div class=""fl split"">|</div>";
                        if (itemtype == "link") itemstr = @"<div class=""fl menu""><a href=""" + itemlink + @""">" + itemname + @"</a></div>";
                        if (itemtype == "memo") itemstr = @"<div class=""fl memo"">" + itemname + @"</div>";
                        if (itempos == "left") leftString += itemstr;
                        if (itempos == "right") rightString += itemstr;
                    }
                }
                html.Append(leftString);
                html.Append(@"<div class=""fr menus"">");
                html.Append(rightString);
                html.Append(@"</div>");
                html.Append(@"<div class=""clear""></div>");
            }
            html.Append(@"</div>");

            return html;
            #endregion
        }

        public StringBuilder GetBoxEndHtml()
        {
            return bottomhtml;
        }

        public StringBuilder GetFormCheckScript()
        {
            StringBuilder js = new StringBuilder();

            if (FormControls.Count > 0)
            {
                js.Append(@"function chkform(){");
                js.Append(@"var subfrm = CMSC.$$(""" + this.Id + @""");");
                js.Append(@"var isok = true;");
                js.Append(@"var isselect = false;");
                foreach (object[] ctrlinfo in FormControls)
                {
                    Control ctrl = (Control)ctrlinfo[1];
                    js.Append(ctrl.CheckScript(1));
                }
                if (IsFormTables)
                {
                    js.Append(@"if(!CMSCTableEdit.submitCheck()){");
                    js.Append(@"showcomment(""" + this.Id + @"_Msg"",""存在不完善或不合法的数据行"");");
                    js.Append(@"isok = false;");
                    js.Append(@"} else {");
                    js.Append(@"backcomment(""" + this.Id + @"_Msg"","""");");
                    js.Append(@"} ");
                    js.Append(@"var subrows = CMSCTableEdit.submitRows();");
                    js.Append(@"if(subrows=="""") {");
                    js.Append(@"showcomment(""" + this.Id + @"_Msg"",""没有添加有效的数据行"");");
                    js.Append(@"isok = false;");
                    js.Append(@"} else {");
                    js.Append(@"CMSC.$$(""SelectRows"").value = http://www.mamicode.com/subrows;");
                    js.Append(@"} ");
                }
                js.Append(@"return isok;");
                js.Append(@"} ");
            }
            else
            {
                js.Append(@"function chkform(){");
                js.Append(@"var subfrm = CMSC.$$(""" + this.Id + @""");");
                js.Append(@"var isok = true;");
                js.Append(@"return isok;");
                js.Append(@"} ");
            }

            string tipetitle = "";
            tipetitle = "提示:";
            js.Append(@"function showcomment(src, commtext) {");
            js.Append(@"CMSC.$$(src).className = ""fl redcomment"";");
            js.Append(@"CMSC.$$(src).style.paddingLeft = ""0px"";");
            js.Append(@"CMSC.$$(src).innerText = """ + tipetitle + @""" + commtext;");
            js.Append(@"}");
            js.Append(@"function backcomment(src, commtext) {");
            js.Append(@"CMSC.$$(src).className = ""fl comment"";");
            js.Append(@"CMSC.$$(src).innerText = commtext;");
            js.Append(@"}");

            return js;
        }

        #endregion

        #region 提供方法

        public void AddFormItem(string name, string value)
        {
            formhtml.Append(@"<input type=""hidden"" id=""" + name + @""" name=""" + name + @""" value=""" + value + @""" />");
        }

        public void AddRow(int rows, string valign)
        {
            FormRows.Add(new object[] { rows, valign });
        }

        public void AddControl(int rows, string align, object control)
        {
            Control ctrl = (Control)control;
            ctrl.Form = this.Id;
            string typename = ctrl.GetType().Name;
            FormControls.Add(new object[] { typename, ctrl, rows, align });
        }

        public void AddSubmitButton(string btnimage)
        {
            btnhtml.Append(@"<div class=""fl"">");
            btnhtml.Append(@"<input type=""image"" src=""" + btnimage + @""" border=""0"" />");
            btnhtml.Append(@"</div>");
            btnhtml.Append(@"<div class=""fl"">&nbsp;&nbsp;</div>");
        }

        #endregion

    }
}

 

SheetForm.cs