// 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.IO; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace Spludlow { public class WebControls { private const string WaitText = "Doing..."; public static Control Find(string id, Control control) { foreach (Control childControl in Children(control)) { if (childControl.ID == id) return childControl; } return null; } public static Control[] Children(Control control) { return Children(control, null); } public static Control[] Children(Control control, Type type) { List list = new List(); ChildrenWork(list, control, type); return list.ToArray(); } private static void ChildrenWork(List list, Control control, Type type) { if (type == null || control.GetType() == type) list.Add(control); foreach (Control childControl in control.Controls) ChildrenWork(list, childControl, type); } public static void DisableOnClick(Page page) { foreach (Control control in Children(page)) { if (control is Button) DisableOnClick((Button)control); if (control is HyperLink) DisableOnClick((HyperLink)control); } } public static void DisableOnClick(Button button) { DisableOnClick(button, button.Attributes, "this.value"); } public static void DisableOnClick(HyperLink hyperLink) { DisableOnClick(hyperLink, hyperLink.Attributes, "innerText"); } private static void DisableOnClick(Control control, AttributeCollection attributes, string parameter) { StringBuilder text = new StringBuilder(); if (parameter != null) { text.Append(parameter); text.Append("='"); text.Append(WaitText); text.Append("';"); text.Append(control.Page.ClientScript.GetPostBackEventReference(control, null)); text.Append(";"); } text.Append("this.disabled=true;"); attributes.Add("onclick", text.ToString()); } public static void ToggleCheckBoxes(GridView gridView, string checkBoxName) { foreach (GridViewRow row in gridView.Rows) { CheckBox checkBox = (CheckBox)row.FindControl(checkBoxName); checkBox.Checked = !checkBox.Checked; } } public static DataKey[] CheckedKeys(GridView gridView, string checkBoxName) { List list = new List(); foreach (GridViewRow row in gridView.Rows) { CheckBox checkBox = (CheckBox)row.FindControl(checkBoxName); if (checkBox.Checked == true) list.Add(gridView.DataKeys[row.RowIndex]); } return list.ToArray(); } public static int[] CheckedKeysInt32(GridView gridView, string checkBoxName, string dataKeyName) { DataKey[] keys = CheckedKeys(gridView, checkBoxName); int[] results = new int[keys.Length]; for (int index = 0; index < keys.Length; ++index) results[index] = (int)keys[index][dataKeyName]; return results; } public static string[] CheckedKeysString(GridView gridView, string checkBoxName, string dataKeyName) { DataKey[] keys = CheckedKeys(gridView, checkBoxName); string[] results = new string[keys.Length]; for (int index = 0; index < keys.Length; ++index) results[index] = (string)keys[index][dataKeyName]; return results; } public static string KeyString(GridView gridView, int index, string dataKeyName) { if (gridView.DataKeys[index][dataKeyName].GetType() == typeof(DBNull)) return null; return (string)gridView.DataKeys[index][dataKeyName]; } public static int KeyInt32(GridView gridView, int index, string dataKeyName) { if (gridView.DataKeys[index][dataKeyName].GetType() == typeof(DBNull)) return 0; return (int)gridView.DataKeys[index][dataKeyName]; } public static long KeyInt64(GridView gridView, int index, string dataKeyName) { if (gridView.DataKeys[index][dataKeyName].GetType() == typeof(DBNull)) return 0; return (long)gridView.DataKeys[index][dataKeyName]; } public static void AddItems(DropDownList dropDownlist, string[] items, int selectedIndex) { foreach (string item in items) dropDownlist.Items.Add(item); dropDownlist.SelectedIndex = selectedIndex; } public static int SelectItem(DropDownList dropDownlist, string item) { int foundIndex = -1; List info = new List(); if (item != null) { for (int index = 0; index < dropDownlist.Items.Count; ++index) { ListItem listitem = dropDownlist.Items[index]; info.Add(listitem.Value + "#" + item); if (listitem.Value == item) { foundIndex = index; break; } } } dropDownlist.SelectedIndex = foundIndex; return foundIndex; } public static int SelectItem(DropDownList dropDownlist, double match) { int foundIndex = -1; for (int index = 0; index < dropDownlist.Items.Count; ++index) { ListItem listitem = dropDownlist.Items[index]; double itemValue = Double.Parse(listitem.Value); if (match == itemValue) { foundIndex = index; break; } } dropDownlist.SelectedIndex = foundIndex; return foundIndex; } public static bool TrimRequired(TextBox[] textBoxes) { bool valid = true; foreach (TextBox textBox in textBoxes) { textBox.Text = textBox.Text.Trim(); if (textBox.Text.Length == 0) valid = false; } return valid; } public static void BindDropDown(DropDownList dropDownList, object dataSource, string dataValueField, string dataTextField) { dropDownList.DataSource = dataSource; dropDownList.DataValueField = dataValueField; dropDownList.DataTextField = dataTextField; dropDownList.DataBind(); } 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 SetDropDown(DropDownList dropDownList, string value) { int foundIndex = -1; for (int index = 0; index < dropDownList.Items.Count; ++index) { if (value == dropDownList.Items[index].Value) { foundIndex = index; break; } } if (foundIndex != -1) dropDownList.SelectedIndex = foundIndex; } public static DataRow GridViewDataRow(GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return null; return ((DataRowView)e.Row.DataItem).Row; } public static DataRow DataListDataRow(DataListItemEventArgs e) { if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return null; return ((DataRowView)e.Item.DataItem).Row; } } }