Can't find a GridView inside a template of a custom templated web control

Discussion in 'ASP .Net' started by studio60podcast@gmail.com, Sep 3, 2007.

  1. Guest

    This is my first Templated Web Control I've written and I hope someone
    can help.

    I have three templates inside my web control and I'm trying to find a
    GridView inside one of them. Naturally, it's throwing a null
    reference exception. Here is the code for my user control:



    1 using System;
    2 using System.Collections.Generic;
    3 using System.ComponentModel;
    4 using System.Text;
    5 using System.Web;
    6 using System.Web.UI;
    7 using System.Web.UI.WebControls;
    8
    9 namespace MyWebControls
    10 {
    11 [ToolboxData("&lt;{0}:SectionGroup runat=server></
    {0}:SectionGroup>"),
    12 ParseChildren(true),
    13 DesignTimeVisible(true)]
    14 public class SectionGroup : WebControl, INamingContainer
    15 {
    16 private ITemplate _imageTemplate = null;
    17 private ITemplate _headerTemplate = null;
    18 private ITemplate _contentTemplate = null;
    19 private PlaceHolder _imagePlaceHolder;
    20 private PlaceHolder _headerPlaceHolder;
    21 private PlaceHolder _contentPlaceHolder;
    22
    23 #region Contructors
    24 public SectionGroup()
    25 {
    26 _imagePlaceHolder = new PlaceHolder();
    27 _headerPlaceHolder = new PlaceHolder();
    28 _contentPlaceHolder = new PlaceHolder();
    29 this.Controls.Add(_imagePlaceHolder);
    30 this.Controls.Add(_headerPlaceHolder);
    31 this.Controls.Add(_contentPlaceHolder);
    32 }
    33 #endregion
    34
    35 [PersistenceMode(PersistenceMode.InnerProperty),
    36 TemplateContainer(typeof(Template))]
    37 public ITemplate Image { get { return _imageTemplate; } set
    { _imageTemplate = value; } }
    38
    39 [PersistenceMode(PersistenceMode.InnerProperty),
    40 TemplateContainer(typeof(Template))]
    41 public ITemplate Header { get { return _headerTemplate; } set
    { _headerTemplate = value; } }
    42
    43 [PersistenceMode(PersistenceMode.InnerProperty),
    44 TemplateContainer(typeof(Template))]
    45 public ITemplate Contents { get { return _contentTemplate; }
    set { _contentTemplate = value; } }
    46
    47 public string HeaderText
    48 {
    49 get { return ViewState["header_text"] != null ?
    (string)ViewState["header_text"] : String.Empty; }
    50 set { ViewState["header_text"] = value; }
    51 }
    52
    53 #region Overridden Page Methods
    54 protected override void CreateChildControls()
    55 {
    56 if (Image != null)
    57 {
    58 _imagePlaceHolder.Controls.Clear();
    59 Template i = new Template();
    60 Image.InstantiateIn(i);
    61 _imagePlaceHolder.Controls.Add(i);
    62 }
    63
    64 if (Header != null)
    65 {
    66 _headerPlaceHolder.Controls.Clear();
    67 Template i = new Template();
    68 Header.InstantiateIn(i);
    69 _headerPlaceHolder.Controls.Add(i);
    70 }
    71
    72 if (Contents != null)
    73 {
    74 _contentPlaceHolder.Controls.Clear();
    75 Template i = new Template();
    76 Contents.InstantiateIn(i);
    77 _contentPlaceHolder.Controls.Add(i);
    78 }
    79
    80 base.CreateChildControls();
    81 }
    82 #endregion
    83
    84 protected override void RenderContents(HtmlTextWriter output)
    85 {
    86 //I output all of the code here...
    87 }
    88 }
    89 }
    90
    Here's my markup to create the control:

    1 <mm:SectionGroup ID="sgRecentDocuments" runat="server"
    HeaderText="Recently Added Documents">
    2 <Contents>
    3 <asp:GridView ID="ucxRecentlyAddedDocuments" runat="server"
    AllowPaging="false" AllowSorting="false" DataKeyNames="documentid"
    OnRowCommand="gvDocuments_RowCommand">
    4 <Columns>
    5 <asp:TemplateField HeaderText="Title / Description"
    SortExpression="Title">
    6 <ItemTemplate>
    7 <div class="gridViewRowTitle">
    8 <asp:LinkButton ID="lbView" runat="server"
    CommandArgument='<%# Eval("DocumentID") %>' CommandName="View"
    ToolTip="View Document"><%# Eval("Title") %></asp:LinkButton></div>
    9 <div class="gridViewRowDescription">
    10 <%# Eval("Description") %>
    11 </div>
    12 </ItemTemplate>
    13 </asp:TemplateField>
    14 <asp:TemplateField HeaderText="Date Added" ItemStyle-
    CssClass="rightAlign" ItemStyle-Width="75px">
    15 <ItemTemplate>
    16 <%# ((DateTime)Eval("DateAdded")).ToShortDateString()%>
    17 </ItemTemplate>
    18 </asp:TemplateField>
    19 <asp:BoundField DataField="FileSizeString" HeaderText="File
    Size" ItemStyle-CssClass="rightAlign" ItemStyle-Width="65px" />
    20 </Columns>
    21 </asp:GridView>
    22 </Contents>
    23 </mm:SectionGroup>



    What have I missed?

    Thanks!

    Jason
     
    , Sep 3, 2007
    #1
    1. Advertising

  2. RE: Can't find a GridView inside a template of a custom templated web

    Well, for starters - all those line numbers aren't going to make it any
    easier for somebody to put your control on a page and try to see what's wrong.
    -- Peter
    Recursion: see Recursion
    site: http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    BlogMetaFinder: http://www.blogmetafinder.com



    "" wrote:

    > This is my first Templated Web Control I've written and I hope someone
    > can help.
    >
    > I have three templates inside my web control and I'm trying to find a
    > GridView inside one of them. Naturally, it's throwing a null
    > reference exception. Here is the code for my user control:
    >
    >
    >
    > 1 using System;
    > 2 using System.Collections.Generic;
    > 3 using System.ComponentModel;
    > 4 using System.Text;
    > 5 using System.Web;
    > 6 using System.Web.UI;
    > 7 using System.Web.UI.WebControls;
    > 8
    > 9 namespace MyWebControls
    > 10 {
    > 11 [ToolboxData("<{0}:SectionGroup runat=server></
    > {0}:SectionGroup>"),
    > 12 ParseChildren(true),
    > 13 DesignTimeVisible(true)]
    > 14 public class SectionGroup : WebControl, INamingContainer
    > 15 {
    > 16 private ITemplate _imageTemplate = null;
    > 17 private ITemplate _headerTemplate = null;
    > 18 private ITemplate _contentTemplate = null;
    > 19 private PlaceHolder _imagePlaceHolder;
    > 20 private PlaceHolder _headerPlaceHolder;
    > 21 private PlaceHolder _contentPlaceHolder;
    > 22
    > 23 #region Contructors
    > 24 public SectionGroup()
    > 25 {
    > 26 _imagePlaceHolder = new PlaceHolder();
    > 27 _headerPlaceHolder = new PlaceHolder();
    > 28 _contentPlaceHolder = new PlaceHolder();
    > 29 this.Controls.Add(_imagePlaceHolder);
    > 30 this.Controls.Add(_headerPlaceHolder);
    > 31 this.Controls.Add(_contentPlaceHolder);
    > 32 }
    > 33 #endregion
    > 34
    > 35 [PersistenceMode(PersistenceMode.InnerProperty),
    > 36 TemplateContainer(typeof(Template))]
    > 37 public ITemplate Image { get { return _imageTemplate; } set
    > { _imageTemplate = value; } }
    > 38
    > 39 [PersistenceMode(PersistenceMode.InnerProperty),
    > 40 TemplateContainer(typeof(Template))]
    > 41 public ITemplate Header { get { return _headerTemplate; } set
    > { _headerTemplate = value; } }
    > 42
    > 43 [PersistenceMode(PersistenceMode.InnerProperty),
    > 44 TemplateContainer(typeof(Template))]
    > 45 public ITemplate Contents { get { return _contentTemplate; }
    > set { _contentTemplate = value; } }
    > 46
    > 47 public string HeaderText
    > 48 {
    > 49 get { return ViewState["header_text"] != null ?
    > (string)ViewState["header_text"] : String.Empty; }
    > 50 set { ViewState["header_text"] = value; }
    > 51 }
    > 52
    > 53 #region Overridden Page Methods
    > 54 protected override void CreateChildControls()
    > 55 {
    > 56 if (Image != null)
    > 57 {
    > 58 _imagePlaceHolder.Controls.Clear();
    > 59 Template i = new Template();
    > 60 Image.InstantiateIn(i);
    > 61 _imagePlaceHolder.Controls.Add(i);
    > 62 }
    > 63
    > 64 if (Header != null)
    > 65 {
    > 66 _headerPlaceHolder.Controls.Clear();
    > 67 Template i = new Template();
    > 68 Header.InstantiateIn(i);
    > 69 _headerPlaceHolder.Controls.Add(i);
    > 70 }
    > 71
    > 72 if (Contents != null)
    > 73 {
    > 74 _contentPlaceHolder.Controls.Clear();
    > 75 Template i = new Template();
    > 76 Contents.InstantiateIn(i);
    > 77 _contentPlaceHolder.Controls.Add(i);
    > 78 }
    > 79
    > 80 base.CreateChildControls();
    > 81 }
    > 82 #endregion
    > 83
    > 84 protected override void RenderContents(HtmlTextWriter output)
    > 85 {
    > 86 //I output all of the code here...
    > 87 }
    > 88 }
    > 89 }
    > 90
    > Here's my markup to create the control:
    >
    > 1 <mm:SectionGroup ID="sgRecentDocuments" runat="server"
    > HeaderText="Recently Added Documents">
    > 2 <Contents>
    > 3 <asp:GridView ID="ucxRecentlyAddedDocuments" runat="server"
    > AllowPaging="false" AllowSorting="false" DataKeyNames="documentid"
    > OnRowCommand="gvDocuments_RowCommand">
    > 4 <Columns>
    > 5 <asp:TemplateField HeaderText="Title / Description"
    > SortExpression="Title">
    > 6 <ItemTemplate>
    > 7 <div class="gridViewRowTitle">
    > 8 <asp:LinkButton ID="lbView" runat="server"
    > CommandArgument='<%# Eval("DocumentID") %>' CommandName="View"
    > ToolTip="View Document"><%# Eval("Title") %></asp:LinkButton></div>
    > 9 <div class="gridViewRowDescription">
    > 10 <%# Eval("Description") %>
    > 11 </div>
    > 12 </ItemTemplate>
    > 13 </asp:TemplateField>
    > 14 <asp:TemplateField HeaderText="Date Added" ItemStyle-
    > CssClass="rightAlign" ItemStyle-Width="75px">
    > 15 <ItemTemplate>
    > 16 <%# ((DateTime)Eval("DateAdded")).ToShortDateString()%>
    > 17 </ItemTemplate>
    > 18 </asp:TemplateField>
    > 19 <asp:BoundField DataField="FileSizeString" HeaderText="File
    > Size" ItemStyle-CssClass="rightAlign" ItemStyle-Width="65px" />
    > 20 </Columns>
    > 21 </asp:GridView>
    > 22 </Contents>
    > 23 </mm:SectionGroup>
    >
    >
    >
    > What have I missed?
    >
    > Thanks!
    >
    > Jason
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Sep 4, 2007
    #2
    1. Advertising

  3. Guest

    Re: Can't find a GridView inside a template of a custom templated web

    How about being less of a smart ass and trying to be a little more
    helpful... If you don't have anything useful to say, don't say it.

    However, here is my code WITHOUT line numbers.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace MyWebControls
    {
    [ToolboxData("&lt;{0}:SectionGroup runat=server></
    {0}:SectionGroup>"),
    ParseChildren(true),
    DesignTimeVisible(true)]
    public class SectionGroup : WebControl, INamingContainer
    {
    private ITemplate _imageTemplate = null;
    private ITemplate _headerTemplate = null;
    private ITemplate _contentTemplate = null;
    private PlaceHolder _imagePlaceHolder;
    private PlaceHolder _headerPlaceHolder;
    private PlaceHolder _contentPlaceHolder;

    #region Contructors
    public SectionGroup()
    {
    _imagePlaceHolder = new PlaceHolder();
    _headerPlaceHolder = new PlaceHolder();
    _contentPlaceHolder = new PlaceHolder();
    this.Controls.Add(_imagePlaceHolder);
    this.Controls.Add(_headerPlaceHolder);
    this.Controls.Add(_contentPlaceHolder);
    }
    #endregion

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(Template))]
    public ITemplate Image { get { return _imageTemplate; }
    set
    { _imageTemplate = value; } }

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(Template))]
    public ITemplate Header { get { return
    _headerTemplate; } set
    { _headerTemplate = value; } }

    [PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(Template))]
    public ITemplate Contents { get { return
    _contentTemplate; }
    set { _contentTemplate = value; } }

    public string HeaderText
    {
    get { return ViewState["header_text"] != null ?
    (string)ViewState["header_text"] : String.Empty; }
    set { ViewState["header_text"] = value; }
    }

    #region Overridden Page Methods
    protected override void CreateChildControls()
    {
    if (Image != null)
    {
    _imagePlaceHolder.Controls.Clear();
    Template i = new Template();
    Image.InstantiateIn(i);
    _imagePlaceHolder.Controls.Add(i);
    }

    if (Header != null)
    {
    _headerPlaceHolder.Controls.Clear();
    Template i = new Template();
    Header.InstantiateIn(i);
    _headerPlaceHolder.Controls.Add(i);
    }

    if (Contents != null)
    {
    _contentPlaceHolder.Controls.Clear();
    Template i = new Template();
    Contents.InstantiateIn(i);
    _contentPlaceHolder.Controls.Add(i);
    }

    base.CreateChildControls();
    }
    #endregion

    protected override void RenderContents(HtmlTextWriter
    output)
    {
    //I output all of the code here...
    }
    }
    }

    Here's my markup to create the control:


    <mm:SectionGroup ID="sgRecentDocuments" runat="server"
    HeaderText="Recently Added Documents">
    <Contents>
    <asp:GridView ID="ucxRecentlyAddedDocuments"
    runat="server"
    AllowPaging="false" AllowSorting="false" DataKeyNames="documentid"
    OnRowCommand="gvDocuments_RowCommand">
    <Columns>
    <asp:TemplateField HeaderText="Title /
    Description"
    SortExpression="Title">
    <ItemTemplate>
    <div
    class="gridViewRowTitle">
    <asp:LinkButton
    ID="lbView" runat="server"
    CommandArgument='<%# Eval("DocumentID") %>' CommandName="View"
    ToolTip="View Document"><%# Eval("Title") %></asp:LinkButton></div>
    <div
    class="gridViewRowDescription">
    <%#
    Eval("Description") %>
    </div>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Date
    Added" ItemStyle-
    CssClass="rightAlign" ItemStyle-Width="75px">
    <ItemTemplate>
    <%#
    ((DateTime)Eval("DateAdded")).ToShortDateString()%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField
    DataField="FileSizeString" HeaderText="File
    Size" ItemStyle-CssClass="rightAlign" ItemStyle-Width="65px" />
    </Columns>
    </asp:GridView>
    </Contents>
    </mm:SectionGroup>
     
    , Sep 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    0
    Views:
    453
    Earl Teigrob
    Jul 7, 2004
  2. RA Scheltema
    Replies:
    3
    Views:
    410
    RA Scheltema
    Jan 6, 2004
  3. Marijn
    Replies:
    5
    Views:
    479
    Marijn
    Feb 13, 2004
  4. Replies:
    0
    Views:
    2,246
  5. Amadeus W. M.
    Replies:
    2
    Views:
    405
    Amadeus W. M.
    Jul 4, 2006
Loading...

Share This Page