最基本的,拼接字符串在Literal里面显示表,IsPostBack,增删改查基本,?传值 Request接收
LinQ to SQL类
在Default主页里面拖入Literal控件再加入一个按钮,后台写代码
函数类:
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// CarDA 的摘要说明 /// </summary> public class CarDA { private DataClassesDataContext context; public CarDA() { context = new DataClassesDataContext(); } public List<Car> selec() { return context.Car.ToList(); } public Car select(string code) { var query = context.Car.Where(p => p.Code == code); return query.First(); } public void delete(string code) { var query = context.Car.Where(p => p.Code == code); if (query.Count() > 0) { Car data = query.First(); context.Car.DeleteOnSubmit(data); context.SubmitChanges(); } } public void insert(Car data) { Productor prod = new Productor(); Brand brand = new Brand(); brand.Brand_Code=data.Brand; brand.Brand_Name=data.Name; brand.Prod_Code=data.Code; brand.Brand_Memo=data.Name; prod.Prod_Code = brand.Prod_Code; prod.Prod_Name = brand.Brand_Name; context.Productor.InsertOnSubmit(prod); context.Brand.InsertOnSubmit(brand); context.Car.InsertOnSubmit(data); context.SubmitChanges(); } public void Update(Car save)//string code, string name,string brand, DateTime time,decimal oil,int powers,int exhaust,decimal price,string pic { var query = context.Car.Where(p => p.Code == save.Code); if (query.Count() > 0) { Car data = query.First(); data.Code = save.Code; data.Name = save.Name; data.Brand =save.Brand; data.Time = save.Time; data.Oil =save.Oil; data.Powers = save.Powers; data.Exhaust = save.Exhaust; data.Price = save.Price; data.Pic = save.Pic; context.SubmitChanges(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { List<Car> list =new CarDA().selec(); string s = "<table width=100% border=1><tr><td>编号</td><td>名称</td><td>价格</td><td>操作</td></ tr>";//</ table>"; foreach (Car data in list) { s += "<tr><td>" + data.Code + " </td><td><a href='Default2.aspx?code=" + data.Code + "'>" + data.Name + "</a></td><td>" + data.Price + "</td><td><a href='delete.aspx?code=" + data.Code + "'>删除</a> <a href='Default2.aspx?code=" + data.Code + "'>修改</a></td></tr>"; } s += "</ table>"; Literal1.Text = s; } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("insert.aspx"); } }
____________________________________________________我是华丽的分割线____________________________________________________
点击添加按钮 添加:
因为这里牵扯到主外键关系,先要加主键才能建外键,所以为了能运行 用了点手段, 点击按钮触发 Response.Redirect("insert.aspx");转到 insert页面
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class insert : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Car data = new Car(); data.Code = TextBox2.Text; data.Name=TextBox3.Text; data.Brand=TextBox4.Text; data.Time = Convert.ToDateTime(TextBox5.Text); data.Oil= Convert.ToDecimal(TextBox6.Text); data.Powers = Convert.ToInt32(TextBox7.Text); data.Exhaust = Convert.ToInt32(TextBox8.Text); data.Price = Convert.ToDecimal(TextBox9.Text); data.Pic = TextBox10.Text; new CarDA().insert(data); Response.Redirect("Default.aspx"); } }
点击insert页面中的提交 执行完添加函数后会转入Default.aspx主页面 Response.Redirect("Default.aspx");
____________________________________________________我是华丽的分割线____________________________________________________
删除:
每行后面有个删除按钮,超链接点击后导入删除界面,执行删除跳回主页面 ?code把 code=后面的值传走
在指定页面Request["code"]接收,Request["code"]是Object类,如果要用请先转换类型
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class delete : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { new CarDA().delete(Request["code"].ToString()); Response.Redirect("Default.aspx"); } }
____________________________________________________我是华丽的分割线____________________________________________________
修改:
修改与添加类似,需要传值,根据传入的值查询具体类,修改后保存
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Car data = new CarDA().select(Request["code"].ToString()); TextBox2.Text = data.Code; TextBox3.Text = data.Name; TextBox4.Text = data.Brand.ToString(); TextBox5.Text = data.Time.ToString(); TextBox6.Text = data.Oil.ToString(); TextBox7.Text = data.Powers.ToString(); TextBox8.Text = data.Exhaust.ToString(); TextBox9.Text = data.Price.ToString(); TextBox10.Text = data.Pic; } } protected void Button1_Click(object sender, EventArgs e) { Car data = new Car(); data.Code =TextBox2.Text; data.Name =TextBox3.Text ; data.Brand = TextBox4.Text; data.Time =Convert.ToDateTime(TextBox5.Text) ; data.Oil = Convert.ToDecimal(TextBox6.Text); data.Powers =Convert.ToInt32( TextBox7.Text) ; data.Exhaust = Convert.ToInt32(TextBox8.Text); data.Price = Convert.ToDecimal(TextBox9.Text); data.Pic = TextBox10.Text; new CarDA().Update(data); Response.Redirect("Default.aspx"); } }
这里要注意,查询要加入IsPostBack条件判定,否则点击提交按钮后会刷新页面,我们修改的数据会随刷新页面覆盖,刷新后重新执行查询并覆盖我们之前输入的数据,这个数据虽然在页面上没有显示更改,但是在后台已经被查出来的数据覆盖,所以我们无论怎么修改都是存的查出来的数据,相当于没改.
为了避免这种情况我们用IsPostBack,他会比对如果重复刷新了就不执行查询了.