If I declare my custom control on an ASCX page (in the designer), everything
works perfect. State is maintained and there are no errors.
If I programatically place my custom control on the ASCX page, I get the
error:
" System.Web.HttpException: 'Mindex.Web.UI.WebControls.TreeView' uses a
ReferenceConverter and cannot be serialized in view state."
I'm pretty sure the problem is either:
1. Adding the Checkbox after the nodes
2. The Nodes property does not return the same structure when adding
TreeNodes programatically that is used when TreeNodes are added
declaritively.
Thank you so much if anyone reads through this whole thing....
Eric Stoll
Here is the ASCX file:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="SMS.WebForm1" %>
...
<Mindex:TreeView id="TreeView1" runat="server">
<Mindex:TreeNode Text="1">
<Mindex:TreeNode Text="1.1">
<Mindex:TreeNode Text="1.1.1">
</Mindex:TreeNode>
</Mindex:TreeNode>
</Mindex:TreeNode>
<Mindex:TreeNode Text="2">
</Mindex:TreeNode>
</Mindex:TreeView>
Here is a programatic example:
Page_Init() {
TreeView1 = new TreeView();
}
Page_Load() {
if(FirstLoad) {
TreeNode node = new TreeNode();
TreeNode node2 = new TreeNode();
node2.Text = "1";
TreeView1.Nodes.Add(node);
node.Text = "2";
TreeView1.Nodes.Add(node);
node = new TreeNode();
node.Text = "1.1";
node2.Nodes.Add(node);
}
}
HERE IS THE CODE FOR TreeView and TreeNode:
using System;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.Serialization;
namespace Mindex.Web.UI.WebControls
{
/// <summary>
/// Summary description for TreeView.
/// </summary>
[ControlBuilderAttribute(typeof(TreeViewControlBuilder))]
[ParseChildren(false)]
public class TreeView : System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
{
#region Attributes
#endregion
#region Contructors/Destructors
public TreeView()
{
}
~TreeView()
{
}
#endregion
#region Properties
public System.Web.UI.ControlCollection Nodes
{
get
{
return NodesControl.Controls;
}
}
private System.Web.UI.Control NodesControl
{
get
{
if(ViewState["_Nodes"] == null)
{
NodesControl = new System.Web.UI.Control();
return NodesControl;
}
return (System.Web.UI.Control)ViewState["_Nodes"];
}
set
{
ViewState["_Nodes"] = value;
}
}
#endregion
#region Methods and Implementations
protected override void CreateChildControls()
{
}
protected override void Render(HtmlTextWriter writer)
{
foreach(System.Web.UI.Control control in this.Controls)
{
control.RenderControl(writer);
}
}
#endregion
}
internal class TreeViewControlBuilder : System.Web.UI.ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary
attribs)
{
if(tagName.ToLower().EndsWith("treenode"))
return typeof(Mindex.Web.UI.WebControls.TreeNode);
return null;
}
public override void AppendLiteralString(string s)
{
// Do nothing to avoid embedded text being added to control
}
}
}
using System;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing;
namespace Mindex.Web.UI.WebControls
{
/// <summary>
/// Summary description for TreeNode.
/// </summary>
[ControlBuilderAttribute(typeof(TreeNodeControlBuilder))]
[ParseChildren(false)]
public class TreeNode : System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
{
#region Attributes
#endregion
#region Constructors and Destructors
public TreeNode()
{
}
~TreeNode()
{
}
#endregion
#region Methods and Implementations
protected override void CreateChildControls()
{
// Create Checkbox Control
CheckBox.Text = CheckBox.Text + "<br />";
this.Controls.AddAt(0, CheckBox);
}
protected override void Render(HtmlTextWriter writer)
{
foreach(System.Web.UI.Control control in this.Controls)
{
control.RenderControl(writer);
}
}
#endregion
#region Properties
public string Text
{
get
{
return CheckBox.Text;
}
set
{
CheckBox.Text = value;
}
}
public string Value
{
get
{
try
{
return (string)ViewState["_Value"];
}
catch
{
Value = "";
return Value;
}
}
set
{
ViewState["_Value"] = value;
}
}
public bool Checked
{
get
{
return CheckBox.Checked;
}
set
{
CheckBox.Checked = value;
}
}
private System.Web.UI.WebControls.CheckBox CheckBox
{
get
{
if(ViewState["_CheckBox"] != null)
{
return (System.Web.UI.WebControls.CheckBox)ViewState["_CheckBox"];
}
else
{
CheckBox = new System.Web.UI.WebControls.CheckBox();
return CheckBox;
}
}
set
{
ViewState["_CheckBox"] = value;
}
}
public System.Web.UI.ControlCollection Nodes
{
get
{
return NodesControl.Controls;
}
}
private System.Web.UI.Control NodesControl
{
get
{
if(ViewState["_Nodes"] == null)
{
NodesControl = new System.Web.UI.Control();
return NodesControl;
}
return (System.Web.UI.Control)ViewState["_Nodes"];
}
set
{
ViewState["_Nodes"] = value;
}
}
#endregion
}
internal class TreeNodeControlBuilder : System.Web.UI.ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary
attribs)
{
if(tagName.ToLower().EndsWith("treenode"))
return typeof(Mindex.Web.UI.WebControls.TreeNode);
return null;
}
public override void AppendLiteralString(string s)
{
// Do nothing to avoid embedded text being added to control
}
}
}
In article <eFKJgdpUBHA.1568@tkmsftngp05>, er...@mindex.com says...
> I am creating a TreeView control and having problems. Originally I was
> having a problem maintaining the ViewState but after a few lessons from
> Zoltan Sekeres, I am back on track with that situation. My current problem
> is the following.
>
{snip}
If you have further questions, please ask!
Good luck,
Eric Stoll
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Sounds like Data Caching is what you want. Check out:
http://gotdotnet.com/quickstart/aspplus/doc/datacaching.aspx#intro
jonathan
--
This posting is provided “AS IS” with no warranties, and confers no rights.
"Drew Curry" <dcu...@home.no.spamola.com> wrote in message
news:MPG.16656c0d6...@news.microsoft.com...