asp.net repeater radio-button

grupo de botones de radio asp.net



repeater radio-button (10)

Actualmente tengo un problema con los botones de radio y la agrupación. Tengo un botón de radio asp dentro de un control de repetidor. Tengo el atributo de nombre de grupo establecido en "Cliente". Cuando se carga la página, los botones de radio no se agrupan. En lugar de establecer los campos de identificación en el nombre del grupo, está configurando los campos de valor de los botones de opción. Sé que he intentado configurar los botones de radio fuera de un control de repetidor y he tenido el mismo problema. ¿Que esta pasando aqui?

aspx

<asp:Repeater ID="repCustomers" runat="server"> <HeaderTemplate> <table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important"> <tr> <th>&nbsp;</th> <th>Cust. No.</th> <th>Cust. Name</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:RadioButton ID="radCustomer" GroupName="Customer" runat="server" ValidationGroup="Customer" ToolTip=''<%#Eval("CustomerNumber") %>'' /> </td> <td><%#Eval("CustomerNumber")%></td> <td><%#Eval("Name") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>

html de salida

<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important"> <tr> <th>&nbsp;</th> <th>Cust. No.</th> <th>Cust. Name</th> </tr> <tr> <td> <span title="111111"><input id="ctl00_PrimaryContent_repCustomers_ctl01_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl01$Customer" value="radCustomer" /></span> </td> <td>111111</td> <td>Jeremy''s Test</td> </tr> <tr> <td> <span title="222222"><input id="ctl00_PrimaryContent_repCustomers_ctl02_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl02$Customer" value="radCustomer" /></span> </td> <td>222222</td> <td>My Test</td> </tr> <tr> <td> <span title="333333"><input id="ctl00_PrimaryContent_repCustomers_ctl03_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl03$Customer" value="radCustomer" /></span> </td> <td>333333</td> <td>Jim Bob''s BBQ</td> </tr> <tr> <td> <span title="444444"><input id="ctl00_PrimaryContent_repCustomers_ctl04_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl04$Customer" value="radCustomer" /></span> </td> <td>444444</td> <td>New Hope Hamburgers</td> </tr> <tr> <td> <span title="555555"><input id="ctl00_PrimaryContent_repCustomers_ctl05_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl05$Customer" value="radCustomer" /></span> </td> <td>555555</td> <td>Pied Piper Pizza</td> </tr> <tr> <td> <span title="666666"><input id="ctl00_PrimaryContent_repCustomers_ctl06_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl06$Customer" value="radCustomer" /></span> </td> <td>666666</td> <td>Sandy''s Subs</td> </tr> <tr> <td> <span title="777777"><input id="ctl00_PrimaryContent_repCustomers_ctl07_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl07$Customer" value="radCustomer" /></span> </td> <td>777777</td> <td>Leonard''s Lambchops</td> </tr> <tr> <td> <span title="888888"><input id="ctl00_PrimaryContent_repCustomers_ctl08_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl08$Customer" value="radCustomer" /></span> </td> <td>888888</td> <td>Dave''s Diamond Deli</td> </tr> <tr> <td> <span title="999999"><input id="ctl00_PrimaryContent_repCustomers_ctl09_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl09$Customer" value="radCustomer" /></span> </td> <td>999999</td> <td>Ernie''s Eatery</td> </tr> </table>


Comenzaría agregando un valor en mi botón de radio Valor = ''<% # Eval ("Número de cliente")%>''.


Desafortunadamente, este es un problema bien conocido con los botones de radio dentro de un repetidor . Una de sus únicas opciones sería crear un control de servidor personalizado derivado de la clase RadioButton y anular la forma en que se procesa.

EDITAR : Aquí hay una muestra de cómo puede verse la clase derivada:

public class MyRadioButton : RadioButton { protected override void Render(HtmlTextWriter writer) { writer.Write("<input id=/"" + base.ClientID + "/" "); writer.Write("type=/"radio/" "); writer.Write("name=/"" + base.ID + "/" "); writer.Write("value=/"" + base.ID + "/" />"); writer.Write("<label for=/"" + base.ClientID + "/">"); writer.Write(base.Text); writer.Write("</label>"); } }



Finalmente resolví esto creando un botón de opción simple y configurando el valor utilizando una evaluación del lado del servidor.

<input type="radio" name="radCustomer" value=''<%#Eval("CustomerNumber") %>'' />

Ahora, cuando la aplicación realiza una devolución de datos, compruebo el valor de Request.Form ["radCustomer"]. Esto funciona perfectamente.


Hice esto:

$("input:radio").attr("name", $("input:radio").first().attr("name"));

¿Por qué? porque si reemplaza la propiedad de nombre por cualquier cadena que desee, obtendrá un ''error no encontrado''. Por lo tanto, debe obtener el nombre del primer botón de radio y cambiar el nombre de todos ellos con ese nombre. Funciona como un sharm;)


Hice que mi botón de radio tuviera el ajuste automático de retorno en verdadero, y luego, en el controlador de eventos, configuré el resto de los botones de radio para que NO fueran seleccionados.

No es lo ideal, pero necesito mucho control sobre la visibilidad y los atributos habilitados del botón de radio, y me pareció más fácil dejar que ASP.NET controle eso en lugar de recurrir al script del lado del cliente.


Lo arreglé en javascript

$(document).ready(function () { $("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName"); });


Mi solución, similar a otras:

<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" > // Fixes this ASP.NET bug: if radio input is inside repeater you can''t set its name. // Every input gets set different name by ASP.NET. // They don''t behave as a group. You can select multiple radios. function fixRadiogroupBug() { $(''[type="radio"][data-fixgroupbug]'').click(function () { $(this).siblings(''[type="radio"]'').prop(''checked'', false); }); } $(document).ready(function () { fixRadiogroupBug(); });


Tuve los mismos problemas. Estoy utilizando Literal como marcador de posición para representar el botón de radio en el evento creado con evento.

ASP.Net

<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated"> <ItemTemplate> <asp:Literal ID="lit" runat="server"></asp:Literal> </ItemTemplate> </asp:Repeater>

DO#

protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) { Literal lit = (Literal)e.Item.FindControl("lit"); lit.Text = "<input type=/"radio/" name=/"myGroup/">"; }


Tuve que modificar ligeramente la respuesta publicada anteriormente por r3dsky.

Esto es lo que funcionó para mí:

$(document).ready(function () { $(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName"); });