2009-10-20 6 views
1

J'ai un adaptateur de contrôle de bouton radio qui tente de rendre le contrôle du bouton radio avec la classe CSS comme faisant partie de la balise d'entrée plutôt que comme une travée environnante.C# .Net - Adaptateur de contrôle RadioButton et publication

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(); 

    } 
} 

Actuellement, ce qui rend assez proche de ce que je veux, la seule différence étant l'attribut name du groupe (le bouton radio standard utilise la valeur interne UniqueGroupName, alors que je me sers juste GroupName. Je ne peux pas sembler trouver un moyen d'obtenir UniqueGroupName, et la ligne ci-dessous devrait contrer toute façon:

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

Old HTML avec radio standard buttons-

<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"/> 

Le problème est que la publication ne fonctionne pas - la valeur RadioButton1.Checked est toujours false. Des idées sur la façon d'obtenir la valeur du bouton radio en publication?

Répondre

3

La raison pour laquelle les publications ne fonctionnent pas est que, lors du retour, le nom du champ ne correspond pas à ce qu'ASP.NET attendait. Donc, ce n'est pas une solution idéale, mais vous pouvez utiliser la réflexion pour obtenir le 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; 

ou cassé en plusieurs lignes pour plus de clarté:

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; 
+0

Merci, a parfaitement fonctionné. – Spongeboy

Questions connexes