【问题标题】:Can't add TemplateField to Datagrid无法将 TemplateField 添加到 Datagrid
【发布时间】:2015-08-22 21:20:21
【问题描述】:

我正在尝试将 DropDownList 添加到 DataGrid 中,以便可以轻松更改位置列。我收到 TemplateField、itemtemplate、label 和 DropDownList 的错误。我收到构建错误:

不允许在 'System.Web.UI.WebControls.DataGridColumnCollection'。

 <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                    <asp:TemplateField HeaderText="Trailer Location">
                        <itemtemplate>
                            <asp:Label ID="lblLocation" runat="server" Text='<%# Eval("Location") %>' Visible = "false" />
                            <asp:DropDownList ID="ddlLocation" runat="server">
                            </asp:DropDownList>
                     </itemtemplate>
                    </asp:TemplateField>
                    <asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>

                    </Columns>
            </asp:DataGrid>

TemplateField、itemtemplate、label 和 DropDownList 下面都有绿线,表示它不是已知元素。

【问题讨论】:

  • 哪些是不允许的?
  • 您使用的是哪个版本的 .net fx?

标签: html asp.net visual-studio drop-down-menu datagrid


【解决方案1】:

您应该使用TemplateColumn,因为它继承自System.Web.UI.WebControls.DataGridColumn

TemplateField 继承自 System.Web.UI.WebControls.DataControlField,这对 GridView 有意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2012-01-15
    • 2011-07-12
    • 1970-01-01
    • 2012-02-13
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多