将网格视图与通用列表绑定(错误:“字段\属性未分配”)

发布于 2024-12-13 08:27:21 字数 2515 浏览 0 评论 0原文

我是 .NET 新手。我正在尝试将通用 List 绑定到 aspx 页面中的 GridView 。我设置了 AutoGenerateColumns="false",我在 .aspx 页面上定义了列,并绑定了它们,但它仍然抛出错误 名称为“Assigned”的字段或属性不是在所选数据源上找到。

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

SITESTATUS CLASS

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();
}

I am new to .NET. I am trying to bind a generic List to a GridView in the aspx page. I set AutoGenerateColumns="false", I defined the columns on my .aspx page, and bound them but it's still throwing the error error A field or property with the name 'Assigned' was not found on the selected data source.

I tried all options but end up no where. CL is an alias for my namespace.

SITESTATUS CLASS

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 File

<%@ 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>

Code Behind:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-12-20 08:27:21

问题看起来出在您的 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;
    }
}

你的其余标记对我来说看起来不错。

The problem looks like it lies with your SiteStatus class. You have properties but no modifiers, so no external code can access them.

Try:

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;
    }
}

The rest of your markup looks fine to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文