Control with simple ArrayList of children - parse as objects, not controls

T

Todd

OK. This shouldn't be that hard. I've poured over many more complex
examples but still failed to distill them down to this simpler
problem.


I want to take the following aspx page syntax:

<my:ClientDictionary id="cd1" runat="server">
<Moniker Key="My dictionary entry" />
<Moniker Key="Another dictionary entry" />
</my:ClientDictionary>


and output translations of the keys supplied. I've already got a
translation dictionary that I can lookup the entries in and translate
into a number of languages: i.e. I would render "My dictionary entry"
in Spanish or Italian or German.

So I want to take this input, parse the children as members of a
simple collection, then output the collection as a client-side
javascript array. That part is trivial. Right now you can see that
my Render method is just trying to render the text to the page.

The crux of my problem seems to be that the monikers get parsed as
generic html controls. The code follows. Any help would be
appreciated.

Todd


using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.ComponentModel;

namespace My.Web.UI.WebControls
{
public class Moniker
{
private string key;

public string Key
{
get {return key;}
set {key = value;}
}
}


public class ClientDictionaryControlBuilder : ControlBuilder
{
public override Type GetChildControlType(String tagName, IDictionary
attributes)
{
// if the child tag is <Moniker>, return a Moniker object type
if (String.Compare(tagName, "moniker", true) == 0)
{
return typeof(Moniker);
}

return null;
}
}


[
DefaultProperty("Monikers"),
ParseChildren(true, "Monikers"),
ControlBuilderAttribute(typeof(ClientDictionaryControlBuilder))
]
public class ClientDictionary : Control
{
private ArrayList monikers = new ArrayList();



[
Category("ClientDictionary"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty)
]
public ArrayList Monikers
{
get {return monikers;}
set {monikers = value;}
}



protected override void AddParsedSubObject(Object obj)
{
// on a <Parameter> tag...
if (obj is Moniker) monikers.Add(obj);
}


protected override void Render(HtmlTextWriter writer)
{
System.Collections.IEnumerator e = monikers.GetEnumerator();
foreach(Moniker m in monikers)
{
writer.WriteLine(m.Key + "<br>");
}
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top