ComposeXhtml

Modified: 2008/08/22 13:08 by admin - Uncategorized
Edit

Using ComposeXhtml in C# and Visual Basic.NET

Edit

Background

The code snippets below are used to feed the autocompleter with items based on what you type in. The ComposeXhtml function uses anonymous delegates which is only supported in C# and not VB.NET. Optionally you could split out the code in a separate function, but if you need additional data to construct the items it get's more difficult.

In the VB.NET code sample below we show you how you could accomplish using the XhtmlTagFactory to render xhtml compatible string to be used in literals anyway.

Edit

C# Code

private System.Web.UI.Control CreateItem(FileInfo inf) { System.Web.UI.WebControls.Literal lit = new System.Web.UI.WebControls.Literal(); lit.ID = inf.Name.Replace(".", "_"); lit.Text = ComposeXhtml.ToString( delegate(XhtmlTagFactory create) { using (create.Div().SetStyle("float:left;padding-right:5px;")) { using (create.Img(null, null, "media/contentImages/ajaxian.gif", "")) { } } using (create.B().WriteContent(inf.Name.Replace(".aspx", ""))) { } using (create.Br()) { } using (create.A(null, null, inf.Name).WriteContent(inf.Name)) { } }); return lit; }

Edit

Visual Basic.NET Code

Private Function CreateItem(ByVal inf As FileInfo) As LiteralControl

Dim literal As New LiteralControl

Using writer As New StringWriter

Dim factory As New XhtmlTagFactory(writer)

CreateItem(factory, inf)

literal.ID = inf.Name.Replace(".", "
_") literal.Text = writer.ToString()

End Using

Return literal

End Function

Private sub CreateItem(ByVal create As Gaia.WebWidgets.HtmlFormatting.XhtmlTagFactory, _ ByVal inf As FileInfo)

Using create.Div().SetStyle("float:left;padding-right:5px;")

Using create.Img(Nothing, Nothing, "media/contentImages/ajaxian.gif", String.Empty)

End Using

Using create.B().WriteContent(inf.Name.Replace(".aspx", ""))

End Using

Using create.Br() End Using

Using create.A(Nothing, Nothing, inf.Name).WriteContent(inf.Name)

End Using

End Using

End Sub



Edit

Adding Directly To Page

Here we use the ComposeXhtml directly to add content to the page.

C# Codebehind protected void Page_Load(object sender, EventArgs e) { Literal lit = new Literal(); lit.Text = Gaia.WebWidgets.ComposeXhtml.ToString( delegate(Gaia.WebWidgets.HtmlFormatting.XhtmlTagFactory create) { // We can store a reference to the Tag created by the creator like this using (Gaia.WebWidgets.HtmlFormatting.Tag div = create.Div()) { div.SetStyle("border:solid 1px Black;padding:5px;width:100px;"); div.WriteContent("Hello Ajax World");

using (create.Br()) { } // Or we can do everything "inline" like we do here; using (create.A(null, null, "http://www.google.com") .SetStyle("text-decoration:none;color:orange;") .WriteContent("Go to Google")) { } } }); Form.Controls.Add(lit); }


HTML Output
Hello Ajax World
Go to Google


Which ends up like this in the browser

ComposeXhtml

ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.