本文介紹透過 Scaffolding 快速建立 增刪改查(CRUD) 的 Web Forms 程式
ASP.NET MVC 有 Scaffolding 可以快速建立 增刪改查(CRUD) 的程式,而 Web Forms 也可以哦!
以下筆者將一步步介紹如果透過 Scaffolding 快速建立 CRUD 的 Web Forms 程式。
我們拿 Microsoft 的範例資料表 HR.Employees ,Schema 如下,
CREATE SCHEMA HR AUTHORIZATION dbo;
GO
-- Create table HR.Employees
CREATE TABLE HR.Employees
(
empid INT NOT NULL IDENTITY,
lastname NVARCHAR(20) NOT NULL,
firstname NVARCHAR(10) NOT NULL,
title NVARCHAR(30) NOT NULL,
titleofcourtesy NVARCHAR(25) NOT NULL,
birthdate DATETIME NOT NULL,
hiredate DATETIME NOT NULL,
address NVARCHAR(60) NOT NULL,
city NVARCHAR(15) NOT NULL,
region NVARCHAR(15) NULL,
postalcode NVARCHAR(10) NULL,
country NVARCHAR(15) NOT NULL,
phone NVARCHAR(24) NOT NULL,
mgrid INT NULL,
CONSTRAINT PK_Employees PRIMARY KEY(empid),
CONSTRAINT FK_Employees_Employees FOREIGN KEY(mgrid)
REFERENCES HR.Employees(empid),
CONSTRAINT CHK_birthdate CHECK(birthdate <= CURRENT_TIMESTAMP)
);
CREATE NONCLUSTERED INDEX idx_nc_lastname ON HR.Employees(lastname);
CREATE NONCLUSTERED INDEX idx_nc_postalcode ON HR.Employees(postalcode);
-- Populate table HR.Employees
SET IDENTITY_INSERT HR.Employees ON;
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(1, N'Davis', N'Sara', N'CEO', N'Ms.', '19581208 00:00:00.000', '20020501 00:00:00.000', N'7890 - 20th Ave. E., Apt. 2A', N'Seattle', N'WA', N'10003', N'USA', N'(206) 555-0101', NULL);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(2, N'Funk', N'Don', N'Vice President, Sales', N'Dr.', '19620219 00:00:00.000', '20020814 00:00:00.000', N'9012 W. Capital Way', N'Tacoma', N'WA', N'10001', N'USA', N'(206) 555-0100', 1);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(3, N'Lew', N'Judy', N'Sales Manager', N'Ms.', '19730830 00:00:00.000', '20020401 00:00:00.000', N'2345 Moss Bay Blvd.', N'Kirkland', N'WA', N'10007', N'USA', N'(206) 555-0103', 2);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(4, N'Peled', N'Yael', N'Sales Representative', N'Mrs.', '19470919 00:00:00.000', '20030503 00:00:00.000', N'5678 Old Redmond Rd.', N'Redmond', N'WA', N'10009', N'USA', N'(206) 555-0104', 3);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(5, N'Buck', N'Sven', N'Sales Manager', N'Mr.', '19650304 00:00:00.000', '20031017 00:00:00.000', N'8901 Garrett Hill', N'London', NULL, N'10004', N'UK', N'(71) 234-5678', 2);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(6, N'Suurs', N'Paul', N'Sales Representative', N'Mr.', '19730702 00:00:00.000', '20031017 00:00:00.000', N'3456 Coventry House, Miner Rd.', N'London', NULL, N'10005', N'UK', N'(71) 345-6789', 5);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(7, N'King', N'Russell', N'Sales Representative', N'Mr.', '19700529 00:00:00.000', '20040102 00:00:00.000', N'6789 Edgeham Hollow, Winchester Way', N'London', NULL, N'10002', N'UK', N'(71) 123-4567', 5);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(8, N'Cameron', N'Maria', N'Sales Representative', N'Ms.', '19680109 00:00:00.000', '20040305 00:00:00.000', N'4567 - 11th Ave. N.E.', N'Seattle', N'WA', N'10006', N'USA', N'(206) 555-0102', 3);
INSERT INTO HR.Employees(empid, lastname, firstname, title, titleofcourtesy, birthdate, hiredate, address, city, region, postalcode, country, phone, mgrid)
VALUES(9, N'Dolgopyatova', N'Zoya', N'Sales Representative', N'Ms.', '19760127 00:00:00.000', '20041115 00:00:00.000', N'1234 Houndstooth Rd.', N'London', NULL, N'10008', N'UK', N'(71) 456-7890', 5);
SET IDENTITY_INSERT HR.Employees OFF;
資料有了,再來就是建立 ASP.NET專案,如下,
專案好了之後,就是透過 Entity Framework 來幫我們處理 DB 的事,建立 ADO.NET Entity Data Model,如下,
然後選取 Employee 資料表,如下,
有了 Model 後,就可以透過 Scaffolding 來建立 Web Forms ,如下,
加入後,會在專案中,建立 Employees 目錄,並產生相關的檔案,如下,
這時,就可以執行 Employees/Default.aspx,如下,
而且可以換頁、修改、新增及刪除的功能,
可是目前顯示欄位是用DB的欄位名稱,而且有些必填的欄位沒有被設定到。
所以我們可以新增一個 EmployeesMetadata Class (屬性可以從 Employees Copy)來設定 Employees ,
並設定 Employees 的 MetadataType 資訊(新增 Employees 的 partial class),如下 ,
[MetadataType(typeof(EmployeesMetadata))]
public partial class Employees
{
}
public class EmployeesMetadata
{
[Required]
[Display(Name = "員工編號")]
public int empid { get; set; }
[Required]
[Display(Name = "姓")]
public string lastname { get; set; }
[Required]
[Display(Name = "名")]
public string firstname { get; set; }
[Required]
[Display(Name = "Title")]
public string title { get; set; }
[Required]
[Display(Name = "Title Courtesy")]
public string titleofcourtesy { get; set; }
[Required]
[Display(Name = "生日")]
public System.DateTime birthdate { get; set; }
[Required]
[Display(Name = "受僱日期")]
public System.DateTime hiredate { get; set; }
[Required]
[Display(Name = "地址")]
public string address { get; set; }
[Required]
[Display(Name = "城市")]
public string city { get; set; }
public string region { get; set; }
public string postalcode { get; set; }
[Required]
[Display(Name = "國家")]
public string country { get; set; }
[Required]
[Display(Name = "電話號碼")]
public string phone { get; set; }
[Display(Name = "主管")]
public Nullable<int> mgrid { get; set; }
}
建置再執行新增,欄位名稱有依 Metadata 設定的顯示,必填驗證也有一併的加進去了哦! 如下,
功能好了之後,就可以加在 Master Page 之中,如下,
<li><a runat="server" href="~/Employees/Default.aspx">Employees</a></li>
其他部份,大家可以依需求再進行調整。
參考資料
ASP.NET Web Forms 持續進化及新功能 - Visual Studio 2013 Update 2 安裝後
Getting Started With Web Forms Scaffolding in ASP.Net
Using DataAnnotations with Entity Framework 4 Entities for a More Dynamic MVC Model
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^