Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't find a GridView inside a template of a custom templated web control

Reply
Thread Tools

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

 
 
studio60podcast@gmail.com
Guest
Posts: n/a
 
      09-03-2007
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      09-04-2007
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



"(E-Mail Removed)" 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
>
>

 
Reply With Quote
 
 
 
 
studio60podcast@gmail.com
Guest
Posts: n/a
 
      09-04-2007
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Access to Container.DataItem From Custom Templated Control within DataGrid Template Earl Teigrob ASP .Net 0 07-07-2004 02:09 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments