Mega Code Archive

 
Categories / Delphi / .NET
 

Fixing the RadioButtonList ASP.NET Web Server control adding Attributes to Items

Title: Fixing the RadioButtonList ASP.NET Web Server control - adding Attributes to Items 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 /// /// RadioButonListItemAttributes renders List Item attributes /// [ToolboxData('')] 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 begin if (Self.Items[j].Attributes['Visible'] = 'False') then begin index := fixedHTML.IndexOf('', index) ; endIndex := (fixedHTML.IndexOf('', index) + ''.Length) ; fixedHTML := fixedHTML.Remove(index, (endIndex - index)) ; end else begin index := (fixedHTML.IndexOf(' 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. ~~~~~~~~~~~~~~~~~~~~~~~~~