Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem: Custom Control - ReferenceConverter

0 views
Skip to first unread message

Eric Stoll

unread,
Oct 11, 2001, 4:53:44 PM10/11/01
to
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.

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
}
}
}

Drew Curry

unread,
Nov 21, 2001, 10:45:50 AM11/21/01
to
Hi Eric, i am receiving the same sort of error simply trying to maintain
a dataset that was bound to a datagrid on a webform. Did you solve this?
I am trying to keep the dataset to allow local manipulation until the
user is satified with changes made to the dataset, then i want to submit
it back to my business object for processing.. But the form-level
dataset is set back to nothing after the page gets control again..

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}

Eric Stoll

unread,
Nov 21, 2001, 11:10:57 AM11/21/01
to
First. You cannot store a DataSet in the ViewState.
However, you can store the DataGrid in the ViewState. Actually, this happens automatically. If you lookup "Control Execution Lifecycle" on MSDN you can see how this works. Just before the control is rendered, the state of each child control is saved to the ViewState. When the page is requested again, the state of these controls is restored after the Initialize event. This is why you do not have to repopulate the DataSet each time the page is requested just to maintain the data in the DataGrid. The problem is; however, that each time you execute the DataBind() method of the DataGrid, the DataGrid needs a datasource. Therefore, each time you DataBind() the DataGrid, your DataSet needs to be populated. By repopulating the DataSet at these intervals, you are also guaranteeing data concurrency. You can write an algorithm to make sure the data from the repopulated DataSet is the same as what you had in your DataGrid and if it changes, you must notify the user and handle the problem. I haven't built data concurrency into my application yet so I cannot give you an exact solution to this problem; however, this is how you would deal with it (lookup Optimistic Concurrency)

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!

Jonathan Wells [msft]

unread,
Nov 21, 2001, 12:36:33 PM11/21/01
to
Hi Drew

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...

Eric Stoll

unread,
Nov 21, 2001, 12:46:55 PM11/21/01
to
Data caching is a nice tool; however, it is only private for the application. If the data you are viewing the specific to that Session, then caching is not the answer. You could use the Session collection; however, there are some performance issues to consider and you should do some research before making your final decision as to what solution will provide the best performance.
0 new messages