Human Resources ASP.NET アプリケーションは、「Human Resources Skills WinApp (EDM サンプル アプリケーション)」の例を使用して定義およびコンパイルしたデータを表示するために、ネットワークまたはインターネット上で使用されるインターフェイスです。この Web アプリケーションによって参照されるスキーマと DLL は、Windows アプリケーションによって使用されるものと同じです。この Web サイトの構築は、次の 3 段階で行われます。
入力値の管理およびデータ表示のためのテキスト ボックス コントロールとグリッド ビュー コントロールが含まれた Web ページ上のフォームとして、ユーザー インターフェイスをデザインします。
入力イベントを処理し、ユーザーによって入力されたパラメータに基づく検索結果を表示するための ASP.NET コードをインターフェイスの背後に記述します。
ASP.NET アプリケーションを実行し、HRSkills データ モデルによって使用される SQL Server 上の格納データに接続するように、IIS を構成します。
ユーザー インターフェイス
ユーザー インターフェイスは、検索パラメータを定義するテキスト入力用の 2 つのテキスト ボックスから構成されます。ユーザーは、姓の最初の文字で従業員を検索したり、キーワードでスキルを検索したりすることができます。これらのパラメータに基づく検索結果は、従業員、スキル、スキル情報、および個人用参照を表す Entity Data Model オブジェクト クエリにバインドされたグリッド ビュー コントロールによって表示されます。
次の画面表示に、ユーザー インターフェイスを示します。
次の XML 仕様では、ユーザーに対して表示される Web ページ上のフォームとコントロールが定義されます。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 runat="server">
<title>HR Skills Online</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True"
Text="Filter employees by first letter of last name;
Use * to get all employees: "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" MaxLength="1"
ontextchanged="TextBox1_TextChanged" Width="29px"
AutoPostBack="True"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True"
Text="Search for skills on keyword; Use * to get all skills: ">
</asp:Label>
<asp:TextBox ID="TextBox2" runat="server" MaxLength="50"
Width="271px" AutoPostBack="True"
ontextchanged="TextBox2_TextChanged">
</asp:TextBox>
<asp:GridView
ID="GridView1" runat="server" 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" runat="server" 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" runat="server"
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 分離コード ファイルに含まれています。次のコードは、オブジェクト クエリを定義し、グリッド ビュー コントロールに結果をバインドするために使用されます。これらの操作については、「HR Skills アプリケーション コード (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)
{
}
}
}