Skip to content

Instantly share code, notes, and snippets.

@tdgroot
Created June 5, 2014 15:02
Show Gist options
  • Save tdgroot/c20fa6b39019401e5092 to your computer and use it in GitHub Desktop.
Save tdgroot/c20fa6b39019401e5092 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Linqerino {
public partial class Posts1 : Page {
BlogEntities blogEntities = new BlogEntities();
protected void Page_Load(object sender, EventArgs e) {
IEnumerable<Posts> posts = from p in blogEntities.Posts select p;
TableRow header = new TableRow();
header.Cells.Add(new TableCell() { Text = "ID" });
header.Cells.Add(new TableCell() { Text = "Title" });
header.Cells.Add(new TableCell() { Text = "Content" });
header.Cells.Add(new TableCell() { Text = "Poster" });
header.Cells.Add(new TableCell() { Text = "Date Created" });
tblData.Rows.Add(header);
foreach (var post in posts) {
TableRow row = new TableRow();
TableCell titleCell = new TableCell();
TextBox titleBox = new TextBox();
titleBox.Text = post.Title;
titleCell.Controls.Add(titleBox);
TableCell contentCell = new TableCell();
TextBox contentBox = new TextBox();
contentBox.Text = post.Content;
contentBox.TextMode = TextBoxMode.MultiLine;
contentCell.Controls.Add(contentBox);
TableCell posterCell = new TableCell();
TextBox posterBox = new TextBox();
posterBox.Text = post.PosterName;
posterCell.Controls.Add(posterBox);
row.Cells.Add(new TableCell() { Text = post.ID.ToString() });
row.Cells.Add(titleCell);
row.Cells.Add(contentCell);
row.Cells.Add(posterCell);
row.Cells.Add(new TableCell() { Text = post.Date });
TableCell editCell = new TableCell();
Button editButton = new Button();
editButton.Text = "Update";
editButton.Click += (o, args) => {
String title = ((TextBox) row.Cells[1].Controls[0]).Text;
String content = ((TextBox)row.Cells[2].Controls[0]).Text;
String poster = ((TextBox)row.Cells[3].Controls[0]).Text;
post.Title = title;
post.Content = content;
post.PosterName = poster;
blogEntities.SaveChanges();
Response.Redirect("Posts.aspx");
};
editCell.Controls.Add(editButton);
row.Cells.Add(editCell);
TableCell buttonCell = new TableCell();
Button button = new Button();
button.Text = "Delete";
button.Click += (o, args) => {
var postToDelete = from p in posts where p.ID == post.ID select p;
blogEntities.Posts.RemoveRange(postToDelete);
blogEntities.SaveChanges();
Response.Redirect("Posts.aspx");
};
buttonCell.Controls.Add(button);
row.Cells.Add(buttonCell);
tblData.Rows.Add(row);
}
}
protected void Button1_Click(object sender, EventArgs e) {
blogEntities.Posts.Add(new Posts() { Content = tbContent.Text, Date = GetTimestamp(DateTime.Now), PosterName = tbPoster.Text, Title = tbTitle.Text});
blogEntities.SaveChanges();
Response.Redirect("Posts.aspx");
}
public static String GetTimestamp(DateTime value) {
return value.ToString("dd-MM-yy hh:mm:ss");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment