我在gridview的DataBinding()时收到以下错误。 虽然“siteName”字段被定义为结构“StructSelectedSiteList”的一部分,但ASP.net gridview定义中的DataBinder(...)无法识别该字段。所有字段和结构都定义为公共。
我曾参考过以前的帖子,但没有帮助。 DataBinding does not contain a property
感谢您的回复。谢谢!
这是代码。 StructSelectedSiteList - 用值填充的结构。这是数据源。
[Serializable]
public struct StructSelectedSiteList
{
public string siteName;
public int taskId;
};
在下面的代码中,我填充了结构。
var lstSites = new List<StructSelectedSiteList>();
LOOP1:
// populate the struct for selected sites
StructSelectedSiteList structSelectedSiteList;
structSelectedSiteList.siteName = lblSiteName.Text;
structSelectedSiteList.taskId = taskId;
lstSites.Add(structSelectedSiteList);
现在gridview与填充结构绑定。
gvSelectedSites.DataSource = lstSites;
gvSelectedSites.DataBind(); <<----- Error occurs here
ASP.net gridview定义是 -
<asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True"
CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI"
Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center"
EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="siteName">
<ItemTemplate>
<asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "siteName")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" Visible="false">
<ItemTemplate>
<asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "taskId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
答案 0 :(得分:0)
经过测试,我发现我正在访问&#39; siteName&#39;作为结构字段,但我需要将其作为属性访问。因此,在将结构的所有字段转换为属性之后,我能够无异常地继续。感谢解决方案"DataBinding: 'index+NewsItem' does not contain a property with the name 'Link'", but property exists (Not a Typo)和'myObject' does not contain a property with the name 'ID' (Not a typo)