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

时间:2015-07-20 07:54:20

标签: c# itemtemplate templatefield

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

ASPX:

 <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”)始终返回null。但是,如果我删除第二个ItemTemplate,它工作正常。但是你看,其中一行有不同的列(gvMotorQuoteItems)。我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

您不能两次覆盖同一模板。但是,您可以在模板中使用两个placeholeholders,并通过Visible属性显示/隐藏相关的一个{。}}。

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