无法将TemplateField添加到Datagrid

时间:2015-06-08 12:00:40

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

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

  a中不允许

  ' System.Web.UI.WebControls.DataGridColumnCollection&#39 ;.

 <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都有绿线,这意味着它不是已知的元素。

1 个答案:

答案 0 :(得分:1)

对于DataGrid,您应该使用TemplateColumn,因为它是从System.Web.UI.WebControls.DataGridColumn继承的。

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