将网格视图与泛型列表绑定(错误:"FieldProperty not assigned")



我是。net的新手。我试图将通用List绑定到aspx页面中的GridView。我设置了AutoGenerateColumns="false",我在我的。aspx页面上定义了列,并绑定了它们,但它仍然抛出错误错误A field or property with the name 'Assigned' was not found on the selected data source.

我尝试了所有的选择,但没有结果。CL是我的命名空间的别名。

SITESTATUS类
public class SiteStatus
{
    public string Opened;
    public string Assigned;
    public string LocationAddress;
    public string LocationId;
    public SiteStatus(string Assigned, string Opened, string LocationAddress, string  LocationId)
    {
        this.Assigned = Assigned;
        this.Opened = Opened;
        this.LocationAddress = LocationAddress;
        this.LocationId = LocationId;
    }    
}

Aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SiteSurveyStatus.aspx.cs"  MasterPageFile="~/Site.Master" Inherits="Website.WebForms.SiteSurveyStatus" %>    
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="SiteSurvey" runat="server" Text="Site Survey Status" Font-Bold="True"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" PageSize="15" CellPadding="0" Width="100%" AutoGenerateColumns="false" EnableViewState="True">
            <Columns>
                <asp:BoundField HeaderText="Assigned" DataField="Assigned" SortExpression="Assigned" />
                <asp:BoundField HeaderText="Opened" DataField="Opened" SortExpression="Opened" />
                <asp:BoundField HeaderText="Location" DataField="LocationAddress" />
                <asp:BoundField HeaderText="LocationId" DataField="LocationId" />
            </Columns>
        </asp:GridView>
    </form>

代码:

protected void Page_Load(object sender, EventArgs e)
{
    List<CL.SiteStatus> list = new List<CL.SiteStatus>();
    list.Add(new CL.SiteStatus("09/12/2011", "User123", "Dallas TX 75724", "USATX75724"));
    list.Add(new CL.SiteStatus("10/11/2011", "User234", "Houston TX 77724", "USATX77724"));
    list.Add(new CL.SiteStatus("02/30/2011", "User567", "Austin TX 70748", "USATX70748"));
    list.Add(new CL.SiteStatus("03/01/2011", "User1234", "El Paso TX 71711", "USATX71711"));
    list.Add(new CL.SiteStatus("04/02/2011", "User125", "Chicago IL 33456", "USAIL33456"));
    GridView1.DataSource = list.ToList();
    GridView1.DataBind();
}

问题似乎在于您的SiteStatus类。你有属性但是没有修饰符,所以没有外部代码可以访问它们。

试题:

public class SiteStatus
{
    public string Opened { get; set; }
    public string Assigned { get; set; }
    public string LocationAddress { get; set; }
    public string LocationId { get; set; }
    public SiteStatus(string Assigned, string Opened, string LocationAddress, string LocationId)
    {
        this.Assigned = Assigned;
        this.Opened = Opened;
        this.LocationAddress = LocationAddress;
        this.LocationId = LocationId;
    }
}

相关内容

最新更新