UserControl output into an XmlNode

O

Owen Blacker

An awkward question.

In a CMS-like environment, I have a list of items that is generated on
the serverside using an XmlDocument to throw together XHTML tags
(mainly because it's substantially easier than using a Repeater, but
the why is a moot point as the project doesn't have the budget for me
to change that).

Now I have a requirement to add a new list item at the end of each list
that is an "Add new item" form. It's very easy for me to write a
UserControl to handle this form action, but would be relatively
difficult to write this form bit as XmlElements to add into the
existing XmlNode tree.

Is there a way for me to get the output of the server control as an
XmlNode and squish that into my XmlNode tree?

Effectively, I want to take my existing code and making it look
something like this:

bool isInAuthoringMode;
XmlDocument xml;
XmlElement div;

5 // ...

if (isInAuthoringMode || (this.Assets != null &&
this.Assets.Count > 0)
{
10 XmlElement ul = xml.CreateElement("ul");
ul.Attributes.Append(xml.CreateAttribute("class"));
ul.Attributes["class"].Value = "file-list";

foreach (Asset a in this.Assets)
15 ul.AppendChild(a.ToXml(xml));

if (isInAuthoringMode)
ul.AppendChild(new EditControl().ToXml(xml));

20 div.AppendChild(ul);
}

Line 18 in this code block is what I'd really like to be able to do,
but I know that would be being very optimistic.

Is there any obvious way of getting "the HTML this control would
generate here" out of it, even if I have to write a new method into it
somewhere?

The behavior I seek is roughly the same as if I had a Repeater like
this:

<asp:Repeater ...>
<ItemTemplate>
<!-- here would be the output of Asset.ToXml -->
</ItemTemplate>
<FooterTemplate>
<My:EditControl .../>
</FooterTemplate>
</asp:Repeater>

But I can't use a Repeater (for reasons too tedious to detail here).

Any ideas gratefully received. If you could cc: me on any replies,
that'd be very cool, as I only get the digest version of this list.
And apologies to anyone who already read this on any of the LiveJournal
communities I just asked too ;o)

Thanks!


Owen
 
O

Owen Blacker

Ok, I don'think I'm gonna find a convenient way to do it the way I'd
hoped, so I've started thinking about rendering the control into a
Stream and XmlDocument.Load-ing that Stream into my XmlNode hierarchy.

Except I'm not getting anything out using
Control.RenderControl(TextWriter):

public XmlDocument RenderXmlDocument()
{
MemoryStream stream = new MemoryStream();

this.RenderControl(new HtmlTextWriter(new StreamWriter(stream)));

byte[] streamContents = stream.ToArray();
System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach (byte tmp in streamContents)
sb.Append(Convert.ToChar(tmp));

HttpContext.Current.Trace.Write("EditSectionOrAsset",
"Rendered control as \n\n[" + sb.ToString() + "]");

XmlDocument control = new XmlDocument();
control.Load(new XmlTextReader(new StreamReader(stream)));

return control;
}

and nothing is getting written to the Trace.

Am I gonna have to work out how to do this without using the control?

Any and all suggestions gratefully received.


Owen
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top