webform 对表的增删改查

增删改查的方法代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class xiugai : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            string id = Request["code"].ToString();

        Car data = new CarBf().Select(id);

        Session["car"] = data;

        TextBox1.Text = data.Code;

        TextBox2.Text = data.Name;

        TextBox3.Text = data.Price.ToString();

        }

        

    }

    protected void Button1_Click(object sender, EventArgs e)

    {



        //在这里用session接收要修改的数据,不能再这里new一个新对象

        Car data = (Car)Session["car"];

        data.Code = TextBox1.Text;

        data.Name = TextBox2.Text;

        data.Price = Convert.ToDecimal(TextBox3.Text);



        new CarBf().Update(data);

        Response.Redirect("Car.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 Car1 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        List<Car> list = new CarBf().Select();



        string s = " <table width=' 100%'  border='1'><tr class='tr1'><td>编号</td><td>名称</td><td>价格</td><td>操作</td></tr>";

        foreach (Car data in list)

        {

            s+=" <tr class='tr2'><td>"+data.Code+"</td><td><a href='xianshi.aspx?code="+data.Code+"'>"+data.Name+"</a></td><td>"+data.Price+ "</td><td><a href='Delete.aspx?code=" + data.Code + "'>删除</a><a href='Update.aspx?code=" + data.Code + "'>修改</a></td></tr>";

        }

        s+="</table><a href='Insert.aspx'>添加<a/>";

        Literal1.Text=s;

    }

}


修改代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class xiugai : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            string id = Request["code"].ToString();

        Car data = new CarBf().Select(id);

        Session["car"] = data;

        TextBox1.Text = data.Code;

        TextBox2.Text = data.Name;

        TextBox3.Text = data.Price.ToString();

        }

        

    }

    protected void Button1_Click(object sender, EventArgs e)

    {



        //在这里用session接收要修改的数据,不能再这里new一个新对象

        Car data =new Car();

        data.Code = TextBox1.Text;

        TextBox1.Enabled = false;

        data.Name = TextBox2.Text;

        data.Price = Convert.ToDecimal(TextBox3.Text);



        new CarBf().Update(data);

        Response.Redirect("Car.aspx");



    }

}

 

你可能感兴趣的:(webform)