以Car表增删改为例
Car.aspx
<body> <form id="form1" runat="server"> <div> <asp:Literal ID="LiteralCar" runat="server"></asp:Literal> </div> </form> </body>
Car.aspx.cs
protected void Page_Load(object sender, EventArgs e) { List<Car> list = new CarBF().Select(); string s = "<center><table width=‘40%‘ border=‘1‘><tr><td>编号</td><td>名称</td><td>价格</td><td>操作</td></tr>"; foreach (Car data in list) { s += "<tr><td>"+data.Code+"</td><td><a href=‘XiangXi.aspx?Code= "+ data.Code+"‘>"+data.Name+"</a></td><td>"+data.Price+"</td><td><a href=‘Delete.aspx?code="+data.Code+"‘>删除</a></td></tr>"; } s += "</table></center>"; LiteralCar.Text = s; }
<a href=‘XiangXi.aspx?Code= "+ data.Code+"‘>"+data.Name+"</a> xxx.aspx?xx=+值 在对应页面Request["xx"].ToString()接收
XiangXi.aspx
<body> <form id="form1" runat="server"> <div> <center> <table> <tr> <td> 编号:<asp:TextBox ID="txtcode" runat="server"></asp:TextBox></td> </tr> <tr><td>名称:<asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr> <tr><td>车系:<asp:TextBox ID="txtbrand" runat="server"></asp:TextBox></td></tr> <tr><td>日期:<asp:TextBox ID="txttime" runat="server"></asp:TextBox></td></tr> <tr><td>油耗:<asp:TextBox ID="txtoil" runat="server"></asp:TextBox></td></tr> <tr> <td>马力:<asp:TextBox ID="txtpowers" runat="server"></asp:TextBox></td></tr> <tr><td>排量:<asp:TextBox ID="txtexhaust" runat="server"></asp:TextBox></td></tr> <tr> <td>单价:<asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr> <tr><td>图片:<asp:TextBox ID="txtpic" runat="server"></asp:TextBox></td></tr> <tr><td> <asp:Button ID="btInsert" runat="server" Text="增加" OnClick="btInsert_Click"></asp:Button> <asp:Button ID="btUpdate" runat="server" Text="修改" OnClick="btUpdate_Click"></asp:Button> </td></tr> </table> </center> </div> </form> </body>
XiangXi.aspx.cs
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string code = Request["Code"].ToString(); Car car = new CarBF().Select(code); Session["Car"] = car; txtcode.Text = car.Code; txtname.Text = car.Name; txtbrand.Text = car.Brand; txttime.Text = car.Time.ToString(); txtoil.Text = car.Oil.ToString(); txtpowers.Text = car.Powers.ToString(); txtexhaust.Text = car.Exhaust.ToString(); txtprice.Text = car.Price.ToString(); txtpic.Text = car.Pic; } } protected void btInsert_Click(object sender, EventArgs e) { Response.Redirect("Insert.aspx"); } protected void btUpdate_Click(object sender, EventArgs e) { Car c = (Car)Session["Car"]; c.Name = txtname.Text; new CarBF().Update(c); Response.Redirect("Cars.aspx"); }
CarBF
private MyDbDataContext Context; public CarBF() { Context = new MyDbDataContext(); } public List<Car> Select() { return Context.Car.ToList(); } public Car Select(string code) { Car query=Context.Car.Single(p=>p.Code==code.Trim()); return query; } public void Insert(Car c) { Context.Car.InsertOnSubmit(c); Context.SubmitChanges(); } public void Update(Car c) { Car car = Context.Car.Single(p=>p.Code==c.Code); car.Name = c.Name; car.Exhaust = c.Exhaust; car.Oil = c.Oil; car.Pic = c.Pic; car.Powers = c.Powers; car.Price = c.Price; car.Time = c.Time; Context.SubmitChanges(); } public void Delete(string code) { Car c = Context.Car.Single(p=>p.Code==code); Context.Car.DeleteOnSubmit(c); Context.SubmitChanges(); }
时间: 2024-10-27 23:47:56