protected void Page_PreRender(object sender, EventArgs e) { ChatContext db = new ChatContext(); ChatListView.DataSource = db.Messages.ToList(); ChatListView.DataBind(); if (ViewState["username"] != null) { ((TextBox)Page.FindControl("tb_Username")).Text = (string)ViewState["username"]; } ((TextBox)Page.FindControl("tb_Message")).Text = ""; }
protected void InsertButton_Command(object sender, CommandEventArgs e) { var tbUsername = ((TextBox)Page.FindControl("tb_Username")); if(string.IsNullOrWhiteSpace(tbUsername.Text)){ return ; } ViewState["username"] = tbUsername.Text; string username = tbUsername.Text; string text = ((TextBox)Page.FindControl("tb_Message")).Text; ChatContext db = new ChatContext(); db.Messages.Add(new Message() { Username = username, Text = text }); db.SaveChanges(); }