Human Resources ASP.NET 网站(EDM 示例应用程序)

Human Resources ASP.NET 应用程序是一个界面,可用在网络中或 Internet 上以显示使用示例人力资源技能 WinApp(EDM 示例应用程序)定义和编译的数据。此 Web 应用程序引用的架构和 DLL 与 Windows 应用程序所使用的相同。该网站分三个阶段构建:

  1. 将用户界面设计为网页上的窗体,此窗体具有 textbox 和 gridview 控件以管理输入和显示数据。

  2. 编写界面后台的 ASP.NET 代码以处理输入事件和显示对用户输入参数的搜索结果。

  3. 配置 IIS 以运行 ASP.NET 应用程序并连接到 HRSkills 数据模型所使用的 SQL Server 上存储的数据。

用户界面

用户界面包括两个文本框,用于输入定义搜索参数的文本。用户可以通过员工姓氏的第一个字母来搜索员工,或通过关键字来搜索技能。对这些参数的搜索结果通过 gridview 控件来显示,该控件绑定到表示员工、技能和技能信息以及个人推荐人的实体数据模型对象查询。

用户界面显示在下面的屏幕中:

ASP.NET HR 技能应用程序查询结果

下列 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 runat="server">
    <title>HR Skills Online</title>
</head>
<body>
    <form id="form1" runat="server">
    &nbsp;<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>
&nbsp;<asp:TextBox ID="TextBox1" runat="server" MaxLength="1" 
        ontextchanged="TextBox1_TextChanged" Width="29px" 
        AutoPostBack="True"></asp:TextBox>
&nbsp;&nbsp;
        <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>
&nbsp;<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>

Web 控件内含的 ASP.NET 代码

用于处理由用户输入生成的事件的 C# 代码包含在 Default.aspx.cs 代码隐藏文件中。下列代码用于定义对象查询并将结果绑定到 gridview 控件。此操作在 HR 技能应用程序代码(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)
        {

        }
    }
}

另请参见

概念

人力资源技能 WinApp(EDM 示例应用程序)