473,221 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 2452

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow...
0
by: MarkD | last post by:
I have an ASP.NET (VB.NET) application that calls all VB6 COM DLL via Interop. The DLL uses functionality contained in a Custom OCX Control (Also VB6) that in turn contains a standard TreeView...
0
by: zoneboy | last post by:
I'm doing a somewhat complex page with client side postbacks, and I'm rendering a control with a TreeView on it in code. What I've found is that if I embed the control directly and populate the...
2
by: Pipo | last post by:
Nobody knows how to get the values provided in the client can be read in the user-control? If have made a Web Custom Control with 2 textboxes and 1 dropdownlist. The user fills in my control (the...
7
by: Jonas | last post by:
Hi. I'm trying to develop a web custom control that uses a programmatically created treeview. My problem is that I get an exception when I try to render the control. With properties do I have to...
1
by: doemon | last post by:
Hi, I'm working on a pagination control and I need to dynamically rerender a treeview to display the next set of nodes depending on which page we're on. For example, page 1 will dispaly only...
1
by: GroupReader | last post by:
When doing a "callback" I normally call a webControl's RenderControl method to get the html for that WebControl. This has always worked great in the past, but it does not seem to work with an...
1
by: GroupReader | last post by:
When doing a "callback" I normally call a webControl's RenderControl method to get the html for that WebControl. This has always worked great in the past, but it does not seem to work with an...
5
by: rajdevramasamy | last post by:
Hi, In my webpage, i am adding my user control using rendercontrol() using the following code: Dim NumericEditControl As New Control NumericEditControl =...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.