簡體   English   中英

在ASP.NET Web表單中使用Entity Framework(代碼優先方法)在Repeater控件中顯示詳細信息

[英]Show details in Repeater control using Entity Framework (code first approach) in asp.net webforms

我如何使用實體框架代碼優先方法獲取ASp.net Web表單的數據

我需要使用收割者控件在Emp.aspx頁面上顯示Emp表中的員工列表

我是EF的新手,正在嘗試為asp.net Webforms學習EF

到目前為止,我已經創建了以下App_Code文件夾

App_Code
    DBClass
        Dept.cs
        Emp.cs
        EmployeeDBContent.cs
        EmpRepository.cs

所有文件的代碼

Dept.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    /// <summary>
    /// Summary description for Dept
    /// </summary>
 namespace empNS
{   
public class Dept
    {
        //Scalar properties
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        //Navigation Property
        public List<Emp> Emp { get; set; }
    }
}

Emp.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for EmpRepository
    /// </summary>
namespace empNS
{
    public class EmpRepository
    {
        public List<Dept> GetDepartments()
        {
            EmployeeDBContext empDBContext = new EmployeeDBContext();
            return empDBContext.Dept.ToList();
        }
    }
}

EmployeeDBContext.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;

    /// <summary>
    /// Summary description for EmployeeDBContext
    /// </summary>
namespace empNS
{
    public class EmployeeDBContext: DbContext
    {
        public DbSet<Dept> Dept { get; set; }
        public DbSet<Emp> Emp { get; set; }

    }
}

EmpRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for EmpRepository
/// </summary>

命名空間empNS {公共類EmpRepository {公共靜態列表GetEmployees(){EmployeeDBContext empDBContext = new EmployeeDBContext(); 返回empDBContext.Emp.ToList(); }}

}

我不確定如何使用中繼器控件在Emp.aspx頁面上顯示應聘者列表

Emp.aspx代碼

 <h1>Employee List</h1>
        <asp:Repeater ID="empList" runat="server">
            <ItemTemplate>
                <tr>
                    <td><%#Eval("ID") %></td>
                    <td><%#Eval("FirstName") %></td>
                    <td><%#Eval("LastName") %></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </div>

代碼隱藏

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
namespace empNS
{
    public partial class Emp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //UPDATED CODE
        rptEmpList.DataSource = EmpRepository.GetEmployees();
        rptEmpList.DataBind();
        }
    }
}

如何從EmpRepository.cs調用GetDepartments()函數,以便在轉發器控件中顯示員工列表。

我對此完全陌生,還無法找到一個簡單的示例,該示例可以使用代碼優先方法來綁定轉發器控制以顯示信息。 任何幫助或指向教程的幫助都會有所幫助。

您可以將List直接綁定到中繼器。 只要確保您指向正確的名稱空間即可。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        empList.DataSource = EmpRepository.GetDepartments();
        empList.DataBind();
    }
}

並且由於您將類綁定到Repeater,因此更容易將其設置為純類型。 然后,您可以直接訪問屬性。

<asp:Repeater ID="empList" runat="server" ItemType="YourNameSpace.Dept">
    <ItemTemplate>
        <tr>
            <td><%# Item.ID %></td>
            <td><%# Item.FirstName %></td>
            <td><%# Item.LastName  %></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

但是,員工似乎是部門的財產。 因此,如果您需要這些,則必須使用嵌套的Repeater,循環所有部門以獲取員工,或為Repeater選擇一個部門。

這將獲得第一個部門的員工名單

empList.DataSource = EmpRepository.GetDepartments()[0].Emp;

嵌套中繼器

<asp:Repeater ID="empList" runat="server" ItemType="YourNameSpace.Dept">
    <ItemTemplate>
        <tr>
            <asp:Repeater ID="empListNested" runat="server" ItemType="YourNameSpace.Dept.Emp" DataSource='<%# Item.Emp %>'>
                <ItemTemplate>
                    <tr>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </tr>
    </ItemTemplate>
</asp:Repeater>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM