Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Fixing the RadioButtonList ASP.NET control - adding Attributes to Items

RadioButonListItemAttributes Web Control

By Zarko Gajic, About.com

When you use the RadioButtonList Web Server ASP.NET control, if you add custom attributes to an Item, the key-value pairs are not rendered to the result page. Here's a "fixed" version of the RadioButtonList - the one rendering Item attributes.

RadioButonListItemAttributes

Here's the fixed RadioButtonList, the one that actually renders its items attributes (by overriding the Render method and making sure each Item attributes are rendered) :

~~~~~~~~~~~~~~~~~~~~~~~~~
unit aspxDelphiWCL.RadioButonListItemAttributes ;

interface

uses
   System.Web.UI, System.Web.UI.WebControls, System.IO, System.Text,
   System.ComponentModel;

type
   /// <summary>
   /// RadioButonListItemAttributes renders List Item attributes
   /// </summary>
   [ToolboxData('<{0}:RadioButonListItemAttributes runat=server></{0}:RadioButonListItemAttributes>')]
   RadioButonListItemAttributes = class(System.Web.UI.WebControls.RadioButtonList)
   strict protected
     procedure Render(writer: HtmlTextWriter) ; override;
   end;

implementation

procedure RadioButonListItemAttributes.Render(writer: HtmlTextWriter) ;
var
   strAttributes: string;
   j: Integer;
   liHtmlTW: System.Web.UI.HtmlTextWriter;
   liSW: System.IO.StringWriter;
   liSB: System.Text.StringBuilder;
   endIndex: Integer;
   index: Integer;
   fixedHTML: string;
   htmlTW: System.Web.UI.HtmlTextWriter;
   SW: System.IO.StringWriter;
   SB: System.Text.StringBuilder;
begin
   SB := System.Text.StringBuilder.Create;
   SW := System.IO.StringWriter.Create(SB) ;
   htmlTW := System.Web.UI.HtmlTextWriter.Create(SW) ;
   inherited Render(htmlTW) ;
   fixedHTML := SB.ToString;

   liSB := System.Text.StringBuilder.Create;
   liSW := System.IO.StringWriter.Create(liSB) ;
   liHtmlTW := System.Web.UI.HtmlTextWriter.Create(liSW) ;
   j := 0; index := 0;
   while (j <= (Self.Items.Count - 1)) do
   begin
     if (Self.Items[j].Attributes['Visible'] = 'False') then
     begin
       index := fixedHTML.IndexOf('<tr>', index) ;
       endIndex := (fixedHTML.IndexOf('</tr>', index) + '</tr>'.Length) ;
       fixedHTML := fixedHTML.Remove(index, (endIndex - index)) ;
     end
     else
     begin
       index := (fixedHTML.IndexOf('<input ', index) + '<input '.Length) ;
       liSB.Remove(0, liSB.Length) ;
       Self.Items[j].Attributes.Render(liHtmlTW) ;
       strAttributes := (liSB.ToString + ' ') ;
       fixedHTML := fixedHTML.Insert(index, strAttributes) ;
     end;
     j := j + 1;
   end;
   writer.Write(fixedHTML) ;
end;

end.
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to set the DataSource property to several db-aware controls in one call
« How to handle system time change using Delphi code

More Delphi Programming Quick Tips

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. Fixing the RadioButtonList ASP.NET Web Server control - adding Attributes to Items

©2009 About.com, a part of The New York Times Company.

All rights reserved.