S
studio60podcast
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
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