Webfrom --超連接傳值

效果圖:數據庫

(一)鏈接Car表  添加一個數據庫方法(myDB)spa

namespace 鏈接數據庫_查表
{
    
    public class myDB
    {
        private MYDBDataContext context = new MYDBDataContext();
        public List<Car> Select()//將表所有查詢 { return context.Car.ToList(); } public Car Select1(string code)//挨個查詢
        {
            var query = context.Car.Where(p => p.Code == code);
            if (query.Count() > 0)
            {
                return query.First();
            }
            return null;

        }
        public bool Update(Car c)//修改 { var query = context.Car.Where(p => p.Code== c.Code); if (query.Count()>0) { Car a = query.First(); a.Name = c.Name; a.Brand = c.Brand; context.SubmitChanges(); return true; } else { return false; } } public void Delete(string code)//刪除
        {
            var query = context.Car.Where(p => p.Code == code);
            if (query.Count() > 0)
            {
                Car stu = query.First();
                context.Car.DeleteOnSubmit(stu);
                context.SubmitChanges();

            }

        }
        public bool All(Car a)//增長 { try { context.Car.InsertOnSubmit(a); context.SubmitChanges(); return true; } catch { return false; } }

     
    }
}

效果圖:3d

(二) 添加一個WEb窗口(che.aspx)code

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 鏈接數據庫_查表
{
    public partial class che : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)//把表裏全部的東西都顯示出來 { List<Car> list = new myDB().Select(); string s = "<table width='100%' border=1 ><tr><td>編號</td><td>名字</td><td>類型</td><td>刪除</td><td>增長</td></tr>"; foreach (Car date in list) { s += "<tr><td>" + date.Brand + "</td><td><a href='Update.aspx?code=" + date.Code + "'>" + date.Name + "</a></td><td>" + date.Brand + "</td><td><a href='delete.aspx?code=" + date.Code + "'>刪除</a></td><td><a href='All.aspx'>增長</a></td></tr>"; } s += "</table>"; Literal1.Text = s; }
    }
}

(三)添加一個增長的Web窗體(All.aspx) server

<視圖>blog

<div>
    
        <asp:Label ID="Label1" runat="server" Text="編號"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="姓名"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="類型"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="確認增長" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主頁" />

    </div>

<代碼>ip

namespace 鏈接數據庫_查表
{
    public partial class All : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)//增長
        {
            Car a = new Car();
            a.Code = TextBox1.Text;
            a.Name = TextBox2.Text;
            new myDB().All(a);
            Response.Redirect("che.aspx");


        }

        protected void Button2_Click(object sender, EventArgs e)//返回
        {
            Response.Redirect("che.aspx");//轉到主頁的界面
        }
    }
}

(四)刪除(delete.aspx)string

添加一個delete Webfrom窗口it

namespace 鏈接數據庫_查表
{
    public partial class delete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new myDB().Delete(Request["code"].ToString());
            Response.Redirect("che.aspx");
        }
    }
}

效果圖:io

(五) 修改(update.aspx)

<視圖>

   <div>

    
        <asp:Label ID="Label1" runat="server" Text="編號"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="姓名"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="類型"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="確認修改" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主頁" />

    
    </div>

<代碼>

namespace 鏈接數據庫_查表
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string s = Request["code"].ToString() ;
           
            Car a = new myDB().Select1(s);
            Session["car"] = a;
            TextBox1.Text = a.Code;
            TextBox2.Text = a.Name;
            TextBox3.Text = a.Brand;
            }
           
        }

        protected void Button1_Click(object sender, EventArgs e)//修改
        {
            Car c = (Car)Session["car"];
            c.Name = TextBox2.Text;
            c.Brand = TextBox3.Text;
           bool isok= new myDB().Update(c);
             if (isok)
             {
                 Response.Write("<script> alert('修改爲功')</script>");
                Response.Redirect("che.aspx");//轉到主頁的界面
             }
             else
             {
                 Response.Write("<script> alert('修改失敗')</script>");
             }
        }

        protected void Button2_Click(object sender, EventArgs e)//返回主頁
        {
                Response.Redirect("che.aspx");//轉到主頁的界面
        }
    }
}

 

相關文章
相關標籤/搜索