locked
RadioButtonList RRS feed

  • Question

  • User-1920287721 posted

    I thought that these CSS Friendly Control Adapters could be a solution to a probelm I having with a RadioButton List. Please let me know if this is possible and if so how I would go about creating the control adapter for it.

    The standard RadioButtonList has a problem when an item has long text and it wraps to the next line. I would like it to not do that. Can these control adapters do this?

     Here's what I mean:

    Standard RadioButtonList

    O This is the start of the item and

    it wraps beneath the radio button

     

    Here's what I would like it to look like:

    O This is the start of the item and

        it wraps beneath the start of the text

     

    I hope this makes sense. I know this control usually renders as <td><input id="..." type="radio" name="..."  /><label for="...">ItemText</label></td> ... but what I would like it to render is <td><input id="..." /></td><td><label for"...">ItemText</label></td>

    Is this possible?

    Thanks

    Thursday, January 25, 2007 6:58 PM

All replies

  • User-1548791341 posted

    Matt, I know this adapter doesn't do what you're wanting to do, but hopefully you can use it as a starting point to create your own RadioButtonListAdapter with the output you want.

    Also, you might think about using DIVs instead of the TABLE TD structure. Placing the text in a floated DIV would also give you your desired behavior without the need for tables. Cheers!
     

     

    1    using System;
    2    using System.Data;
    3    using System.Collections;
    4    using System.Configuration;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11   
    12   namespace WebServices.CssFriendlyAdapters
    13   {
    14       public class RadioButtonListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
    15       {
    16           public RadioButtonListAdapter()
    17           {
    18           }
    19   
    20           protected override void RenderBeginTag(HtmlTextWriter writer)
    21           {
    22               writer.WriteBeginTag("ul");
    23               if (Control.CssClass != null && Control.CssClass.Length > 0)
    24               {
    25                   writer.WriteAttribute("class", Control.CssClass);
    26               }
    27               writer.Write(HtmlTextWriter.TagRightChar);
    28           }
    29   
    30           protected override void RenderEndTag(HtmlTextWriter writer)
    31           {
    32               writer.WriteEndTag("ul");
    33           }
    34   
    35           protected override void RenderContents(HtmlTextWriter writer)
    36           {
    37               RadioButtonList ButtonList = Control as RadioButtonList;
    38               if (null != ButtonList)
    39               {
    40                   int i = 0;
    41                   foreach (ListItem li in ButtonList.Items)
    42                   {
    43                       string itemID = string.Format("{0}_{1}", ButtonList.ClientID, i++);
    44                       writer.WriteBeginTag("li");
    45                       writer.Write(HtmlTextWriter.TagRightChar);
    46                       writer.WriteBeginTag("input");
    47                       writer.WriteAttribute("id", itemID);
    48                       writer.WriteAttribute("type", "radio");
    49                       writer.WriteAttribute("name", ButtonList.UniqueID);
    50                       writer.WriteAttribute("value", li.Value);
    51                       if (li.Selected)
    52                       {
    53                           writer.WriteAttribute("checked", "checked");
    54                       }
    55                       writer.Write(HtmlTextWriter.TagRightChar);
    56                       writer.WriteEndTag("input");
    57                       writer.WriteBeginTag("label");
    58                       writer.WriteAttribute("for", itemID);
    59                       writer.Write(HtmlTextWriter.TagRightChar);
    60                       writer.Write(li.Text);
    61                       writer.WriteEndTag("label");
    62                       writer.WriteEndTag("li");
    63                       writer.WriteLine();
    64                       Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID, li.Value);
    65                   }
    66                   Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID);
    67               }
    68           }
    69       }
    70   }
    
      
    Monday, February 5, 2007 4:28 PM
  • User1660405101 posted

     Hi Falcon,

     Your solution is exactly what I was looking for. However, I just noticed that, when used with ajax.net, it doesn't appear to trigger a postback upon selection change. The standard radiobuttonlist does, however. Got any ideas?
     

    Wednesday, January 23, 2008 7:19 PM
  • User-1385398420 posted

    The CodePlex release of the control adapters includes a RadioButtonList adapter. I haven't tried it with ASP.Net AJAX, but you may want to give it a shot.

    Latest releases available at http://www.codeplex.com/cssfriendly/SourceControl/ListDownloadableCommits.aspx

     

    Friday, January 25, 2008 8:49 AM