private void RenderPageWithFields(Dictionary <string, string> fieldsToShow) { Dictionary <string, PEValidator> validatorList = Session["validatorDictionary"] as Dictionary <string, PEValidator>; Dictionary <string, string> controlIDs = new Dictionary <string, string>(); Dictionary <string, ControlType> controlDControlType = new Dictionary <string, ControlType>(); string value; foreach (var item in fieldsToShow) { PEValidator validator = validatorList[item.Key]; if (validator.ValueType.Equals("Date")) { try { value = Convert.ToDateTime(item.Value).ToString("dd-MM-yyyy"); } catch { value = item.Value; } } else { value = item.Value; } ControlIDWithType elementIDType = CreateElement(item.Key, value, validator); controlIDs.Add(elementIDType.ID, item.Key); controlDControlType.Add(elementIDType.ID, elementIDType.Type); } Session["controlIDsWithType"] = controlDControlType; Session["controlIDs"] = controlIDs; }
int GetMaxDefinedLength(Dictionary <string, PEValidator> validatorList) { int returnLength = 0; foreach (var item in validatorList) { PEValidator validator = item.Value; if (validator.DisplayLength != 0) { if (returnLength < validator.DisplayLength) { returnLength = validator.DisplayLength; } } } if (returnLength > MaxDisplayLength) { returnLength = MaxDisplayLength; } if (returnLength < MinDisplayLength) { returnLength = MinDisplayLength; } return(returnLength); }
int GetMaxUndefinedLength(Dictionary <string, PEValidator> validatorList, Dictionary <string, string> fieldsToShow) { int returnLength = 0; foreach (var item in fieldsToShow) { PEValidator validator = validatorList[item.Key]; string defaultValue = validator.DefaultValue != null?validator.DefaultValue.ToString() : string.Empty; string value = item.Value.Trim().Equals(string.Empty) ? defaultValue : item.Value.Trim(); int valueLength = value.Length * CharLength; if (returnLength < valueLength) { returnLength = valueLength; } } if (returnLength > MaxDisplayLength) { returnLength = MaxDisplayLength; } if (returnLength < MinDisplayLength) { returnLength = MinDisplayLength; } return(returnLength); }
protected void pickBtn_Click(object sender, EventArgs e) { try { Button pickBtn = (Button)sender; string targetFieldId = pickBtn.Attributes["targetFieldId"].ToString(); string fieldName = pickBtn.Attributes["inputFieldName"].ToString(); hdnCurTargetControlIdForGridvalue.Value = targetFieldId; Dictionary <string, PEValidator> validatorList = Session["validatorDictionary"] as Dictionary <string, PEValidator>; PEValidator curValidator = validatorList[fieldName]; IDatabaseFunctionsManager db = DBManagerFactory.GetDBManager(activeDB, conStringLOV_SQL); DataTable dt = db.GetDataTable(curValidator.LOVValue); DataTable dtCloned = CloneDataTableWithAllColumnStringDataType(dt); gvPopup.DataSource = dtCloned; Session["GridDataTable"] = dtCloned; gvPopup.DataBind(); modalGrid.Show(); } catch (Exception ex) { ScriptManager.RegisterStartupScript(Page, typeof(Page), "errorExists", "<script>$(document).ready(function(){alert('Cannot load the grid.\\nSQL in the LOVP is not correct for this field or Connection string invalid.')});</script>", false); } }
protected void CustomValidator_Handler(object source, ServerValidateEventArgs args) { Dictionary <string, string> controlIDFieldNamePair = Session["controlIDs"] as Dictionary <string, string>; Dictionary <string, PEValidator> validatorList = Session["validatorDictionary"] as Dictionary <string, PEValidator>; CustomValidator cv = (CustomValidator)source; string fieldName = controlIDFieldNamePair[cv.ControlToValidate]; PEValidator curValidator = validatorList[fieldName]; if (!curValidator.IsValidInput(args.Value)) { args.IsValid = false; cv.ErrorMessage = curValidator.ErrorMsg; } }
int GetLabelsMaxCharacter(Dictionary <string, PEValidator> validatorList, Dictionary <string, string> fieldsToShow) { int charCount = 0; foreach (var item in fieldsToShow) { PEValidator validator = validatorList[item.Key]; string caption = validator.Caption != string.Empty ? validator.Caption : item.Key; if (charCount < caption.Length) { charCount = caption.Length; } } return(charCount); }
bool CheckInputConsistentWithList(PEValidator validator, string value) { IDatabaseFunctionsManager db = DBManagerFactory.GetDBManager(activeDB, conStringLOV_SQL); DataTable dt = db.GetDataTable(validator.LOVValue); string fieldName = validator.FieldName; DataType curType = (DataType)Enum.Parse(typeof(DataType), validator.ValueType, true); bool fieldExists = false; foreach (DataColumn dc in dt.Columns) { if (dc.ColumnName.ToLower() == fieldName.ToLower()) { fieldExists = true; break; } } if (fieldExists) { foreach (DataRow record in dt.Rows) { if (curType == DataType.Date) { if (record[fieldName].ToString().Trim() != Convert.ToDateTime(record[fieldName]).ToString("dd-MM-yyyy")) { return(true); } } else { if (record[fieldName].ToString() == value) { return(true); } } } } return(false); }
private string IsAllFieldValid(ref bool warningExist, ref List <string> warningField) { Dictionary <string, ControlType> controlDControlType = Session["controlIDsWithType"] as Dictionary <string, ControlType>; Dictionary <string, string> controlIDsFieldName = Session["controlIDs"] as Dictionary <string, string>; Dictionary <string, PEValidator> validators = Session["validatorDictionary"] as Dictionary <string, PEValidator>; bool returnValue = true; string errorMsg = ""; foreach (var item in controlIDsFieldName) { string fieldName = item.Value; string controlID = item.Key; PEValidator curValidator = validators[fieldName]; if (curValidator.AllowEdit.Equals(false)) { continue; } ControlType cType = controlDControlType[controlID]; string labelCaption = curValidator.Caption != "" ? curValidator.Caption : fieldName; labelCaption = labelCaption.ToFirstCharUpper(); hdnErrorCtrID.Value = ""; switch (cType) { case ControlType.TextBox: TextBox txtControl = Page.FindControl(controlID) as TextBox; string value = txtControl.Text; if (curValidator.Mandatory == EnumMandatory.Lov) { if (curValidator.LOVValue != null) { bool isConsistent = CheckInputConsistentWithList(curValidator, value); if (!isConsistent) { if (curValidator.ErrorLevel == 2) { errorMsg = string.Format("In {0} field {1} is not consistent with the list", labelCaption, value); returnValue = false; } else { curValidator.WanrningExist = true; } } } else if ((string)curValidator.ExtractedValue != value) { errorMsg = labelCaption + " value cannot be changed. No list of values definded"; returnValue = false; } } if (returnValue && !curValidator.IsValidInput(value)) { errorMsg = string.Format("In {0} field value {1} is not valid. {2}", labelCaption, value, curValidator.ErrorMsg); txtControl.ForeColor = System.Drawing.Color.Red; returnValue = false; } if (curValidator.WanrningExist) { warningExist = curValidator.WanrningExist; warningField.Add(fieldName); txtControl.ForeColor = System.Drawing.Color.Red; } else { txtControl.ForeColor = System.Drawing.Color.Black; } break; case ControlType.DropDownList: DropDownList ddl = Page.FindControl(controlID) as DropDownList; TextBox txtBxEditDropdown = Page.FindControl("txt" + controlID) as TextBox; value = txtBxEditDropdown.Text; if (curValidator.Mandatory == EnumMandatory.Lov) { bool isConsistent = CheckInputConsistentWithList(ref ddl, value); if (!isConsistent) { if (curValidator.ErrorLevel == 2) { errorMsg = string.Format("In {0} field \"{1}\" is not consistent with the list", labelCaption, value); returnValue = false; } else { curValidator.WanrningExist = true; } } } if (returnValue && !curValidator.IsValidInput(value)) { errorMsg = string.Format("Invalid value in {0} field. {1}", labelCaption, curValidator.ErrorMsg); returnValue = false; } if (curValidator.WanrningExist) { warningExist = curValidator.WanrningExist; warningField.Add(fieldName); } break; default: break; } if (!returnValue) { hdnErrorCtrID.Value = controlID; break; } } return(returnValue ? "" : errorMsg); }
Dictionary <string, string> GetFieldsWithValues(bool warningExist) { Dictionary <string, string> fieldValues = new Dictionary <string, string>(); Dictionary <string, PEValidator> validators = Session["validatorDictionary"] as Dictionary <string, PEValidator>; Dictionary <string, ControlType> controlDControlType = Session["controlIDsWithType"] as Dictionary <string, ControlType>; Dictionary <string, string> controlIDsFieldName = Session["controlIDs"] as Dictionary <string, string>; foreach (var item in controlIDsFieldName) { string fieldName = item.Value; string controlID = item.Key; ControlType cType = controlDControlType[controlID]; PEValidator curValidator = validators[fieldName]; DataType curType = (DataType)Enum.Parse(typeof(DataType), curValidator.ValueType, true); if (curType == DataType.Objarea || curType == DataType.Objlen || (!warningExist && curValidator.AllowEdit.Equals(false))) { continue; } switch (cType) { case ControlType.TextBox: TextBox txtControl = Page.FindControl(controlID) as TextBox; if (curType == DataType.Lastupdate) { fieldValues.Add(fieldName, DateTime.Today.ToString("yyyy-MM-dd")); } else if (curType == DataType.Date) { string date = DateTime.ParseExact(txtControl.Text, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"); fieldValues.Add(fieldName, date); } else { fieldValues.Add(fieldName, txtControl.Text); } break; case ControlType.DropDownList: DropDownList ddl = Page.FindControl(controlID) as DropDownList; string value = (Page.FindControl("txt" + controlID) as TextBox).Text; if (curType == DataType.Date) { try { value = DateTime.ParseExact(value, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"); } catch { } } fieldValues.Add(fieldName, value); break; default: break; } } return(fieldValues); }
private ControlIDWithType CreateElement(string fieldName, string userInputValue, PEValidator validator) { ControlIDWithType idType = new ControlIDWithType(); string value = userInputValue; if (value.Trim() == string.Empty && validator.DefaultValue != null) { value = validator.DefaultValue.ToString(); } Label lbl = new Label(); lbl.ID = string.Concat("lbl", fieldName); lbl.Style.Add("width", "100%"); if (validator.Caption != string.Empty) { lbl.Text = validator.Caption.ToFirstCharUpper(); } else { lbl.Text = fieldName.ToFirstCharUpper(); } HtmlGenericControl divLabels = new HtmlGenericControl("div"); divLabels.Style.Add("padding-right", "10px"); divLabels.Style.Add("text-align", "right"); divLabels.Controls.Add(lbl); TextBox txtBx = new TextBox(); DropDownList ddl = new DropDownList(); HtmlGenericControl divControl = new HtmlGenericControl("div"); DataType curType = (DataType)Enum.Parse(typeof(DataType), validator.ValueType, true); if (validator.LovType == EnumLovType.Lovc || validator.LovType == EnumLovType.Lovcp) { ddl.ID = string.Concat("ddl", fieldName); ddl.ToolTip = validator.ToolTip; ddl.Attributes.Add("onchange", "SelectDDLValue(this,'txtddl" + fieldName + "')"); ddl.Style.Add("z-index", "-100"); hdnDrpIDs.Value += ddl.ID + ","; if (validator.LovType != EnumLovType.Lovc || (validator.LovType == EnumLovType.Lovc && validator.LOVValue.Trim().ToLower().StartsWith("select"))) { IDatabaseFunctionsManager db = DBManagerFactory.GetDBManager(activeDB, conStringLOV_SQL); DataTable dt = db.GetDataTable(validator.LOVValue); if (IsColumnExistInTable(dt, fieldName)) { if (curType != DataType.Date) { ddl.DataSource = dt; ddl.DataTextField = fieldName; ddl.DataValueField = fieldName; } else { List <string> dates = new List <string>(); foreach (DataRow record in dt.Rows) { if (record[fieldName].ToString().Trim() != "") { dates.Add(Convert.ToDateTime(record[fieldName]).ToString("dd-MM-yyyy")); } } ddl.DataSource = dates; } } } else { char delimiter = validator.LOVValue[0]; //if (validator.LOVValue.StartsWith("/")) // delimiter = '/'; //else if (validator.LOVValue.StartsWith(";")) // delimiter = ';'; //else delimiter = ','; string[] valueList = validator.LOVValue.Split(delimiter); ddl.DataSource = valueList.Where <string>(val => val != string.Empty).ToList <string>(); } ddl.DataBind(); for (int i = 0; i < ddl.Items.Count; i++) { if (ddl.Items[i].Value.ToLower() == userInputValue.ToLower()) { ddl.Items[i].Selected = true; break; } } divControl.Controls.Add(ddl); idType.ID = ddl.ID; idType.Type = ControlType.DropDownList; if (validator.AllowEdit && validator.LovType == EnumLovType.Lovcp) { ddl.Style.Add("width", validator.ActualLength - 14 + "px"); } else { ddl.Style.Add("width", validator.ActualLength + 4 + "px"); } if (validator.AllowEdit) { if (validator.LovType == EnumLovType.Lovcp) { CreatePickButton(ddl.ID, fieldName, ref divControl); } // Text box for editing dropdown list. txtBx.ID = string.Concat("txtddl", fieldName); txtBx.Style.Add("position", "absolute"); txtBx.Style.Add("display", "none"); txtBx.Style.Add("border-style", "none"); if (!userInputValue.Trim().Equals(string.Empty)) { if (ddl.SelectedValue != null && ddl.SelectedValue != "") { if (ddl.SelectedValue.Trim().ToLower() == userInputValue.Trim().ToLower()) { txtBx.Text = ddl.SelectedValue; } else { txtBx.Text = userInputValue; } } } else { txtBx.Text = ddl.SelectedValue; } divControl.Controls.Add(txtBx); txtBx.ToolTip = validator.ToolTip; } else { ddl.Enabled = false; } //if (!validator.IsInitialValueValid(value)) //{ // ddl.ForeColor = System.Drawing.Color.Red; //} } else { txtBx.ID = string.Concat("txt", fieldName); txtBx.Text = value; divControl.Controls.Add(txtBx); txtBx.ToolTip = validator.ToolTip; txtBx.Style.Add("width", validator.ActualLength + "px"); if (validator.AllowEdit && (validator.LovType == EnumLovType.Lovp || curType == DataType.Date)) { txtBx.Style.Add("width", validator.ActualLength - 18 + "px"); } else { txtBx.Style.Add("width", validator.ActualLength + "px"); } if (validator.AllowEdit) { if (validator.LovType == EnumLovType.Lovp) { CreatePickButton(txtBx.ID, fieldName, ref divControl); } } else { txtBx.ReadOnly = true; } switch (curType) { case DataType.Integer: if (validator.MinValueInt >= 0 && validator.MaxValueInt > 0 && validator.ErrorLevel == 2) { RangeValidator rv = new RangeValidator(); rv.ID = string.Concat("rv", fieldName); rv.Type = ValidationDataType.Integer; rv.MinimumValue = validator.MinValueInt.ToString(); rv.MaximumValue = validator.MaxValueInt.ToString(); rv.ControlToValidate = txtBx.ID; rv.ErrorMessage = string.Format("Value should be between {0} and {1}", validator.MinValueInt, validator.MaxValueInt); rv.ValidationGroup = "peValidation"; divControl.Controls.Add(rv); } break; case DataType.Float: if (validator.ErrorLevel == 2) { if (validator.MinValueInt > 0 && validator.MaxValueInt > 0) { RangeValidator rv = new RangeValidator(); rv.ID = string.Concat("rv", fieldName); rv.Type = ValidationDataType.Double; rv.MinimumValue = validator.MinValueFloat.ToString(); rv.MaximumValue = validator.MaxValueFloat.ToString(); rv.ControlToValidate = txtBx.ID; rv.ErrorMessage = string.Format("Value should be between {0} and {1}", validator.MinValueFloat, validator.MaxValueFloat); rv.ValidationGroup = "peValidation"; divControl.Controls.Add(rv); } } break; case DataType.String: //if (validator.MaxStringLength > 0) //{ // txtBx.MaxLength = validator.MaxStringLength; //} if (validator.ErrorLevel == 2) { CustomValidator cv = new CustomValidator(); cv.ID = string.Concat("cv", fieldName); cv.ValidationGroup = "peValidation"; cv.ControlToValidate = txtBx.ID; cv.ValidateEmptyText = true; cv.ServerValidate += new ServerValidateEventHandler(CustomValidator_Handler); divControl.Controls.Add(cv); } break; case DataType.Date: if (value.Trim() == string.Empty) { txtBx.Text = value = DateTime.Today.ToString("dd-MM-yyy"); } if (validator.AllowEdit == true && validator.LovType != EnumLovType.Lovp) { ImageButton imgBtn = new ImageButton(); imgBtn.ID = string.Concat("imgBtn", fieldName); imgBtn.ImageUrl = "Images/calendericon.png"; imgBtn.Attributes.Add("onmouseover", "ShowCalander(this)"); CalendarExtender calander = new CalendarExtender(); calander.ID = string.Concat("cal", fieldName); calander.TargetControlID = txtBx.ID; calander.PopupButtonID = imgBtn.ID; calander.Format = "dd-MM-yyyy"; divControl.Controls.Add(imgBtn); divControl.Controls.Add(calander); } if (validator.ErrorLevel == 2) { CustomValidator cv = new CustomValidator(); cv.ID = string.Concat("cv", fieldName); cv.ValidationGroup = "peValidation"; cv.ControlToValidate = txtBx.ID; cv.ValidateEmptyText = true; cv.ServerValidate += new ServerValidateEventHandler(CustomValidator_Handler); divControl.Controls.Add(cv); } break; default: break; } if (validator.Mandatory == EnumMandatory.Yes && validator.ErrorLevel != 3) { RequiredFieldValidator rfv = new RequiredFieldValidator(); rfv.ID = string.Concat("rfv", fieldName); rfv.ControlToValidate = txtBx.ID; rfv.ErrorMessage = "Required"; rfv.ValidationGroup = "peValidation"; divControl.Controls.Add(rfv); } idType.ID = txtBx.ID; idType.Type = ControlType.TextBox; } // Check Initail value is correct or has an error. // If error than make it Red color. bool foundError = false; if (validator.Mandatory == EnumMandatory.Lov && userInputValue.Trim() != string.Empty) { //if (validator.LovType != EnumLovType.Lovc && (validator.LovType == EnumLovType.Lovc && validator.LOVValue.ToLower().StartsWith("select"))) if (validator.LovType != EnumLovType.Lovp) { // dropdown control if (!CheckInputConsistentWithList(ref ddl, userInputValue.Trim())) { foundError = true; } } else if (!CheckInputConsistentWithList(validator, userInputValue.Trim())) { foundError = true; } } if (foundError || !validator.IsInitialValueValid(value)) { txtBx.ForeColor = System.Drawing.Color.Red; } HtmlGenericControl table = new HtmlGenericControl("table"); HtmlGenericControl row = new HtmlGenericControl("tr"); HtmlGenericControl firstCol = new HtmlGenericControl("td"); HtmlGenericControl secondCol = new HtmlGenericControl("td"); // By default labels length = 128px; Considered that we have max 15 chars. // For more char than 15 we add CharLength(7px) for each char. int labelLength = 228; if (lebalMaxChar > 35 && inputBoxMaxLength < MaxDisplayLength) { int availableSpace = MaxDisplayLength - inputBoxMaxLength; int extraLength = (lebalMaxChar - 35) * CharLength; if (extraLength <= availableSpace) { labelLength = labelLength + extraLength; } else { labelLength = labelLength + availableSpace; } } if (maxLabelLength < labelLength) { maxLabelLength = labelLength; } table.Attributes.Add("width", "100%"); firstCol.Style.Add("width", labelLength + "px"); firstCol.Attributes.Add("valign", "top"); secondCol.Attributes.Add("valign", "top"); table.Controls.Add(row); row.Controls.Add(firstCol); row.Controls.Add(secondCol); firstCol.Controls.Add(divLabels); secondCol.Controls.Add(divControl); pnlControl.Controls.Add(table); return(idType); }
private PEValidator GetFieldValidator(DataRow restrictions, KeyValuePair <string, string> item) { PEValidator validator = new PEValidator(); //DataType curType = (DataType)Enum.Parse(typeof(DataType), validator.ValueType, true); string propertyValue = item.Value; string fieldName = item.Key; string type = restrictions["fieldtype"].ToString().ToLower(); string allowEdit = restrictions["allowedit"].ToString().ToLowerInvariant(); validator.FieldName = fieldName; validator.Mandatory = (EnumMandatory)Enum.Parse(typeof(EnumMandatory), restrictions["mandatory"].ToString(), true); if (allowEdit.Equals('t') || allowEdit.Equals("true")) { validator.AllowEdit = true; } else { validator.AllowEdit = false; } switch (type) { case "int": validator.ValueType = DataType.Integer.ToString(); validator.MinValueInt = restrictions["minvalue"].ToString() != string.Empty ? Convert.ToInt32(restrictions["minvalue"]) : 0; validator.MaxValueInt = restrictions["maxvalue"].ToString() != string.Empty ? Convert.ToInt32(restrictions["maxvalue"]) : 0; break; case "float": validator.ValueType = DataType.Float.ToString(); validator.MinValueFloat = restrictions["minvalue"].ToString() != string.Empty ? Convert.ToDouble(restrictions["minvalue"]) : 0; validator.MaxValueFloat = restrictions["maxvalue"].ToString() != string.Empty ? Convert.ToDouble(restrictions["maxvalue"]) : 0; validator.MaxDecimals = restrictions["decimals"].ToString() != string.Empty ? Convert.ToInt32(restrictions["decimals"]) : 0; break; case "str": validator.ValueType = DataType.String.ToString(); validator.MaxStringLength = restrictions["strlen"].ToString() != string.Empty ? Convert.ToInt32(restrictions["strlen"]) : 0; break; case "date": try { propertyValue = Convert.ToDateTime(item.Value).ToString("dd-MM-yyyy"); } catch { } validator.ValueType = DataType.Date.ToString(); break; case "lastupdate": propertyValue = Convert.ToDateTime(item.Value).ToString("dd-MM-yyyy"); validator.ValueType = DataType.Lastupdate.ToString(); validator.AllowEdit = false; validator.Mandatory = EnumMandatory.No; break; case "objlen": validator.ValueType = DataType.Objlen.ToString(); validator.AllowEdit = false; break; case "objarea": validator.ValueType = DataType.Objarea.ToString(); validator.AllowEdit = false; break; default: break; } validator.Caption = restrictions["caption"] != null ? restrictions["caption"].ToString().Trim() : string.Empty; validator.DefaultValue = restrictions["default"].ToString() != "" ? restrictions["default"] as object : null; validator.ExtractedValue = propertyValue; validator.ToolTip = restrictions["tip"].ToString().Trim(); if (restrictions["displen"] != null && restrictions["displen"].ToString() != "") { validator.DisplayLength = Convert.ToInt32(restrictions["displen"]); } else { validator.DisplayLength = 0; } //LogWriter.WriteLog("rest: " + restrictions["allowedit"]); //allowError = false; //string allowErr = restrictions["allowerror"].ToString().ToLowerInvariant(); //if (allowErr.Equals('t') || allowErr.Equals("true")) //{ // allowError = true; //} //validator.AllowError = allowError; validator.ErrorLevel = Convert.ToInt16(restrictions["errorlevel"]); //if (validator.Mandatory == EnumMandatory.Lov) //{ if (restrictions["lovcp"].ToString().Trim() != string.Empty) { validator.LOVValue = restrictions["lovcp"].ToString().Trim(); validator.LovType = EnumLovType.Lovcp; } else if (restrictions["lovp"].ToString().Trim() != string.Empty) { validator.LOVValue = restrictions["lovp"].ToString().Trim(); validator.LovType = EnumLovType.Lovp; } else if (restrictions["lovc"].ToString().Trim() != string.Empty) { validator.LOVValue = restrictions["lovc"].ToString().Trim(); validator.LovType = EnumLovType.Lovc; } else { validator.LovType = EnumLovType.None; } //} return(validator); }