// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Collections; namespace Spludlow { public class WebForms { public static string[] ReadForm(Page page, DataRow row) { return ReadForm(page, row, ""); } public static string[] ReadForm(Page page, DataRow row, string namePrefix) { List errors = new List(); string[] controlTypes = new string[] { "TextBox", "DropDownList", "Calendar", "CheckBox" }; foreach (DataColumn column in row.Table.Columns) { if (column.ReadOnly == true) continue; foreach (string controlType in controlTypes) { string controlName = controlType + namePrefix + column.ColumnName; Control control = Spludlow.WebControls.Find(controlName, page); if (control != null) { string text = null; switch (controlType) { case "TextBox": TextBox textBox = (TextBox)control; textBox.Text = textBox.Text.Trim(); text = textBox.Text; break; case "DropDownList": DropDownList dropDownList = (DropDownList)control; text = dropDownList.SelectedValue; break; case "Calendar": Calendar calender = (Calendar)control; if (calender.SelectedDate != DateTime.MinValue) text = calender.SelectedDate.ToString(); break; case "CheckBox": CheckBox checkBox = (CheckBox)control; text = checkBox.Checked.ToString(); break; default: throw new ApplicationException("WebUI, ReadForm; Control type not supported:\t" + controlName); } if (text != null) { try { switch (column.DataType.Name) { case "String": row[column] = text; break; case "Int32": if (text == "") text = "0"; row[column] = Int32.Parse(text); break; case "DateTime": if (text != "") row[column] = DateTime.Parse(text); break; case "Decimal": if (text == "") text = "0"; row[column] = Decimal.Parse(text); break; case "Boolean": row[column] = Boolean.Parse(text); break; default: throw new ApplicationException("Data type not supported"); } } catch (Exception ee) { errors.Add(controlName.Substring(controlType.Length) + ": " + ee.Message); } } break; } } } return errors.ToArray(); } public static void WriteForm(Page page, DataRow row) { WriteForm(page, row, ""); } public static void WriteForm(Page page, DataRow row, string namePrefix) { string[] controlTypes = new string[] { "TextBox", "DropDownList", "Calendar", "CheckBox", "Literal", }; foreach (DataColumn column in row.Table.Columns) { if (row.IsNull(column) == true) continue; foreach (string controlType in controlTypes) { string controlName = controlType + namePrefix + column.ColumnName; Control control = Spludlow.WebControls.Find(controlName, page); if (control != null) { string text = null; switch (column.DataType.Name) { case "String": text = (string)row[column]; break; case "Int32": text = ((Int32)row[column]).ToString(); break; case "DateTime": DateTime date = (DateTime)row[column]; if (date != DateTime.MinValue) text = date.ToString(); break; case "Decimal": text = ((Decimal)row[column]).ToString(); break; case "Boolean": text = ((Boolean)row[column]).ToString(); break; default: throw new ApplicationException("WebUI, WriteForm; Data type not supported"); } if (text != null) { switch (controlType) { case "TextBox": TextBox textBox = (TextBox)control; textBox.Text = text; break; case "DropDownList": DropDownList dropDownList = (DropDownList)control; Spludlow.WebControls.SetDropDown(dropDownList, text); break; case "Calendar": Calendar calender = (Calendar)control; calender.SelectedDate = DateTime.Parse(text); break; case "CheckBox": CheckBox checkBox = (CheckBox)control; checkBox.Checked = Boolean.Parse(text); break; case "Literal": Literal literal = (Literal)control; literal.Text = text; break; default: throw new ApplicationException("WebUI, WriteForm; Control type not supported:\t" + controlName); } } break; } } } } public static int RequestQueryInt32(Page page, string key, bool required) { string text = RequestQueryString(page, key, required); int result = 0; if (text != null && Int32.TryParse(text, out result) == false) throw new ApplicationException("WebUI, QueryString unable to parse Int32:\t" + key + " : " + text); return result; } public static string RequestQueryString(Page page, string key, bool required) { string text = page.Request.QueryString[key]; if (required == true && text == null) throw new ApplicationException("WebUI, QueryString not found:\t" + key); if (text == null) return null; return text; } public static int TrimTextBoxes(Page page) { int usedCount = 0; foreach (Control control in Spludlow.WebControls.Children(page, typeof(TextBox))) { TextBox textBox = (TextBox)control; textBox.Text = textBox.Text.Trim(); if (textBox.Text.Length > 0) ++usedCount; } return usedCount; } } }