简体   繁体   English

C#.Net-RadioButton Control适配器和回发

[英]C#.Net - RadioButton Control adapter and postback

I have a radio button control adaptor that attempts to render the radio button control with the CSS class as part of the input tag, rather than as a surrounding span. 我有一个单选按钮控件适配器,它尝试使用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();

    }
}

Currently, this renders pretty close to what I want, the only difference being the group name attribute (the standard radio button uses the internal value UniqueGroupName, whereas I am using just GroupName. I can't seem to find a way to get UniqueGroupName, and the line below should counter this anyway: 目前,这使渲染效果非常接近我想要的效果,唯一的区别是组名属性(标准单选按钮使用内部值UniqueGroupName,而我仅使用GroupName。我似乎找不到找到UniqueGroupName的方法,并且下面的行无论如何都应对此予以反击:

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

Old HTML with standard radio buttons- 带有标准单选按钮的旧HTML-

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

New rendering- 新渲染

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

The problem is that postback is not working - the RadioButton1.Checked value is always false. 问题是回发不起作用-RadioButton1.Checked值始终为false。 Any ideas on how to get the radio button's value in postback? 关于如何在回发中获取单选按钮值的任何想法?

The reason postbacks aren't working is because on the return trip the field name doesn't match what ASP.NET was expecting. 回发不起作用的原因是,在回程中,字段名称与ASP.NET期望的名称不匹配。 So, it's not an ideal solution, but you can use reflection to get the UniqueGroupName: 因此,这不是理想的解决方案,但是您可以使用反射来获取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;

Or broken into separate lines for clarity: 或为了清晰起见,分为几行:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM