SImple ASP.NET web usercontrol to overwrite default rendered HTML

Viewed 39

I want to use a asp:RadioButton in a web form, but to render it using my own HTML rather than that that is generated by the standard control - a basic asp:RadioButton

<asp:RadioButton runat="server" ID="rTest" GroupName="rTests" Text="ABC" CssClass="someclass" />

is rendered:

<span class="someclass"><input id="rTest" type="radio" name="rTests" value="rTest" /><label for="rTest">ABC</label></span>

I want to render it thus:

 <label><input id="rTest" type="radio" name="rTests" class="someclass" > ABC</label>

so.. I wrote a simple WebUserControl to overwrite the rendering HTML (yeah, I know it's VB. Thank you.)

Public Class psuRadio
   Inherits RadioButton

   Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
      ' MyBase.RenderBeginTag(writer)
   End Sub

   Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
      ' MyBase.RenderEndTag(writer)
   End Sub

   Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
      Dim sb As New System.Text.StringBuilder
      Try
         sb.Append("<label><input class=""")
         sb.Append(CssClass)
         sb.Append(""" type=""radio""")
         sb.Append(" name=""")
         sb.Append(GroupName)
         sb.Append("""")
         sb.Append(" id=""")
         sb.Append(ID)
         sb.Append("""")
         If Checked Then sb.Append(" checked")
         sb.Append("> ")
         sb.Append(Text)
         sb.Append("</label>")
      Catch ex As Exception
         sb = New StringBuilder
         sb.Append("Error building Control:<br />")
         sb.Append(ex.Message)
      End Try
      output.Write(sb.ToString)
   End Sub

End Class

but this is ignored and it simply renders using the default RadioButton code. What am I doing wrong?

If I inherit the class from WebControl instead of RadioButton then it does render as I want, but then I have issues with persisting the properties during PostBack events - but perhaps we can save that for another day... I would like to know why I can't simply overwite the default rendering of a RadioButton control.

0 Answers
Related