473,320 Members | 1,881 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,320 software developers and data experts.

ASP.NET - Preserve child controls state

Howdy eXperts!

I have this problem w/ my ASP.NET control. The control is a menu with the
following structure:

Menu
Group 1
Item 1
Item 2
Item 3
Group 2
Item 4
Item 5

etc...

What I want is to be able to add controls to any Item. I'm able to do that
but the state is lost and any events fired by those controls could not be
handled correctly.

Here's some of the code (stripped for simplicity):

[assembly:TagPrefix("Menu123.WebControls", "Menu123")]
namespace Menu123.WebControls
{
internal class MyMenuBuilder : ControlBuilder
{
public override bool AllowWhitespaceLiterals()
{
return false;
}
public override Type GetChildControlType(string tagName, IDictionary
attribs)
{
if (tagName.ToLower() == "MyMenugroup" || tagName.ToLower() ==
"Menu123:MyMenugroup")
{
return typeof(MyMenuGroupControl);
}
return null;
}
}

[ToolboxData("<{0}:MyMenu id=MyMenu1 runat=server></{0}:MyMenu>")]
[Designer("Menu123.WebControls.MyMenuDesigner")]
[ControlBuilderAttribute(typeof(MyMenuBuilder))]
[ParseChildren(false)]
public class MyMenu : WebControl, IPostBackEventHandler, INamingContainer
{
private ArrayList _MenuStructure = new ArrayList();
[Browsable(false)]
public ArrayList MenuStructure
{
get
{
return _MenuStructure;
}
set
{
_MenuStructure = value;
}
}

protected override void AddParsedSubObject(object obj)
{
if (obj is MyMenuGroupControl)
{
MenuStructure.Add(obj);
}
}

protected override void Render(HtmlTextWriter output)
{
foreach (MyMenuGroupControl _group in MenuStructure)
{
// Render the group

foreach (MyMenuItemControl _item in _group.GroupStructure)
{
// Render the items

// Render the nested controls here !!! (e.g. add them to table cell,
etc)
}
}
}

protected override object SaveViewState()
{
object baseState = base.SaveViewState();

object[] state = new object[2];
state[0] = (baseState != null) ? baseState : null;
if (MenuStructure != null && MenuStructure.Count > 0)
{
state[1] = new object[MenuStructure.Count];
for (int i = 0; i < MenuStructure.Count; i++)
{
object[] g_state = new object[2];
if (_group.GroupStructure != null && _group.GroupStructure.Count > 0)
{
g_state[0] = new object[_group.GroupStructure.Count];
for (int n = 0; n < _group.GroupStructure.Count; n++)
{
object[] i_state = null;
MyMenuItemControl _item =
(MyMenuItemControl)_group.GroupStructure[n];
if (_item != null)
{
i_state = new object[1];
i_state[0] = _item.Label;
((object[])g_state[0])[n] = i_state;
}
}
}

g_state[1] = _group.Label;
((object[])state[1])[i] = g_state;
}
}
return state;
}

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] state = (object[])savedState;

base.LoadViewState(state[0]);
if (state[1] != null)
{
MenuStructure.Clear();
foreach (object[] g_state in (object[])state[1])
{
if (g_state == null) continue;
MyMenuGroupControl _group = new MyMenuGroupControl();
if (g_state[0] != null)
{
foreach (object[] i_state in (object[])g_state[0])
{
if (i_state == null) continue;
MyMenuItemControl _item = new MyMenuItemControl();
_item.Label = (string)i_state[0];
_group.GroupStructure.Add(_item);
}
}
_group.Label = (string)g_state[1];
MenuStructure.Add(_group);
}
}
}
}
}
[System.ComponentModel.ToolboxItem(false)]
internal class MyMenuGroupBuilder : ControlBuilder
{
public override bool AllowWhitespaceLiterals()
{
return false;
}
public override Type GetChildControlType(string tagName, IDictionary
attribs)
{
if (tagName.ToLower() == "MyMenuitem" || tagName.ToLower() ==
"Menu123:MyMenuitem")
{
return typeof(MyMenuItemControl);
}
return null;
}
}

[ControlBuilderAttribute(typeof(MyMenuGroupBuilder) )]
[System.ComponentModel.ToolboxItem(false)]
[ParseChildren(false)]
public class MyMenuGroupControl : WebControl
{
public MyMenuGroupControl() {}

protected override void AddParsedSubObject(object obj)
{
if (obj is MyMenuItemControl)
{
GroupStructure.Add(obj);
}
}

private ArrayList _GroupStructure = new ArrayList();
/// <summary>
/// Use this container to programaticaly add Items in the group.
/// </summary>
[Browsable(false)]
public ArrayList GroupStructure
{
get { return _GroupStructure; }
set { _GroupStructure = value; }
}

private string _Label;
/// <summary>
/// Use this property to specify the group label.
/// </summary>
public string Label
{
get { return _Label; }
set { _Label = value; }
}
}

[System.ComponentModel.ToolboxItem(false)]
[ParseChildren(true, "ItemTemplate")]
[PersistChildren(false)]
public class MyMenuItemControl : WebControl, INamingContainer
{
public MyMenuItemControl()
{
_ItemTemplate = new ControlCollection(this);
}
private ControlCollection _ItemTemplate;

[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty) ]
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)]
public ControlCollection ItemTemplate
{
get { return _ItemTemplate; }
}

private string _Label;
/// <summary>
/// Use this property to specify the item label.
/// </summary>
public string Label
{
get { return _Label; }
set { _Label = value; }
}
}
}

Nov 18 '05 #1
0 1054

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

Similar topics

8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
3
by: James Spibey | last post by:
Hi, I have an MDI application which has aboout 10 child windows. The way the app needs to work is that only one window should be visible at a time and it should be maximized within the parent...
3
by: Arulraja | last post by:
Hello, I have created 2 custom server controls, The parent custom control contains multiple child custom controls. The Child control has a button on it. If I Click the button control, it...
3
by: Vinicius Bellino | last post by:
Hi All, Is there a way to change a property of an UI control, such as the enabled property of a button, in a child thread? I mean, my code runs a long process, and because I wanted to show the...
3
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
0
by: ChrisMiddle10 | last post by:
Hey, I am developing a fairly simple custom web control. I am, however, having a major problem understanding how to have any child controls maintain state. For instance, in my particular control...
3
by: Maximiliano | last post by:
Hello, I have an asp.net project that calculates a general tax. Ok, this tax is a big object formed with another child objects (as a mather of fact 15 another child object within it), like Ship,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.