コード例 #1
0
        /// <summary>
        /// Generates the CodeBehind for the Editor
        /// </summary>
        /// <param name="pageName">The name of the page - can end with ASPX if you like but it will be stripped</param>
        /// <param name="providerName">The Data provider to use</param>
        /// <param name="tableName">Name of the table</param>
        /// <param name="languageType">Output language, i.e C# or that other one</param>
        /// <returns></returns>
        public static string GenerateCode(string pageName, string providerName, string tableName, LanguageType languageType)
        {
            //Grab the tableschema
            TableSchema.Table tbl          = DataService.GetTableSchema(tableName, providerName);
            string            pageTemplate = ResourceHelper.GetString(TemplateName.EDITOR_CODE);

            /*
             #NAMESPACE_USING# = if there is more than one provider, include this Using statement
             #PAGEBINDLIST# = The list that binds the page to the object
             #BINDLIST# = List that binds the object to the page
             #DROPLIST# = Loads each dropdown
             #CLASSNAME# = the name of the class
             */

            //make sure the providers are loaded
            DataService.LoadProviders();

            //Set the Namespace
            string namespaceUsing;

            //if (DataService.Databases.Count > 1)
            //{
            //have to use the namespace
            namespaceUsing = "using " + Utility.CheckNamingRules(tbl.Provider) + ";";
            //}
            pageTemplate = pageTemplate.Replace(TemplateVariable.NAMESPACE_USING, namespaceUsing);

            //Set the class name
            string className = tbl.ClassName;

            pageTemplate = pageTemplate.Replace(TemplateVariable.CLASS_NAME, className);

            //Set the page class name
            string pageClass = pageName.Replace(FileExtension.DOT_ASPX, String.Empty);

            pageTemplate = pageTemplate.Replace(TemplateVariable.PAGE_CLASS, pageClass);


            //now loop out the columns for each row of the table editor and grid
            StringBuilder bindRows     = new StringBuilder();
            StringBuilder pageBindRows = new StringBuilder();
            StringBuilder dropList     = new StringBuilder();

            foreach (TableSchema.TableColumn col in tbl.Columns)
            {
                string controlName = col.PropertyName;

                if (!col.IsPrimaryKey)
                {
                    if (col.IsForeignKey)
                    {
                        //dropdown lists

                        TableSchema.Table FKTable = DataService.GetForeignKeyTable(col, tbl);
                        if (FKTable != null)
                        {
                            dropList.AppendLine(AddTabs(2) + "Query qry" + controlName + " = " + FKTable.ClassName + ".CreateQuery();");
                            dropList.AppendLine(AddTabs(2) + "qry" + controlName + ".OrderBy = OrderBy.Asc(\"" + FKTable.Columns[1].ColumnName + "\");");
                            dropList.AppendLine(AddTabs(2) + "Utility.LoadDropDown(" + controlName + ", " + "qry" + controlName + ".ExecuteReader(), true);");
                            if (col.IsNullable)
                            {
                                dropList.AppendLine(AddTabs(2) + controlName + ".Items.Insert(0, new ListItem(\"(Not Specified)\", String.Empty));");
                            }
                        }
                    }
                    pageBindRows.AppendLine(SetControlValue(col, className));
                    if (col.DataType != DbType.Binary && col.DataType != DbType.Byte)
                    {
                        bindRows.AppendLine(GetControlValue(col, className));
                    }
                }
            }

            pageTemplate = pageTemplate.Replace(TemplateVariable.BIND_LIST, bindRows.ToString());
            pageTemplate = pageTemplate.Replace(TemplateVariable.PAGE_BIND_LIST, pageBindRows.ToString());
            pageTemplate = pageTemplate.Replace(TemplateVariable.DROP_LIST, dropList.ToString());


            return(pageTemplate);
        }
コード例 #2
0
        /// <summary>
        /// Generates the "In Front" code for an ASPX page
        /// </summary>
        /// <param name="providerName">Name of the DataProvider to use</param>
        /// <param name="tableName">Name of Table</param>
        /// <param name="pageFileName">The name of the ASPX page - like "Editor.aspx"</param>
        /// <param name="masterPage">The Master Page File, if any</param>
        /// <param name="languageType">Output language, i.e C# or that other one</param>
        /// <returns></returns>
        public static string GeneratePage(string providerName, string tableName, string pageFileName, string masterPage, LanguageType languageType)
        {
            //Grab the tableschema
            TableSchema.Table tbl = DataService.GetTableSchema(tableName, providerName);

            if (tbl.PrimaryKey == null)
            {
                return(tableName + " does not contain a primary key. Primary keys are required for scaffold generation.");
            }

            string pageTemplate = ResourceHelper.GetString(TemplateName.EDITOR);

            /*
             #PK# = Primary Key of the table
             #GRIDROWS# = all the rows in the GridView
             #EDITORROWS# = the rows inside the editor table
             #TABLENAME# = the name of the table
             */

            //this part's simple enough - just straight up text replacement
            pageTemplate = pageTemplate.Replace(TemplateVariable.TABLE_NAME, tbl.DisplayName);
            pageTemplate = pageTemplate.Replace(TemplateVariable.PK, tbl.PrimaryKey.ColumnName);
            pageTemplate = pageTemplate.Replace(TemplateVariable.PAGE_FILE, pageFileName);

            //now loop out the columns for each row of the table editor and grid
            StringBuilder gridRows   = new StringBuilder();
            StringBuilder editorRows = new StringBuilder();

            foreach (TableSchema.TableColumn col in tbl.Columns)
            {
                if (!col.IsPrimaryKey)
                {
                    string control;
                    string properties = String.Empty;
                    control = GetControlText(typeof(BoundField).Name, null);
                    AddControlProperty(ref properties, "DataField", col.ColumnName);
                    AddControlProperty(ref properties, "HeaderText", Utility.ParseCamelToProper(col.ColumnName));
                    AddControlProperty(ref properties, "SortExpression", col.ColumnName);
                    ApplyControlProperties(ref control, properties);
                    gridRows.AppendLine(AddTabs(2) + control);
                    editorRows.AppendLine("\t\t<tr>" + Environment.NewLine + "\t\t\t<td class=\"scaffoldEditItemCaption\">" + Utility.ParseCamelToProper(col.ColumnName) + "</td>" + Environment.NewLine + "\t\t\t<td class=\"scaffoldEditItem\">" + GetEditControl(col) + "</td>" + Environment.NewLine + "\t\t</tr>");
                }
            }

            pageTemplate = pageTemplate.Replace(TemplateVariable.EDITOR_ROWS, editorRows.ToString());
            pageTemplate = pageTemplate.Replace(TemplateVariable.GRID_ROWS, gridRows.ToString());


            //get the wrapper
            bool   useMaster = !String.IsNullOrEmpty(masterPage);
            string wrapper   = GetPageWrapper(useMaster);

            string jsBlock = CodeBlock.JS_BEGIN_SCRIPT + ResourceHelper.GetString(TemplateName.SCAFFOLD_DELETE_CONFIRMATION) + ResourceHelper.GetString(TemplateName.SCAFFOLD_CHARACTER_COUNTER) + CodeBlock.JS_END_SCRIPT;

            wrapper = wrapper.Replace(TemplateVariable.JAVASCRIPT_BLOCK, jsBlock);
            wrapper = wrapper.Replace(TemplateVariable.PAGE_CODE, pageTemplate.ToString());
            wrapper = wrapper.Replace(TemplateVariable.MASTER_PAGE, masterPage);
            wrapper = wrapper.Replace(TemplateVariable.TABLE_NAME, pageFileName.Replace(FileExtension.DOT_ASPX, String.Empty));

            //determine the language
            string codeFileExt = FileExtension.CS;

            if (languageType == LanguageType.VB)
            {
                codeFileExt = FileExtension.VB;
            }
            wrapper = wrapper.Replace(TemplateVariable.LANGUAGE_EXTENSION, codeFileExt);

            return(wrapper);
        }