【问题标题】:C#.Net - RadioButton Control adapter and postbackC#.Net - RadioButton 控制适配器和回发
【发布时间】:2010-12-08 05:27:00
【问题描述】:

我有一个单选按钮控件适配器,它尝试将带有 CSS 类的单选按钮控件呈现为输入标记的一部分,而不是作为周围的跨度。

public class RadioButtonAdapter : WebControlAdapter
{
    protected override void Render(HtmlTextWriter writer)
    {
        RadioButton targetControl = this.Control as RadioButton;

        if (targetControl == null)
        {
            base.Render(writer);

            return;
        }                    

        writer.AddAttribute(HtmlTextWriterAttribute.Id, targetControl.ClientID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");        
        writer.AddAttribute(HtmlTextWriterAttribute.Name, targetControl.GroupName); //BUG - should be UniqueGroupName        
        writer.AddAttribute(HtmlTextWriterAttribute.Value, targetControl.ID);
        if (targetControl.CssClass.Length > 0)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, targetControl.CssClass);
        }        

        if (targetControl.Page != null)
        {
            targetControl.Page.ClientScript.RegisterForEventValidation(targetControl.GroupName, targetControl.ID);
        }
        if (targetControl.Checked)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
        }            
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

    }
}

目前,这非常接近我想要的,唯一的区别是组名属性(标准单选按钮使用内部值 UniqueGroupName,而我只使用 GroupName。我似乎找不到方法获取 UniqueGroupName,下面的行无论如何都应该解决这个问题:

targetControl.Page.ClientScript.RegisterForEventValidation(targetControl.GroupName, targetControl.ID);

带有标准单选按钮的旧 HTML-

<span class="radio">
<input id="ctl00_ctl00_mainContent_RadioButton1" type="radio" value="RadioButton1" name="ctl00$ctl00$mainContent$mygroup"/>
</span>

新渲染-

<input id="ctl00_ctl00_mainContent_RadioButton1" class="radio" type="radio" value="RadioButton1" name="mygroup"/>

问题在于回发不起作用 - RadioButton1.Checked 值始终为 false。关于如何在回发中获取单选按钮的值的任何想法?

【问题讨论】:

    标签: c# asp.net controls postback control-adapter


    【解决方案1】:

    回发不起作用的原因是在回程中字段名称与 ASP.NET 所期望的不匹配。因此,这不是一个理想的解决方案,但您可以使用反射来获取 UniqueGroupName:

    using System.Reflection;
    
    //snip...
    
    RadioButton rdb = this.Control as RadioButton;
    string uniqueGroupName = rdb.GetType().GetProperty("UniqueGroupName",
        BindingFlags.Instance | BindingFlags.NonPublic).GetValue(rdb, null) as string;
    

    为了清楚起见,或分成单独的行:

    Type radioButtonType = rdb.GetType(); //or typeof(RadioButton)
    
    //get the internal property
    PropertyInfo uniqueGroupProperty = radioButtonType.GetProperty("UniqueGroupName",
        BindingFlags.Instance | BindingFlags.NonPublic);
    
    //get the value of the property on the current RadioButton object
    object propertyValue = uniqueGroupProperty.GetValue(rdb, null);
    
    //cast as string
    string uniqueGroupName = propertyValue as string;
    

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2016-05-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2018-04-09
      • 2020-04-25
      相关资源
      最近更新 更多