TreeView in Custom User Control RenderControl error

A

apenly

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top