Aspose.DotNetNuke.Modules.AsposeDynamicFormGeneratorExcel.View.InitForm C# (CSharp) Method

InitForm() private method

private InitForm ( ) : void
return void
        private void InitForm()
        {
            try
            {
                // Check for license and apply if exists
                string licenseFile = Server.MapPath("~/App_Data/Aspose.Total.lic");
                if (File.Exists(licenseFile))
                {
                    License license = new License();
                    license.SetLicense(licenseFile);
                }

                DataTable dataTable = null;
                if (Session["AsposeDynamicFormsdataTable"] == null)
                {
                    //Creating a file stream containing the Excel file to be opened
                    FileStream fstream = new FileStream(Server.MapPath("~/DesktopModules/Aspose.DNN.DynamicFormGenerator.Excel/Docs/AsposeDynamicFormsDataFile.xlsx"), FileMode.Open, FileAccess.Read);

                    //Instantiating a Workbook object
                    //Opening the Excel file through the file stream
                    Workbook workbook = new Workbook(fstream);

                    //Accessing a worksheet using its sheet name
                    Worksheet worksheet = workbook.Worksheets["Settings"];

                    //Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
                    dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.Rows.Count, 10, true);

                    //Closing the file stream to free all resources
                    fstream.Close();

                    dataTable.DefaultView.Sort = "[Sort ID] ASC";
                    dataTable = dataTable.DefaultView.ToTable();

                    Session["AsposeDynamicFormsdataTable"] = dataTable;
                }
                else
                {
                    dataTable = (DataTable)Session["AsposeDynamicFormsdataTable"];
                }

                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                    {
                        string fieldType = "Text";
                        int rows = 0;
                        foreach (DataRow row in dataTable.Rows)
                        {
                            if (!row[7].ToString().Trim().ToLower().Equals("false"))
                            {
                                rows += 1;
                                fieldType = row[1].ToString();

                                switch (fieldType)
                                {
                                    case "Title":
                                        {
                                            lblTitle.Text = row[4].ToString().Trim();
                                        }
                                        break;
                                    case "Success":
                                        {
                                            success_msg.InnerText = row[4].ToString().Trim();
                                        }
                                        break;
                                    case "Text":
                                        {
                                            Label lbl = new Label();
                                            lbl.ID = "lbl" + row[2].ToString();
                                            lbl.Text = row[3].ToString();
                                            lbl.CssClass = "dnnLabel";
                                            myPlaceHolder.Controls.Add(lbl);

                                            TextBox textBox = new TextBox();
                                            textBox.ID = row[2].ToString().Trim();
                                            textBox.Attributes.Add("placeholder", row[4].ToString().Trim());
                                            if (myPlaceHolder.FindControl(textBox.ID) == null)
                                                myPlaceHolder.Controls.Add(textBox);
                                            LiteralControl literalBreak = new LiteralControl("<br />");
                                            myPlaceHolder.Controls.Add(literalBreak);
                                        }
                                        break;
                                    case "MultiText":
                                        {
                                            Label lbl = new Label();
                                            lbl.ID = "lbl" + row[2].ToString().Trim();
                                            lbl.Text = row[3].ToString().Trim();
                                            lbl.CssClass = "dnnLabel";
                                            myPlaceHolder.Controls.Add(lbl);

                                            TextBox textBox = new TextBox();
                                            textBox.ID = row[2].ToString().Trim();
                                            textBox.Attributes.Add("placeholder", row[4].ToString().Trim());
                                            textBox.TextMode = TextBoxMode.MultiLine;
                                            textBox.Rows = 5;
                                            if (myPlaceHolder.FindControl(textBox.ID) == null)
                                                myPlaceHolder.Controls.Add(textBox);
                                            LiteralControl literalBreak = new LiteralControl("<br />");
                                            myPlaceHolder.Controls.Add(literalBreak);
                                        }
                                        break;
                                    case "Radio":
                                        {
                                            Label lbl = new Label();
                                            lbl.ID = "lbl" + row[2].ToString().Trim();
                                            lbl.Text = row[3].ToString().Trim();
                                            lbl.CssClass = "dnnLabel";
                                            myPlaceHolder.Controls.Add(lbl);

                                            int i = 0;
                                            foreach (string strItem in row[2].ToString().Trim().Split(';'))
                                            {
                                                if (!strItem.Equals(""))
                                                {
                                                    RadioButton radioButton = new RadioButton();
                                                    radioButton.GroupName = "RadioGroup" + rows.ToString();

                                                    if (row[4].ToString().Trim().Split(';').Length >= i)
                                                    {
                                                        radioButton.Text = row[4].ToString().Trim().Split(';')[i];
                                                        radioButton.ID = row[2].ToString().Trim().Split(';')[i];
                                                        if (i == 0)
                                                        {
                                                            radioButton.Checked = true;
                                                        }
                                                    }
                                                    if (myPlaceHolder.FindControl(radioButton.ID) == null)
                                                        myPlaceHolder.Controls.Add(radioButton);
                                                    i++;
                                                }
                                            }
                                            LiteralControl literalBreak = new LiteralControl("<br />");
                                            myPlaceHolder.Controls.Add(literalBreak);

                                        }
                                        break;
                                    case "Check":
                                        {
                                            Label lbl = new Label();
                                            lbl.ID = "lbl" + row[2].ToString().Trim();
                                            lbl.Text = row[3].ToString().Trim();
                                            lbl.CssClass = "dnnLabel";
                                            myPlaceHolder.Controls.Add(lbl);

                                            int i = 0;
                                            foreach (string strItem in row[4].ToString().Trim().Split(';'))
                                            {
                                                if (!strItem.Equals(""))
                                                {
                                                    CheckBox checkBox = new CheckBox();

                                                    if (row[4].ToString().Trim().Split(';').Length >= i)
                                                    {
                                                        checkBox.Text = row[4].ToString().Trim().Split(';')[i];
                                                        checkBox.ID = row[2].ToString().Trim().Split(';')[i];
                                                    }
                                                    if (myPlaceHolder.FindControl(checkBox.ID) == null)
                                                        myPlaceHolder.Controls.Add(checkBox);
                                                    i++;
                                                }
                                            }
                                            LiteralControl literalBreak = new LiteralControl("<br />");
                                            myPlaceHolder.Controls.Add(literalBreak);

                                        }
                                        break;
                                    case "DropDown":
                                        {
                                            Label lbl = new Label();
                                            lbl.ID = "lbl" + row[2].ToString().Trim();
                                            lbl.Text = row[3].ToString().Trim();
                                            lbl.CssClass = "dnnLabel";
                                            myPlaceHolder.Controls.Add(lbl);

                                            DropDownList dropdownList = new DropDownList();
                                            dropdownList.ID = row[2].ToString().Trim();

                                            foreach (string strItem in row[4].ToString().Trim().Split(';'))
                                            {
                                                if (!strItem.Equals(""))
                                                {
                                                    dropdownList.Items.Add(strItem);
                                                }
                                            }
                                            if (myPlaceHolder.FindControl(dropdownList.ID) == null)
                                                myPlaceHolder.Controls.Add(dropdownList);
                                            LiteralControl literalBreak = new LiteralControl("<br />");
                                            myPlaceHolder.Controls.Add(literalBreak);
                                        }
                                        break;
                                    default:

                                        break;
                                }
                            }
                        }
                    }
                }

            }
            catch (Exception exc)
            {
                success_msg.Visible = false;
                error_msg.Visible = true;
                error_msg.InnerText = exc.Message;
            }
        }