请帮助我,因为我是c#
的新手我面临以下错误
“DataBinding:'Sherserve.CustomTypeLayer.EmployeeLeave'不包含名为'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:
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>
答案 0 :(得分:2)
看起来您的数据绑定指的是empId
,EmployeeLeave
上的字段实际上称为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>