一个模板字段中有两个项目模板?

Two Item templates in one Template field?

是否可以在一个模板字段中有两个项目模板?这是我当前的代码,它不起作用,因为当我 运行 它 e.Row.FindControl("gvQuoteItems") 总是 returns null:

A​​SPX:

 <asp:TemplateField ItemStyle-Width="50px">
                    <ItemTemplate>
                        <img alt="" style="cursor: pointer" src="images/plus.png" />
                        <asp:Panel ID="pnlQuoteItems" runat="server" Style="display: none">
                            <asp:GridView ID="gvQuoteItems" runat="server" AutoGenerateColumns="false" CssClass="GridView" OnRowDeleting="gvQuote_RowDeleting" ShowFooter="True">
                                <Columns>
                                    <asp:CommandField ShowDeleteButton="True" />
                                    <asp:BoundField DataField="QuoteItemID" HeaderText="QuoteItemID" SortExpression="QuoteItemID" Visible="false" />
                                    <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" Visible="false" />
                                    <asp:BoundField DataField="Cover" HeaderText="Cover" SortExpression="Cover" />
                                    <asp:BoundField DataField="CoverType" HeaderText="Cover Type" SortExpression="CoverType"  Visible="false" />
                                    <asp:BoundField DataField="SumInsured" HeaderText="Sum Insured" SortExpression="SumInsured" />
                                    <asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" />
                                    <asp:BoundField DataField="AnnualPremium" HeaderText="Annual Premium" SortExpression="AnnualPremium" />
                                    <asp:BoundField DataField="MonthlyPremium" HeaderText="Monthly Premium" SortExpression="MonthlyPremium" />
                                </Columns>
                                <FooterStyle BackColor="#022439" Font-Bold="True" ForeColor="White" />
                                <EmptyDataTemplate>
                                    No Data To Display!
                                </EmptyDataTemplate>
                            </asp:GridView>
                        </asp:Panel>
                    </ItemTemplate>
                    <ItemTemplate>
                        <img alt="" style="cursor: pointer" src="images/plus.png" />
                        <asp:Panel ID="pnlMotorQuoteItems" runat="server" Style="display: none">
                            <asp:GridView ID="gvMotorQuoteItems" runat="server" AutoGenerateColumns="false" CssClass="GridView" OnRowDeleting="gvQuote_RowDeleting" ShowFooter="True">
                                <Columns>
                                    <asp:CommandField ShowDeleteButton="True" />
                                    <asp:BoundField DataField="QuoteItemID" HeaderText="QuoteItemID" SortExpression="QuoteItemID" Visible="false" />
                                    <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" Visible="false" />
                                    <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
                                    <asp:BoundField DataField="MakeAndModel" HeaderText="Make And Model" SortExpression="MakeAndModel" />
                                    <asp:BoundField DataField="NCB" HeaderText="NCB" SortExpression="NCB" />
                                    <asp:BoundField DataField="Cover" HeaderText="Cover" SortExpression="Cover" />
                                    <asp:BoundField DataField="CoverType" HeaderText="Cover Type" SortExpression="CoverType"  Visible="false" />
                                    <asp:BoundField DataField="SumInsured" HeaderText="Sum Insured" SortExpression="SumInsured" />
                                    <asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" />
                                    <asp:BoundField DataField="AnnualPremium" HeaderText="Annual Premium" SortExpression="AnnualPremium" />
                                    <asp:BoundField DataField="MonthlyPremium" HeaderText="Monthly Premium" SortExpression="MonthlyPremium" />
                                </Columns>
                                <FooterStyle BackColor="#022439" Font-Bold="True" ForeColor="White" />
                                <EmptyDataTemplate>
                                    No Data To Display!
                                </EmptyDataTemplate>
                            </asp:GridView>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:TemplateField>

代码隐藏:

  if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int categoryID = int.Parse(gvQuote2.DataKeys[e.Row.RowIndex].Value.ToString());
               if (categoryID == 16)
                {
                    GridView gvQuoteItems = e.Row.FindControl("gvMotorQuoteItems") as GridView;
                    gvQuoteItems.DataSource = _QuoteBLL._GetMotorQuoteItemsDataTable(quote.QuoteID);
                    gvQuoteItems.DataBind();
                }
                else
                {
                    GridView gvQuoteItems = e.Row.FindControl("gvQuoteItems") as GridView;
                    gvQuoteItems.DataSource = _QuoteBLL._GetQuoteItemsDataTable(quote.QuoteID, categoryID);
                    gvQuoteItems.DataBind();
                }
            }

当我尝试 运行 上面的内容时,e.Row.FindControl("gvQuoteItems") 总是 returns 为空。但是如果我删除第二个 ItemTemplate,它就可以正常工作。但是你看,其中一行有不同的列(gvMotorQuoteItems)。我该怎么做?

您不能两次覆盖同一个模板。但是,您可以在模板中有两个 placeholeholders,并且 show/hide 只有相关的 Visible 属性.

<ItemTemplate>
  <asp:PlaceHolder ID="phFirst" runat="server">
    ... pnlQuoteItems
  </asp:PlaceHolder>
  <asp:PlaceHolder ID="phSecond" runat="server">
    ... pnlMotorQuoteItems
  </asp:PlaceHolder>
</ItemTemplate>