0

我有一个包含名称和 ID 的国家/地区的数据库。

这是代码隐藏。

CountriesEntities context = new CountriesEntities();
using (context)
{
    this.gridViewCountries.DataSource = context.Countries;
    this.gridViewCountries.DataBind();
}

当我把它放在 html 中时,它会像 expeted 一样工作,显示所有列和每个国家的信息。 <asp:GridView ID="gridViewCountries" runat="server" AutoGenerateColumns="true"/>

但是当我将 GridView 更改为:

<asp:GridView ID="gridViewCountries" runat="server" AutoGenerateColumns="false">
    <asp:Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                Name
            </HeaderTemplate>
            <ItemTemplate>
                <span><%# Eval("Name") %></span>  
            </ItemTemplate>
        </asp:TemplateField>
    </asp:Columns>
</asp:GridView>

页面上没有显示任何内容,我不知道为什么。有人可以给我一个提示吗?

4

1 回答 1

0

In a GridView, I usually use Bind for this, not Eval:

<ItemTemplate>
    <span><%# Bind("Name") %></span>  
</ItemTemplate>

If that doesn't work, the only other possibility I can think if is that the Datasource that's bound to your GridView doesn't have a column named "Name" (for some reason).

Edit:

Actually, you might also want to try using that inside a server-side Label control:

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>  
</ItemTemplate>
于 2012-08-14T13:11:36.330 回答