人力資源 ASP.NET 網站 (EDM 範例應用程式)
人力資源 ASP.NET 應用程式是可在網路或網際網路上使用的介面,以便使用範例來顯示所定義及編譯的資料:人力資源技能 WinApp (EDM 範例應用程式)。這個 Web 應用程式所參考的結構描述和 DLL 與 Windows 應用程式使用的相同。網站會在三個階段建置:
將使用者介面設計為網頁上的表單 (包含 textbox 和 gridview 控制項),以管理輸入及顯示資料。
撰寫此介面的後置 ASP.NET 程式碼來處理輸入事件,並顯示使用者輸入之參數的搜尋結果。
設定 IIS 執行 ASP.NET 應用程式,並連接到 HRSkills 資料模型使用之 SQL Server 上儲存的資料。
使用者介面
使用者介面是由定義搜尋參數之文字輸入的兩個文字方塊所組成。使用者可以依據姓氏的第一個字母搜尋員工,或是依據關鍵字搜尋技能。這些參數的搜尋結果會由繫結至 Entity Data Model 物件查詢的 gridview 控制項所顯示,以代表員工、技能和技能資訊及個人參考。
使用者介面顯示在以下螢幕畫面中:
下列 XML 規格會定義網頁上將要顯示給使用者的表單和控制項。將此語法複製到 Visual Studio 中 ASP.NET Web 應用程式的 Default.aspx 檔。
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>HR Skills Online</title>
</head>
<body>
<form id="form1" >
<asp:Label ID="Label1" Font-Bold="True"
Text="Filter employees by first letter of last name;
Use * to get all employees: "></asp:Label>
<asp:TextBox ID="TextBox1" MaxLength="1"
ontextchanged="TextBox1_TextChanged" Width="29px"
AutoPostBack="True"></asp:TextBox>
<br />
<asp:Label ID="Label2" Font-Bold="True"
Text="Search for skills on keyword; Use * to get all skills: ">
</asp:Label>
<asp:TextBox ID="TextBox2" MaxLength="50"
Width="271px" AutoPostBack="True"
ontextchanged="TextBox2_TextChanged">
</asp:TextBox>
<asp:GridView
ID="GridView1" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2"
ForeColor="Black"
onselectedindexchanged="GridView1_SelectedIndexChanged"
Caption="Employees (Click row for skills)" CaptionAlign="Top"
CellSpacing="1">
<footerstyle backcolor="Tan" />
<selectedrowstyle backcolor="DarkSlateBlue"
forecolor="GhostWhite" />
<pagerstyle backcolor="PaleGoldenrod"
forecolor="DarkSlateBlue"
horizontalalign="Center" />
<headerstyle backcolor="Tan" font-bold="True"
font-size="Medium" />
<alternatingrowstyle backcolor="PaleGoldenrod" />
</asp:GridView>
<br />
<asp:GridView
ID="GridView2" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2"
ForeColor="Black"
onselectedindexchanged="GridView2_SelectedIndexChanged"
Caption="Skills (Click row for details)" CaptionAlign="Top"
CellSpacing="1"
PageSize="5">
<footerstyle backcolor="Tan" />
<selectedrowstyle backcolor="DarkSlateBlue"
forecolor="GhostWhite" />
<pagerstyle backcolor="PaleGoldenrod"
forecolor="DarkSlateBlue" horizontalalign="Center" />
<headerstyle backcolor="Tan" font-bold="True" />
<alternatingrowstyle backcolor="PaleGoldenrod" />
</asp:GridView>
<br />
<asp:GridView ID="GridView3"
BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CaptionAlign="Top"
CellPadding="2"
CellSpacing="1" ForeColor="Black"
onselectedindexchanged="GridView3_SelectedIndexChanged">
<footerstyle backcolor="Tan" forecolor="GhostWhite" />
<pagerstyle backcolor="PaleGoldenrod"
forecolor="DarkSlateBlue"
horizontalalign="Center" />
<headerstyle backcolor="Tan" font-bold="True" />
<alternatingrowstyle backcolor="PaleGoldenrod" />
</asp:GridView>
</form>
</body>
</html>
ASP.NET 程式碼後置 Web 控制項
用來處理使用者輸入所產生之事件的 C# 程式碼包含在 Default.aspx.cs 程式碼後置檔案中。下列程式碼是用來定義物件查詢,並將結果繫結到 gridview 控制項。人力資源應用程式程式碼 (EDM 範例應用程式) 中會更詳細描述這些作業。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRSkillsModel;
using System.Data.Objects;
namespace HRSkillsOnline
{
public partial class _Default : System.Web.UI.Page
{
protected void GridView1_SelectedIndexChanged(object sender,
EventArgs e)
{
GridView2.SelectedIndex = -1;
GridView3.SelectedIndex = -1;
string connectionStr =
Application.Contents["connectionString"] as string;
using (HRSkillsEntities HRSkills = new HRSkillsEntities())
{
Guid empId = new Guid(
GridView1.SelectedRow.Cells[1].Text);
// Find the Employee.
ObjectParameter param = new ObjectParameter("p", empId);
Employees employee = HRSkills.Employees.Where(
"it.EmployeeId = @p", param).First();
employee.Skills.Load();
GridView2.Caption = "Skills of "
+ employee.FirstName + " " +
employee.LastName;
GridView2.AutoGenerateSelectButton = true;
GridView2.DataSource = employee.Skills;
GridView2.DataBind();
GridView2.Focus();
// Find Employee references.
employee.References.Load();
GridView3.AutoGenerateSelectButton = false;
GridView3.Caption = "References for "
+ employee.FirstName + " " +
employee.LastName;
GridView3.DataSource = employee.References;
GridView3.DataBind();
}
}
protected void GridView2_SelectedIndexChanged(object sender,
EventArgs e)
{
string connectionStr =
Application.Contents["connectionString"] as string;
using (HRSkillsEntities HRSkills = new HRSkillsEntities())
{
// Create a ObjectParameter from the SkillId property
// and get the Skill.
Guid skillId = new Guid(
GridView2.SelectedRow.Cells[1].Text);
ObjectParameter param =
new ObjectParameter("p", skillId);
Skills skill = HRSkills.Skills.Where("it.SkillId = @p",
param).First();
// Load the SkillInfo entities using
// SkillInfo_Skill association.
skill.SkillInfo.Load();
GridView3.AutoGenerateSelectButton = true;
GridView3.DataSource = skill.SkillInfo;
GridView3.DataBind();
GridView3.Caption = "Skills URLs for " +
GridView2.SelectedRow.Cells[2].Text;
GridView3.Focus();
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
GridView1.SelectedIndex = -1;
GridView2.SelectedIndex = -1;
GridView3.SelectedIndex = -1;
string connectionStr =
Application.Contents["connectionString"] as string;
using (HRSkillsEntities HRSkills = new HRSkillsEntities())
{
if ("*".Equals(TextBox1.Text))
{
GridView1.Caption = "All Employees in System";
GridView1.AutoGenerateSelectButton = true;
GridView1.DataSource =
HRSkills.Employees.Execute(
MergeOption.OverwriteChanges);
GridView1.DataBind();
TextBox1.Text = "";
return;
}
ObjectParameter param =
new ObjectParameter("p", TextBox1.Text.ToUpper());
ObjectQuery<Employees> dbQuery =
HRSkills.Employees.Where(
"SqlServer.SUBSTRING(it.LastName, 1, 1) = @p",
param);
GridView1.Caption = "Employees' last name on: " +
TextBox1.Text.ToUpper();
GridView1.AutoGenerateSelectButton = true;
GridView1.DataSource =
dbQuery.Execute(MergeOption.OverwriteChanges);
GridView1.DataBind();
}
TextBox1.Text = "";
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
string connectionStr =
Application.Contents["connectionString"] as string;
using (HRSkillsEntities HRSkills = new HRSkillsEntities())
{
GridView1.SelectedIndex = -1;
GridView2.SelectedIndex = -1;
GridView3.SelectedIndex = -1;
if ("*".Equals(TextBox2.Text))
{
GridView2.Caption = "All Skills in System.";
GridView2.AutoGenerateSelectButton = true;
GridView2.DataSource =
HRSkills.Skills.Execute(
MergeOption.OverwriteChanges );
GridView2.DataBind();
GridView1.DataSource = null;
GridView1.DataBind();
TextBox2.Text = "";
return;
}
// Create ObjectParameter from each keyword
// and search properties of Skills.
ObjectParameter param = new ObjectParameter(
"p", "%" + TextBox2.Text.Trim(
'\"', '&', '%', '$', '#') + "%");
ObjectQuery<Skills> skillsQuery =
HRSkills.Skills.Where(
"it.BriefDescription Like @p " +
"OR it.SkillName Like @p", param);
List<Employees> employeeList = new List<Employees>();
foreach (Skills skill in skillsQuery)
{
skill.EmployeesReference.Load();
employeeList.Add(skill.Employees);
}
GridView1.Caption = "Employees with skills on " +
TextBox2.Text;
GridView1.AutoGenerateSelectButton = true;
GridView1.DataSource = employeeList;
GridView1.DataBind();
GridView2.Caption = "Skills on " +
TextBox2.Text;
GridView2.AutoGenerateSelectButton = true;
GridView2.DataSource =
skillsQuery.Execute(MergeOption.OverwriteChanges);
GridView2.DataBind();
}
TextBox2.Text = "";
}
protected void GridView3_SelectedIndexChanged(object sender,
EventArgs e)
{
string scriptString = @"<script> window.open('" +
GridView3.SelectedRow.Cells[2].Text +
"', left=100)</script>";
if (!ClientScript.IsClientScriptBlockRegistered("PopUpWindow"))
ClientScript.RegisterClientScriptBlock(GridView3.GetType(),
"PopUpWindow", scriptString);
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}