<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>Scott Elkin &#187; vb.net</title> <atom:link href="http://scottelkin.com/category/programming/vbnet/feed/" rel="self" type="application/rss+xml" /><link>http://scottelkin.com</link> <description>Tech, Love, Life</description> <lastBuildDate>Thu, 07 Apr 2011 23:02:52 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Render RadioButtonList as an Unordered List UL</title><link>http://scottelkin.com/aspnet/render-radiobuttonlist-as-an-unordered-list-ul/</link> <comments>http://scottelkin.com/aspnet/render-radiobuttonlist-as-an-unordered-list-ul/#comments</comments> <pubDate>Sun, 28 Jan 2007 19:44:00 +0000</pubDate> <dc:creator>Scott</dc:creator> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[vb.net]]></category><guid isPermaLink="false">/archive/2007/01/28/Render-RadioButtonList-as-an-Unordered-List-UL.aspx</guid> <description><![CDATA[In my latest project, I found the need to use the RadioButtonList which spits out the absolute nastiest HTML to render it unusable (for me anyway). The control gives you to HTML options using the RepeatLayout attribute. Flow spits out a series of span&#8217;s and label&#8217;s with BR&#8217;s if you don&#8217;t specify RepeatDirection of Horizontal. [...]No related posts.]]></description> <content:encoded><![CDATA[<p>In my latest project, I found the need to use the RadioButtonList which spits out the absolute nastiest HTML to render it unusable (for me anyway).  The control gives you to HTML options using the RepeatLayout attribute.</p><p>Flow spits out a series of span&#8217;s and label&#8217;s with BR&#8217;s if you don&#8217;t specify RepeatDirection of Horizontal. And choosing Table gives you a nicely unaccessible table you can&#8217;t format easily. Because of these limitation&#8217;s, I have always stayed away from CheckBoxList and RadioButtonList controls.</p><p>My solution is to write my own RadioButtonList, but to spit it out as an Unordered List which I can style very easily horizontally or vertically, little or lots of padding, all with CSS.</p><p>I unfortunately have been forced to regress back to ASP.NET 1.1 and VB.NET for a little while until the company upgrades to 2.0.  Hence why I wrote the following instead of writing a CSS Control Adapter.</p><pre><code>Imports System
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace MyControls

 &lt;DefaultProperty("Text"), _
ToolboxData("&lt;{0}:ULRadioButtonList runat=server /&gt;") _
, Description("Creates a RadioButtonList using an Unordered List.")&gt; _
Public Class ULRadioButtonList
Inherits RadioButtonList

 Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

//"SBE 01/28/2007:  just in case...not sure if this is needed or not
Controls.Clear()

//"SBE 01/28/2007:  not sure why I am leaving quotes as a param...I was just undecided if I would ever need to use " instead of "
Dim inputFormatString As String = "&lt;input id={0}{1}{0} name={0}{2}{0} type={0}radio{0} value={0}{3}{0} {4} /&gt;"
Dim labelFormatString As String = "&lt;label for={0}{1}{0}&gt;{2}&lt;/label&gt;"

//"SBE 01/28/2007:  if user sets the cssclass, add it to the &lt;ul&gt; tag
If (Not MyBase.CssClass Is Nothing AndAlso MyBase.CssClass &lt;&gt; "") Then
     writer.WriteLine("&lt;ul class="" &amp; MyBase.CssClass &amp; ""&gt;")
Else
     writer.WriteLine("&lt;ul&gt;")
End If

//"SBE 01/28/2007:  loop through the dataitems
For index As Integer = 0 To Items.Count - 1
     writer.Indent += 1
     writer.WriteLine("&lt;li&gt;")
     writer.Indent += 1

     Dim inputBuilder As New StringBuilder
</code><code>     </code><code>Dim labelBuilder As New StringBuilder
</code><code>     </code><code>Dim checked As String = ""

</code><code>     </code><code>If (Items(index).Selected) Then
     </code><code>     </code><code>checked = "checked"
</code><code>     </code><code>End If

</code><code>     </code><code>inputBuilder.AppendFormat(inputFormatString, """", MyBase.ClientID &amp; "_" &amp; index.ToString(), MyBase.UniqueID, Items(index).Value, checked)
</code><code>     </code><code>labelBuilder.AppendFormat(labelFormatString, """", MyBase.ClientID &amp; "_" &amp; index.ToString(), Items(index).Text)

</code><code>     </code><code>writer.WriteLine(inputBuilder.ToString())
</code><code>     </code><code>writer.WriteLine(labelBuilder.ToString())

</code><code>     </code><code>writer.Indent -= 1

</code><code>     </code><code>writer.WriteLine("&lt;/li&gt;")
</code><code>     </code><code>writer.WriteLine()
</code><code>     </code><code>writer.Indent -= 1

 Next

 writer.WriteLine("&lt;/ul&gt;")

 End Sub

 End Class

End Namespace

</code></pre><p>Before I started writing this, I found a beautiful website <a href="http://www.nikhedonia.com/notebook/entry/render-checkboxlist-as-an-unordered-list/">SimplyGold</a> who had already wrote something similar for the CheckBoxList. Thanks for saving me some time!</p><p><strong>Now playing:</strong> <a href="http://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?artistTerm=Menomena">Menomena</a> &#8211; <a href="http://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?songTerm=Muscle%20n%27%20Flo&amp;artistTerm=Menomena">Muscle n&#8217; Flo</a></p><p>No related posts.</p>]]></content:encoded> <wfw:commentRss>http://scottelkin.com/aspnet/render-radiobuttonlist-as-an-unordered-list-ul/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: scottelkin.com @ 2012-02-05 13:06:46 -->
