469,609 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

TreeView in Custom User Control RenderControl error

Hi all-

I'm trying to Render a TreeView in a custom control, but I'm receiving
a NullReferenceException at runtime. If I put the TreeView on the page
it works fine, but as soon as I try to render it within my control it
crashes and burns. I've even tried placing it within another control
(a PlaceHolder) and tried to render the PlaceHolder to no avail. Any
ideas on how to fix this problem?

Here's my code:

ASPX Page
================================================== ======
<%@ Page MasterPageFile="~/presentationLayer/General/_Master.master"
Language="C#" AutoEventWireup="true" CodeFile="ViewSchoolUsers.aspx.cs"
Inherits="presentationLayer_User_ViewSchoolUsers" Theme="Gray" %>
<%@ Register TagPrefix="MadeIt"
Namespace="MadeItUtils.UI.WindowControls" Assembly="MadeItUtils" %>

<asp:Content ID="content" ContentPlaceHolderID="cphMain"
runat="Server">

<MadeIt:TreeWindow ID="winSchoolTree" UseSolidBackground="false"
Width="200" Height="300" runat="server"></MadeIt:TreeWindow>

<asp:PlaceHolder ID="phTreeView" runat="server"></asp:PlaceHolder>

</asp:Content>

ASPX CodeBehind
================================================== ======
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class presentationLayer_User_ViewSchoolUsers :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.initializeControls();
}
}

private void initializeControls()
{
TreeView tree = new TreeView();
tree.SkipLinkText = String.Empty;

tree.NodeStyle.NodeSpacing = 2;
tree.NodeStyle.HorizontalPadding = 3;

tree.Nodes.Add(new TreeNode("TEST 1", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes.Add(new TreeNode("TEST 2", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));

tree.Nodes[0].ChildNodes.Add(new TreeNode("TEST 1.1", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[0].ChildNodes.Add(new TreeNode("TEST 1.2", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[0].ChildNodes.Add(new TreeNode("TEST 1.3", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[0].ChildNodes.Add(new TreeNode("TEST 1.4", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[0].ChildNodes.Add(new TreeNode("TEST 1.5", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));

tree.Nodes[0].ChildNodes[2].ChildNodes.Add(new TreeNode("TEST
1.3.1", "0", ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[0].ChildNodes[2].ChildNodes.Add(new TreeNode("TEST
1.3.2", "0", ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));

tree.Nodes[1].ChildNodes.Add(new TreeNode("TEST 2.1", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[1].ChildNodes.Add(new TreeNode("TEST 2.2", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[1].ChildNodes.Add(new TreeNode("TEST 2.3", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[1].ChildNodes.Add(new TreeNode("TEST 2.4", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));
tree.Nodes[1].ChildNodes.Add(new TreeNode("TEST 2.5", "0",
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/Icons/imgFolder.gif"));

this.winSchoolTree.Tree = tree;
}
}
Custom Control (TreeWindow.cs)
================================================== ===============
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OncallUtils;

namespace MadeItUtils.UI.WindowControls
{
public class TreeWindow : Window
{
public TreeWindow()
{
}

#region protected override void Render(HtmlTextWriter output)
/// <summary>
/// Overrides the Page Render method. Renders the control to
the page.
/// </summary>
/// <param name="output">HtmlTextWriter</param>
protected override void Render(HtmlTextWriter output)
{
try
{
// Get the width of the stretch
int intStretchWidth = this.Width - 30;
int intStretchWidthMiddle = this.Width - 22;
int intStretchHeight = this.Height - 43;

this.getHtmlWrapStart(intStretchWidth,
intStretchWidthMiddle, intStretchHeight);

this.addHtml("<table cellspacing=\"0\"
cellpadding=\"0\" border=\"0\" style=\"width: 100%;\">");
this.addHtml("<tr>");
this.addHtml("<td align=\"" + this.ContentTextAlign +
"\" valign=\"top\">");
this.addHtml("<span class=\"" + this.ContentCssClass +
"\">" + this.Content + "</span>");
this.addHtml("</td>");
this.addHtml("</tr>");
this.addHtml("<tr>");
this.addHtml("<td align=\"left\" valign=\"top\">");
this.addHtml("<img src=\"" +
ConfigurationManager.AppSettings["THEME_ROOT"] +
"Gray/Images/spacer.gif\" width=\"1\" height=\"20\" alt=\"0\"
style=\"border: 0px;\"></td>");
this.addHtml("</td>");
this.addHtml("</tr>");
this.addHtml("<tr>");
this.addHtml("<td align=\"center\"
valign=\"middle\">");
this.addHtml("<table cellspacing=\"0\"
cellpadding=\"0\" border=\"0\">");
this.addHtml("<tr>");

this.addHtml(this.renderTree());

this.addHtml("</td>");
this.addHtml("</tr>");
this.addHtml("</table>");
this.addHtml("</td>");
this.addHtml("</tr>");
this.addHtml("</table>");

this.getHtmlWrapEnd(intStretchWidth,
intStretchWidthMiddle, intStretchHeight);

output.Write(this.getHtml());
}
catch (System.Exception exp)
{
throw new System.Exception(exp.Message, exp);
}
}
#endregion

#region Getters/Setters

private TreeView _tree = new TreeView();
public TreeView Tree
{
get { return this._tree; }
set { this._tree = value; }
}

#endregion

#region Methods

#region private string renderTree()
private string renderTree()
{
StringBuilder sboTree = new StringBuilder();

if (this._tree != null)
{
StringBuilder result = new StringBuilder();

this._tree.RenderControl(new HtmlTextWriter(new
StringWriter(result)));

sboTree.Append(result.ToString());
}

return sboTree.ToString();
}
#endregion

#endregion
}
}
Thanks in advance for any help!

Nov 7 '06 #1
0 2308

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pham Nguyen | last post: by
reply views Thread by zoneboy | last post: by
7 posts views Thread by Jonas | last post: by
1 post views Thread by doemon | last post: by
1 post views Thread by GroupReader | last post: by
1 post views Thread by GroupReader | last post: by
reply views Thread by Solution2021 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.