/// <summary> /// Handles the <see cref="E:System.Web.UI.Control.Init"/> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param> protected override void OnInit(EventArgs e) { base.OnInit(e); if(!DesignMode) { // load em up // cheap way to check for load state if(Items.Count == 0) { if(!String.IsNullOrEmpty(tableName)) { DataProvider provider = DataService.GetInstance(ProviderName); SqlQuery q = new Select(provider).From(tableName); q.CheckLogicalDelete(); if(String.IsNullOrEmpty(valueField) || String.IsNullOrEmpty(textField)) { // look it up using the table schema TableSchema.Table tbl = DataService.GetSchema(tableName, providerName, TableType.Table); if(tbl != null) { if(String.IsNullOrEmpty(valueField)) valueField = tbl.PrimaryKey.ColumnName; if(String.IsNullOrEmpty(textField)) textField = tbl.Columns.Count > 1 ? tbl.Columns[1].ColumnName : tbl.Columns[0].ColumnName; } else throw new Exception("Table name '" + tableName + "' using Provider '" + providerName + "' doesn't work"); } q.SelectColumnList = new[] {valueField, textField}; if(!String.IsNullOrEmpty(OrderField)) q.OrderAsc(OrderField); else q.OrderAsc(textField); if(!String.IsNullOrEmpty(WhereField)) q.Where(WhereField).IsEqualTo(WhereValue); IDataReader rdr = null; try { rdr = q.ExecuteReader(); while(rdr.Read()) { ListItem item = new ListItem(rdr[1].ToString(), rdr[0].ToString()); Items.Add(item); } } catch(DataException x) { throw new Exception("Error loading up ListItems for " + ClientID + ": " + x.Message); } finally { if(rdr != null) rdr.Close(); } ListItem prompt = new ListItem(promptText, PromptValue); if(showPrompt) Items.Insert(0, prompt); if(!String.IsNullOrEmpty(SelectedValue)) { foreach(ListItem item in Items) { if(Utility.IsMatch(item.Value, SelectedValue)) { item.Selected = true; break; } } } } } } }
/// <summary> /// Binds the editor. /// </summary> /// <param name="tableSchema">The table schema.</param> /// <param name="keyId">The key id.</param> protected void BindEditor(TableSchema.Table tableSchema, string keyId) { //get all the data for this row SqlQuery qry = new Select(tableSchema.Provider).From(tableSchema).Where(tableSchema.PrimaryKey).IsEqualTo(keyId); //qry.AddWhere(tbl.PrimaryKey.ColumnName, keyID); using(IDataReader rdr = qry.ExecuteReader()) { if(rdr.Read()) { foreach(TableSchema.TableColumn col in tableSchema.Columns) { if(col.IsPrimaryKey) { Control ctrl = FindControl(PK_ID + col.ColumnName); if(ctrl != null) { string colValue = rdr[col.ColumnName].ToString(); Type ctrlType = ctrl.GetType(); if(ctrlType == typeof(Label)) ((Label)ctrl).Text = colValue; else if(ctrlType == typeof(DropDownList)) ((DropDownList)ctrl).SelectedValue = colValue; else if(ctrlType == typeof(TextBox)) ((TextBox)ctrl).Text = colValue; } } else { Control ctrl = FindControl(col.ColumnName); if(ctrl != null) { Type ctrlType = ctrl.GetType(); if(ctrlType == typeof(TextBox)) { TextBox tbx = ((TextBox)ctrl); tbx.Text = rdr[col.ColumnName].ToString(); } else if(ctrlType == typeof(CheckBox)) { if(!col.IsNullable || (col.IsNullable && rdr[col.ColumnName] != DBNull.Value)) ((CheckBox)ctrl).Checked = Convert.ToBoolean(rdr[col.ColumnName]); } else if(ctrlType == typeof(DropDownList)) ((DropDownList)ctrl).SelectedValue = rdr[col.ColumnName].ToString(); else if(ctrlType == typeof(CalendarControl)) { DateTime dt; if(DateTime.TryParse(rdr[col.ColumnName].ToString(), out dt)) { CalendarControl cal = (CalendarControl)ctrl; cal.SelectedDate = dt.Date; } } else if(ctrlType == typeof(Label)) ((Label)ctrl).Text = rdr[col.ColumnName].ToString(); } } } } rdr.Close(); } }
/// <summary> /// Adds the many to many. /// </summary> /// <param name="htmlTable">The HTML table.</param> private void AddManyToMany(HtmlTable htmlTable) { foreach(TableSchema.ManyToManyRelationship m2m in ManyToManyCollection) { TableSchema.Table mapTable = DataService.GetSchema(m2m.MapTableName, ProviderName); TableSchema.Table foreignTable = DataService.GetSchema(m2m.ForeignTableName, ProviderName); CheckBoxList chk = new CheckBoxList {ID = mapTable.ClassName}; AddRow(htmlTable, mapTable.DisplayName, chk); chk.Items.Clear(); chk.RepeatColumns = 2; bool isSortable = Utility.GetEffectiveMaxLength(foreignTable.Columns[1]) < 256; SqlQuery query = new Select(foreignTable.Provider).From(foreignTable); if(isSortable) query.OrderAsc(foreignTable.Columns[1].ColumnName); using(IDataReader rdrAllMappings = query.ExecuteReader()) { while(rdrAllMappings.Read()) chk.Items.Add(new ListItem(rdrAllMappings[1].ToString(), rdrAllMappings[0].ToString().ToLower())); rdrAllMappings.Close(); } List<string> activeIds = new List<string>(); SqlQuery queryMappings = new Select(mapTable.Provider).From(mapTable).Where(mapTable.GetColumn(m2m.MapTableLocalTableKeyColumn)).IsEqualTo(PrimaryKeyValue); using(IDataReader rdrActiveMappings = queryMappings.ExecuteReader()) { while(rdrActiveMappings.Read()) activeIds.Add(rdrActiveMappings[m2m.MapTableForeignTableKeyColumn].ToString().ToLower()); rdrActiveMappings.Close(); } foreach(string id in activeIds) { ListItem li = chk.Items.FindByValue(id); if(li != null) li.Selected = true; } } }
/// <summary> /// Special builder for many to many relational tables. /// </summary> /// <returns></returns> private HtmlTable CreateManyMapper() { HtmlTable tbl = new HtmlTable {Width = "600px"}; if(ShowScaffoldCaption) AddRow(tbl, String.Format("<h2>{0} Map</h2>", TableSchema.DisplayName), 2); foreach(TableSchema.TableColumn col in TableSchema.Columns) { //by convention, each key in the map table should be a foreignkey //if not, it's not good if(col.IsPrimaryKey) { string fkTable = col.ForeignKeyTableName; SqlQuery qry = new Select(col.Table.Provider).From(fkTable); //Query qry = new Query(DataService.GetSchema(fkTable, ProviderName, TableType.Table)); DropDownList ddl = new DropDownList {ID = col.ColumnName}; AddRow(tbl, fkTable, ddl); using(IDataReader rdr = qry.ExecuteReader()) { while(rdr.Read()) ddl.Items.Add(new ListItem(rdr[1].ToString(), rdr[0].ToString())); rdr.Close(); } } else { Control ctrl = GetEditControl(col); AddRow(tbl, Utility.ParseCamelToProper(col.ColumnName), ctrl); } } return tbl; }
/// <summary> /// Gets the edit control. /// </summary> /// <param name="tableColumn">The table column.</param> /// <returns></returns> protected Control GetEditControl(TableSchema.TableColumn tableColumn) { WebControl cOut = null; string colName = tableColumn.ColumnName.ToLower(); //use special care with the Primary Key if(tableColumn.IsPrimaryKey && FindControl(PK_ID + tableColumn.ColumnName) == null) { //don't want to edit an auto-increment if(tableColumn.AutoIncrement || tableColumn.DataType == DbType.Guid) cOut = new Label(); else cOut = new TextBox(); cOut.ID = PK_ID + tableColumn.ColumnName; } else { bool buildForeignKey = false; if(tableColumn.IsForeignKey) { DropDownList ddl = new DropDownList(); ApplyCssClass(ddl, DropDownCssClass); string fkTableName = tableColumn.ForeignKeyTableName; if(!String.IsNullOrEmpty(fkTableName)) { buildForeignKey = true; TableSchema.Table tbl = DataService.GetSchema(fkTableName, ProviderName, TableType.Table); TableSchema.TableColumn displayCol = Utility.GetDisplayTableColumn(tbl); bool isSortable = Utility.GetEffectiveMaxLength(displayCol) < 250; if(tableColumn.IsNullable) { ListItem liNull = new ListItem("(Not Specified)", String.Empty); ddl.Items.Add(liNull); } SqlQuery qry = new Select(tbl.Provider, tbl.Columns[0].ColumnName, displayCol.ColumnName).From(tbl); if(isSortable) qry.OrderAsc(displayCol.ColumnName); using(IDataReader rdr = qry.ExecuteReader()) { while(rdr.Read()) { ListItem item = new ListItem(rdr[1].ToString(), rdr[0].ToString()); ddl.Items.Add(item); } rdr.Close(); } cOut = ddl; } } if(!buildForeignKey) { switch(tableColumn.DataType) { case DbType.Guid: case DbType.AnsiString: case DbType.String: case DbType.StringFixedLength: case DbType.Xml: case DbType.Object: case DbType.AnsiStringFixedLength: if(Utility.MatchesOne(colName, ReservedColumnName.CREATED_BY, ReservedColumnName.MODIFIED_BY)) cOut = new Label(); else { TextBox t = new TextBox(); if(Utility.GetEffectiveMaxLength(tableColumn) > 250) { t.TextMode = TextBoxMode.MultiLine; t.Columns = 60; t.Rows = 4; } else { t.Width = Unit.Pixel(250); if(colName.EndsWith("guid", StringComparison.InvariantCultureIgnoreCase)) { t.Text = Guid.NewGuid().ToString(); t.Enabled = false; } } cOut = t; } break; case DbType.Binary: //do nothing break; case DbType.Boolean: CheckBox chk = new CheckBox(); ApplyCssClass(chk, CheckBoxCssClass); if(Utility.IsMatch(colName, ReservedColumnName.IS_ACTIVE)) chk.Checked = true; if(Utility.MatchesOne(colName, ReservedColumnName.DELETED, ReservedColumnName.IS_DELETED)) chk.Checked = false; cOut = chk; break; case DbType.Date: case DbType.Time: case DbType.DateTime: if(Utility.MatchesOne(colName, ReservedColumnName.MODIFIED_ON, ReservedColumnName.CREATED_ON)) cOut = new Label(); else cOut = new CalendarControl(); break; case DbType.Int16: case DbType.Int32: case DbType.UInt16: case DbType.Int64: case DbType.UInt32: case DbType.UInt64: case DbType.VarNumeric: case DbType.Single: case DbType.Currency: case DbType.Decimal: case DbType.Double: case DbType.Byte: TextBox tt = new TextBox {Width = Unit.Pixel(50)}; //if (!this.isNew) //tt.Text = this.GetColumnValue(col.ColumnName).ToString(); cOut = tt; break; default: cOut = new TextBox(); break; } } if(cOut != null) cOut.ID = tableColumn.ColumnName; } if(cOut is TextBox) { TextBox tbx = (TextBox)cOut; ApplyCssClass(tbx, TextBoxCssClass); if(cOut.GetType() == typeof(TextBox)) //Not Redundant! CalendarControl is TextBox == true; myCalendarControl.GetType() == typeof(TextBox) == false! { int maxLength = Utility.GetEffectiveMaxLength(tableColumn); if(maxLength > 0) tbx.MaxLength = maxLength; } } if(cOut != null && listReadOnlyColumns.Contains(colName)) cOut.Enabled = false; return cOut; }