//change cells in row to textboxes so user can edit fields and save change to database then update contactsGridview protected void contactGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { //get AddressID of selected row int id = (int)contactGridView.DataKeys[e.RowIndex].Value; //get row object GridViewRow editingRow = contactGridView.Rows[e.RowIndex]; TextBox firstNameTextBox = (TextBox)editingRow.Cells[2].Controls[0]; //convert FirstName cell to textbox object TextBox lastNameTextBox = (TextBox)editingRow.Cells[3].Controls[0]; //convert LastName cell to textbox object TextBox phoneTextBox = (TextBox)editingRow.Cells[4].Controls[0]; //convert Phone cell to textbox object TextBox emailTextBox = (TextBox)editingRow.Cells[5].Controls[0]; //convert Email cell to textbox object contactGridView.EditIndex = -1; //set edit index to default value //get contact that has the same AddressID of the selected row Models.Address updatedAddress = (from contact in dbContext.Addresses where contact.AddressID == id select contact).Single(); //if textboxes aren't null assign contact fields to values of textboxes if (firstNameTextBox.Text != String.Empty) { updatedAddress.FirstName = firstNameTextBox.Text; } if (lastNameTextBox.Text != String.Empty) { updatedAddress.LastName = lastNameTextBox.Text; } if (phoneTextBox.Text != String.Empty) { updatedAddress.Phone = phoneTextBox.Text; } if (emailTextBox.Text != String.Empty) { updatedAddress.Email = emailTextBox.Text; } dbContext.SaveChanges(); //save changes to database LoadAllContacts(); //update contactsGridView }
protected void submitButton_Click(object sender, EventArgs e) { //validate server controls Validate(); //if page is valid create new Address object and assign text values from text boxes to Address object properites if (IsValid) { //create new Address object Models.Address address = new Models.Address(); address.FirstName = firstNameTextBox.Text; address.LastName = lastNameTextBox.Text; address.Phone = phoneTextBox.Text; address.Email = emailTextBox.Text; dbContext.Addresses.Add(address); //add new row to database dbContext.SaveChanges(); //save changeds to database Response.Redirect("AddressBook.aspx"); //redirect to AddressBook page } }
protected void submitButton_Click(object sender, EventArgs e) { //validate server controls Validate(); //if page is valid create new Address object and assign text values from text boxes to Address object properites if (IsValid) { //create new Address object Models.Address address = new Models.Address(); address.FirstName = firstNameTextBox.Text; address.LastName = lastNameTextBox.Text; address.Phone = phoneTextBox.Text; address.Email = emailTextBox.Text; dbContext.Addresses.Add(address); //add new row to database dbContext.SaveChanges();//save changeds to database Response.Redirect("AddressBook.aspx"); //redirect to AddressBook page } }
//delete a contact from gridview protected void contactGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get AddressID for selected row int id = (int)contactGridView.DataKeys[e.RowIndex].Value; //get Address object from selected from selected row Models.Address address = (from a in dbContext.Addresses where a.AddressID == id select a).First(); //delete contact from gridview and database dbContext.Addresses.Remove(address); //save changes to database dbContext.SaveChanges(); //set EditIndext to default value contactGridView.EditIndex = -1; //show updated contactGridView LoadAllContacts(); }