温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Linq三层模式之增删改源码
当前文件:
LinqDemo/Default.aspx.cs,打开代码结构图
LinqDemo/Default.aspx.cs,打开代码结构图
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//Download from 51aspx.com/cv/LinqDemo
public partial class _Default : System.Web.UI.Page
{
StaffBLL sb = new StaffBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bindData();
}
}
private void bindData()
{
//StaffBLL sb = new StaffBLL();
this.GridView1.DataSource = sb.LinqStaff();
this.GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
TextBox staffTb = this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox;
TextBox ageTb = this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
string staffName = staffTb.Text;
int age = Convert.ToInt32(ageTb.Text);
sb.LinqUpdateStaff(id, staffName, age);
this.GridView1.EditIndex = -1;
bindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bindData();
}
protected void Button1_Click(object sender, EventArgs e)
{
sb.LinqInsertStaff(this.TextBox1.Text,Convert.ToInt32(this.TextBox2.Text));
bindData();
this.TextBox1.Text = "";
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
sb.LinqDeleteStaff(id);
bindData();
}
}

