473,379 Members | 1,260 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,379 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 2460

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 =...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.