将网格视图与通用列表绑定(错误:“字段\属性未分配”)
我是 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题看起来出在您的 SiteStatus 类上。您有属性但没有修饰符,因此外部代码无法访问它们。
尝试:
你的其余标记对我来说看起来不错。
The problem looks like it lies with your SiteStatus class. You have properties but no modifiers, so no external code can access them.
Try:
The rest of your markup looks fine to me.