protected void ButtonCreateMessage_Click(object sender, EventArgs e) { try { int channelId = Convert.ToInt32(this.DropDownListChannels.SelectedValue); TwitterEntities context = new TwitterEntities(); Message msg = new Message(); Channel channel = context.Channels.Find(channelId); msg.Channel = channel; string msgText = MessageContent.Text; Verificator.ValidateMessage(msgText); msg.MessageContent = msgText; var currentUserName = this.User.Identity.Name; var author = context.AspNetUsers.FirstOrDefault(x => x.UserName == currentUserName); msg.AspNetUser = author; msg.Date = DateTime.Now; context.Messages.Add(msg); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Message created successfully."); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("ChannelMessages.aspx?channelId=" + channelId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void HyperLinkAddMessage_Click(object sender, EventArgs e) { try { int channelId = Convert.ToInt32(Request.Params["channelId"]); TwitterEntities context = new TwitterEntities(); string currentUserName = User.Identity.Name; LinkButton button = sender as LinkButton; var tr = button.Parent.Parent; var trControls = tr.Controls; string messageContent = null; foreach (Control tdControl in trControls) { foreach (Control control in tdControl.Controls) { if (control.ID == "MessageContent") { messageContent = (control as TextBox).Text; } } } Verificator.ValidateMessage(messageContent); string userId = context.AspNetUsers.FirstOrDefault(x => x.UserName == currentUserName).Id; Message message = new Message() { ChannelId = channelId, Date = DateTime.Now, MessageContent = messageContent, UserId = userId }; context.Messages.Add(message); context.SaveChanges(); this.GridViewMessages.DataBind(); this.InfoHolder.Visible = false; //ErrorSuccessNotifier.ShowAfterRedirect = false; //ErrorSuccessNotifier.AddSuccessMessage("Message created successfully."); } catch (Exception ex) { this.InfoHolder.InnerText = ex.Message; this.InfoHolder.Attributes.Add("class", "alert alert-error"); this.InfoHolder.Visible = true; //ErrorSuccessNotifier.ShowAfterRedirect = false; //ErrorSuccessNotifier.AddErrorMessage(ex); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewMyMessages_UpdateItem(int MessageId) { TwitterEntities context = new TwitterEntities(); Twitter.Models.Message item = null; // Load the item here, e.g. item = MyDataLayer.Find(id); item = context.Messages.FirstOrDefault(x => x.MessageId == MessageId); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", MessageId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); try { Verificator.ValidateMessage(item.MessageContent); context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Message has been edited successfully"); } catch (Exception ex) { if (ex.Message != null) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } else { ErrorSuccessNotifier.AddErrorMessage(ex); } } } }