void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) { MetaTable childTable = ChildrenColumn.ChildTable; if (Mode == DataBoundControlMode.Edit) { ObjectContext.LoadProperty(e.Entity, Column.Name); } dynamic entityCollection = Column.EntityTypeProperty.GetValue(e.Entity, null); foreach (dynamic childEntity in childTable.GetQuery(e.Context)) { var isCurrentlyInList = ListContainsEntity(childTable, entityCollection, childEntity); string pkString = childTable.GetPrimaryKeyString(childEntity); ListItem listItem = CheckBoxList1.Items.FindByValue(pkString); if (listItem == null) continue; if (listItem.Selected) { if (!isCurrentlyInList) entityCollection.Add(childEntity); } else { if (isCurrentlyInList) entityCollection.Remove(childEntity); } } }
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e) { int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId")); Picture myPicture = (Picture)e.Entity; myPicture.PhotoAlbumId = photoAlbumId; FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1"); string virtualFolder = "~/GigPics/"; string physicalFolder = Server.MapPath(virtualFolder); string fileName = Guid.NewGuid().ToString(); string extension = System.IO.Path.GetExtension(FileUpload1.FileName); FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension)); myPicture.ImageUrl = virtualFolder + fileName + extension; }
protected void EntityDataSource1_Deleting(object sender, EntityDataSourceChangingEventArgs e) { Courses classtodel = (Courses)e.Entity; classtodel.SetCourses.Load(); int i = (from sc in classtodel.SetCourses select sc).Count(); if (classtodel.SetCourses.Count() != 0) { e.Cancel = true; Exception ex = new Exception(); ex.Data["DeleteError"] = "此班级还有学生,不能删除"; throw ex; } }
protected void EntityDataSource1_Deleting(object sender, EntityDataSourceChangingEventArgs e) { Classes classtodel = (Classes)e.Entity; classtodel.SetCourses.Load(); classtodel.Students.Load(); int i = (from sc in classtodel.SetCourses select sc).Count(); int j = (from s in classtodel.Students select s).Count(); if (classtodel.SetCourses.Count() != 0 || classtodel.Students.Count() != 0) { e.Cancel = true; Response.Write(@"<script>alert("+"此班级还有学生,不能删除!');</script>"); } }
void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) { MetaTable childTable = ChildrenColumn.ChildTable; // Comments assume employee/territory for illustration, but the code is generic // Get the collection of territories for this employee RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null); // In Edit mode, make sure it's loaded (doesn't make sense in Insert mode) if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded) { entityCollection.Load(); } // Get an IList from it (i.e. the list of territories for the current employee) // REVIEW: we should be using EntityCollection directly, but EF doesn't have a // non generic type for it. They will add this in vnext IList entityList = ((IListSource)entityCollection).GetList(); // Go through all the territories (not just those for this employee) foreach (object childEntity in childTable.GetQuery(e.Context)) { // Check if the employee currently has this territory bool isCurrentlyInList = entityList.Contains(childEntity); // Find the checkbox for this territory, which gives us the new state string pkString = childTable.GetPrimaryKeyString(childEntity); ListItem listItem = CheckBoxList1.Items.FindByValue(pkString); if (listItem == null) continue; // If the states differs, make the appropriate add/remove change if (listItem.Selected) { if (!isCurrentlyInList) entityList.Add(childEntity); } else { if (isCurrentlyInList) entityList.Remove(childEntity); } } }
void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) { MetaTable childTable = ChildrenColumn.ChildTable; // En los comentarios se asume un empleado o territorio a modo de ejemplo, pero el código es genérico // Obtener la colección de territorios de este empleado RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null); // En el modo de edición, asegurarse de que se ha cargado (no tiene sentido hacerlo en el modo de inserción) if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded) { entityCollection.Load(); } // Obtener una IList de la colección (es decir, la lista de territorios del empleado actual) // REVISIÓN: debemos usar EntityCollection directamente, pero EF no tiene un // tipo que no sea genérico para esta colección. Se agregará en la siguiente versión IList entityList = ((IListSource)entityCollection).GetList(); // Recorrer todos los territorios (no solo los de este empleado) foreach (object childEntity in childTable.GetQuery(e.Context)) { // Comprobar si el empleado tiene actualmente este territorio bool isCurrentlyInList = entityList.Contains(childEntity); // Buscar la casilla para este territorio, que nos indica el nuevo estado string pkString = childTable.GetPrimaryKeyString(childEntity); ListItem listItem = CheckBoxList1.Items.FindByValue(pkString); if (listItem == null) continue; // Si el estado es diferente, agregar o quitar según corresponda if (listItem.Selected) { if (!isCurrentlyInList) entityList.Add(childEntity); } else { if (isCurrentlyInList) entityList.Remove(childEntity); } } }
private void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) { MetaTable childTable = ChildrenColumn.ChildTable; // Comments assume employee/territory for illustration, but the code is generic if (Mode == DataBoundControlMode.Edit) { ObjectContext.LoadProperty(e.Entity, Column.Name); } // Get the collection of territories for this employee dynamic entityList = Column.EntityTypeProperty.GetValue(e.Entity, null); // Go through all the territories (not just those for this employee) foreach (dynamic childEntity in childTable.GetQuery(e.Context)) { // Check if the employee currently has this territory bool isCurrentlyInList = ListContainsEntity(childTable, entityList, childEntity); // Find the checkbox for this territory, which gives us the new state string pkString = childTable.GetPrimaryKeyString(childEntity); ListItem listItem = CheckBoxList1.Items.FindByValue(pkString); if (listItem == null) { continue; } // If the states differs, make the appropriate add/remove change if (listItem.Selected) { if (!isCurrentlyInList) { entityList.Add(childEntity); } } else { if (isCurrentlyInList) { entityList.Remove(childEntity); } } } }
// Method that inserts a new user into the database protected void EntityDataSourceGeneral_Inserting(object sender, EntityDataSourceChangingEventArgs e) { // Hides the error-layer msgDv.Visible = false; int errCnter = 0; // Checks if the username exists before in the database. If it does - then save the // number of 1 to the errCnter variable above TextBox usern = dbListView.InsertItem.FindControl("usrs_usrsnmeTextBox") as TextBox; using (MindDumpsEntities db = new MindDumpsEntities()) { var query = from user in db.minddumps_usrs where user.mnd_usrs_usrsnme == usern.Text.Trim() select user.mnd_usrs_usrsnme; if (query.Count() > 0) { errCnter = 1; } } // If the username not exists in the database - add it ... if (errCnter == 0) { HashPswdGeneratorHandler hpg = new HashPswdGeneratorHandler(); string nwSalt1 = hpg.GenerateSalt(); string nwSalt2 = hpg.GenerateSalt(); minddumps_usrs dbUsrs = e.Entity as minddumps_usrs; dbUsrs.mnd_usrs_usrssaltfrst = nwSalt1.ToString(); dbUsrs.mnd_usrs_usrssaltlst = nwSalt2.ToString(); TextBox pswrdet = dbListView.InsertItem.FindControl("usrs_usrspswdTextBox") as TextBox; dbUsrs.mnd_usrs_usrspswd = hpg.GetHashedPassword(nwSalt1, pswrdet.Text.Trim(), nwSalt2); } // ... or show an error message if it does else { e.Cancel = true; msgDv.Visible = true; msgDv.Attributes.Add("class", "errorMsg"); msgs.Attributes.Add("class", "errorMsg"); msgs.Attributes.Add("class", "whiteText"); string errTxt = "User could not be added because the username is already taken!"; msgs.Text = errTxt; ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "alert('" + errTxt + "');", true); } }
private void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) { MetaTable childTable = ChildrenColumn.ChildTable; // Les commentaires supposent employé/territoire pour l'illustration, mais le code est générique // Obtient la collection de territoires pour cet employé var entityCollection = (RelatedEnd) Column.EntityTypeProperty.GetValue(e.Entity, null); // En mode Edition, vérifie qu'il est chargé (n'a pas de sens en mode Insertion) if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded) { entityCollection.Load(); } // Obtient un IList (c-à-d. la liste des territoires pour l'employé actuel) // RÉVISION : il est conseillé d'utiliser EntityCollection directement, mais EF n'a pas de // type non générique pour lui. Cela sera ajouté dans vnext IList entityList = ((IListSource) entityCollection).GetList(); // Parcourt tous les territoires (pas uniquement pour cet employé) foreach (object childEntity in childTable.GetQuery(e.Context)) { // Vérifie si l'employé a actuellement ce territoire bool isCurrentlyInList = entityList.Contains(childEntity); // Trouve la case à cocher pour ce territoire, ce qui donne le nouvel état string pkString = childTable.GetPrimaryKeyString(childEntity); ListItem listItem = CheckBoxList1.Items.FindByValue(pkString); if (listItem == null) continue; // Si les états diffèrent, apporte les modifications appropriées, ajout ou suppression if (listItem.Selected) { if (!isCurrentlyInList) entityList.Add(childEntity); } else { if (isCurrentlyInList) entityList.Remove(childEntity); } } }
protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e) { PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity; myPhotoAlbum.UserName = User.Identity.Name; }
protected void EntityDataSourceTowns_Inserting(object sender, EntityDataSourceChangingEventArgs e) { Town newTown = (Town)e.Entity; int countryId = Convert.ToInt32(this.gvCountries.SelectedDataKey.Value); newTown.CountryId = countryId; }
// Method that add's the new item (in the category) to the user that are currently logged in protected void EntityDataSourceGeneral_Inserting(object sender, EntityDataSourceChangingEventArgs e) { arcencase_itms itmIns = e.Entity as arcencase_itms; itmIns.aenc_itms_itmsusrsid = int.Parse(Session["usrnid"].ToString()); }
// Method that updates things about a specific user's credentials etc protected void EntityDataSourceGeneral_Updating(object sender, EntityDataSourceChangingEventArgs e) { // Hides the error-layer msgDv.Visible = false; int errCnter = 0; // Checks if the username exists before in the database. If it does - then save the // number of 1 to the errCnter variable above HiddenField oldUsr = dbListView.EditItem.FindControl("oldUsernme") as HiddenField; TextBox usern = dbListView.EditItem.FindControl("usrs_usrsnmeTextBox") as TextBox; using (ArcEncaseEntities db = new ArcEncaseEntities()) { var query = from user in db.arcencase_usrs where user.aenc_usrs_usrsnme == usern.Text.Trim() select user.aenc_usrs_usrsnme; if (query.Count() > 0) { errCnter = 1; } } arcencase_usrs dbUsrs = e.Entity as arcencase_usrs; // If the username exists in the database - don't add it ... if (errCnter == 1 && (oldUsr.Value.Trim() != usern.Text.Trim())) { e.Cancel = true; msgDv.Visible = true; msgDv.Attributes.Add("class", "errorMsg"); msgs.Attributes.Add("class", "errorMsg"); msgs.Attributes.Add("class", "whiteText"); string errTxt = "The username could not be changed because the new one you choose is already taken!"; msgs.Text = errTxt; ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "alert('" + errTxt + "');", true); } // ... else - add it else { msgs.Text = ""; msgDv.Visible = false; dbUsrs.aenc_usrs_usrsnme = usern.Text.Trim(); } // Update the password (+salts) for the specific user HashPswdGeneratorHandler hpg = new HashPswdGeneratorHandler(); string nwSalt1 = hpg.GenerateSalt(); string nwSalt2 = hpg.GenerateSalt(); dbUsrs.aenc_usrs_usrssaltfrst = nwSalt1.ToString(); dbUsrs.aenc_usrs_usrssaltlst = nwSalt2.ToString(); TextBox pswrdet = dbListView.EditItem.FindControl("usrs_usrspswdTextBox") as TextBox; dbUsrs.aenc_usrs_usrspswd = hpg.GetHashedPassword(nwSalt1, pswrdet.Text.Trim(), nwSalt2); }