Why does ASP.Net RadioButton and CheckBox render inside a Span?

前端 未结 7 1384
一个人的身影
一个人的身影 2020-12-08 00:12

I would expect this:




        
7条回答
  •  我在风中等你
    2020-12-08 00:42

    I came across this issue and am attempted to solve it using control adaptors.

    See here for an example of doing this to a radio button list.

    I ended up with this as my RadioButtonAdaptor-

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.Adapters;
    
    public class RadioButtonAdapter : WebControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            RadioButton targetControl = this.Control as RadioButton;
    
            if (targetControl == null)
            {
                base.Render(writer);
    
                return;
            }
    
            writer.WriteBeginTag("input");
            writer.WriteAttribute("type", "radio");
            writer.WriteAttribute("name", targetControl.GroupName);
            writer.WriteAttribute("id", targetControl.ClientID);            
    
            if (targetControl.CssClass.Length > 0)
            {
                writer.WriteAttribute("class", targetControl.CssClass);
            }      
    
            writer.Write(" />");
    
        }
    }
    

    And this added to my browsers file-

    
                        
                
            
    
    

    Of course, there are some downsides to this. Along with those mentioned at the above link, you also lose functionality if you do not impliment everything (the above code does not allow for a radio button to be checked). There are some CSS friendly control adaptors, but they do not cover the radio button control. It may be possible to use Reflector to get the default control adaptor as a starting point.

提交回复
热议问题