protected void Page_Load(object sender, EventArgs e)
{
LoadGrid();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["Id"] = GridView1.SelectedDataKey["Id"];
TextBox1.Text = GridView1.SelectedRow.Cells[2].Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
if (ViewState["Id"] == null)
{//Save
Temp t = new Temp();
t.Name = TextBox1.Text;
db.Temps.InsertOnSubmit(t);
db.SubmitChanges();
TextBox1.Text = "";
}
else
{
//Update
Temp t = db.Temps.Single(b=>b.Id.Equals(ViewState["Id"]));
t.Name=TextBox1.Text;
db.SubmitChanges();
TextBox1.Text = "";
}
LoadGrid();
}
public void LoadGrid()
{
DataClassesDataContext db = new DataClassesDataContext();
var p = from b in db.Temps
select new { b.Id, b.Name };
GridView1.DataSource = p;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
string i = GridView1.SelectedRow.Cells[1].Text;
Temp t = db.Temps.Single(b => b.Id.Equals(Convert.ToInt32(i)));
db.Temps.DeleteOnSubmit(t);
db.SubmitChanges();
LoadGrid();
}
No comments:
Post a Comment