Search code examples
c#itemtemplatetemplatefield

Two Item templates in one Template field?


Is it possible to have two item templates in one template field? This is my current code and its not working because when I run it e.Row.FindControl("gvQuoteItems") always returns 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>

Code-behind:

  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();
                }
            }

When I try to run the above as it is, e.Row.FindControl("gvQuoteItems") always returns null. But if I remove the second ItemTemplate, it works fine. But you see, one of the rows has different columns (gvMotorQuoteItems). How can I go about this?


Solution

  • You cannot override the same template two times. However you can have two placeholeholders within the template and show/hide only the relevant one via Visible property.

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