protected void LinkButtonUpdate_Command(object sender, CommandEventArgs e) { try { int channelId = Convert.ToInt32(e.CommandArgument); TwitterEntities context = new TwitterEntities(); var channel = context.Channels.FirstOrDefault(x => x.ChannelId == channelId); var button = sender as LinkButton; var tr = button.Parent.Parent; var textBox = tr.FindControl("TextBoxEditChannel") as TextBox; string newChannelName = textBox.Text; Verificator.ValidateChannel(newChannelName); channel.Name = newChannelName; context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Channel name updated successfully."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void ButtonCreateChannel_Click(object sender, EventArgs e) { try { TwitterEntities context = new TwitterEntities(); Channel channel = new Channel(); var currentUserName = this.User.Identity.Name; var author = context.AspNetUsers.FirstOrDefault(x => x.UserName == currentUserName); Verificator.ValidateChannel(ChannelName.Text); channel.AspNetUser = author; channel.Name = ChannelName.Text; context.Channels.Add(channel); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Channel created successfully."); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("../Messages/ChannelMessages.aspx?channelId=" + channel.ChannelId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewMyChannels_UpdateItem(int ChannelId) { TwitterEntities context = new TwitterEntities(); Twitter.Models.Channel item = null; // Load the item here, e.g. item = MyDataLayer.Find(id); item = context.Channels.Find(ChannelId); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", ChannelId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { try { Verificator.ValidateChannel(item.Name); context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Channel updated successfully"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } // Save changes here, e.g. MyDataLayer.SaveChanges(); } }