本文介绍了DataBinding不包含属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,因为我是新的c#

Please help as i am new in c#

我面临下面的错误

DataBinding:'Sherserve.CustomTypeLayer.EmployeeLeave'不包含名称为empId的属性

"DataBinding: 'Sherserve.CustomTypeLayer.EmployeeLeave' does not contain a property with the name 'empId' "

这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    LMSManager leavestatus = new LMSManager();
    int emplid = Convert.ToInt32(Session["EmployeeID"]);
    List<EmployeeLeave> leaverecord = leavestatus.GetLeaveRecord(emplid);

    GridView1.DataSource = leaverecord;
    GridView1.DataBind();
}

这里是EmployeeLeave:

here is EmployeeLeave :

public class EmployeeLeave
{
    public LeaveReason LeaveType { get; set; }
    public int EmployeeId { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public string Reason { get; set; }
}

这里是aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
  PageSize="3" OnPageIndexChanging="GridView1_PageIndexchanging" CellPadding="4"
  ForeColor="#333333" Height="16px"
  Style="margin-bottom: 0px; margin-right: 0px;">
  <%-- DataSourceID="SqlDataSource1"--%>
    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />

    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("empId")%>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblLeaveType" runat="server" Text='<%#Eval("LeaveType") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblempid" runat="server" Text='<%#Eval("EmployeeId") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblDateFrm" runat="server" Text='<%#Eval("DateFrom") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblDateTo" runat="server" Text='<%#Eval("DateTo") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblRsn" runat="server" Text='<%#Eval("Reason") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


推荐答案

看起来你的数据绑定是指 empId EmployeeLeave 上的字段实际上称为 EmployeeId

It looks like your databinding is referring to empId while the field on EmployeeLeave is actually called EmployeeId.

尝试更改

<ItemTemplate>
    <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("empId")%>'></asp:Label>
</ItemTemplate>

<ItemTemplate>
    <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("EmployeeId")%>'></asp:Label>
</ItemTemplate>

这篇关于DataBinding不包含属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:15