2010-05-31 11 views
6
<asp:RadioButtonList ID="RdoBtnHasNotified" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="RdoBtnHasNotified_SelectedIndexChanged"> 
    <asp:ListItem Value="1">Yes</asp:ListItem> 
    <asp:ListItem Value="0" Selected="True">No</asp:ListItem> 
</asp:RadioButtonList> 


<asp:TextBox ID="TxtHowNotified" runat="server" TextMode="MultiLine" MaxLength="100"></asp:TextBox> 

Quiero habilitar el TextBox haciendo clic en el RadioButtonList, sin necesidad de utilizar autopostback=true. ¿Cómo puedo hacer esto con JavaScript?Javascript en asp.net

Respuesta

1

Puede usar jQuery para manipular el estado habilitado de entrada (traducción HTML para TextBox) o puede usar ASP.NET Ajax para poder establecer ambos controles dentro del panel de actualización; en este caso, no verá la página recargada en devolución lo cual debe suceder para que pueda cambiar el estado de TextBox en algún otro evento. Tbh me gustaría ir con ASP.NET Ajax porque mi experiencia muestra que jQuery no funciona tan bien con los controles ASP.NET cuando se trata de cosas complejas, es decir. ASP.NET utiliza JavaScript para la activación de eventos, lo que puede causar que jQuery o ASP.NET no funcionen como es de esperar.

Buena suerte con paneles de actualización ...

0

Cada ListItem hace un botón de radio con el mismo parámetro de nombre; Sugeriría ejecutar la aplicación y buscar en la fuente generada para tener una idea de lo que debe hacer para escuchar los eventos del botón de opción. En esencia, el ID de la lista de botones de radio es el parámetro de nombre, para que pueda obtener el grupo de botones de radio como (usando jQuery):

$("input[name='<%= rbl.ClientID%>']").click(function() { 
    var tb = $("#textboxid"); 
    //do something here; this points to the radio button 
}); 

HTH.

0

Aquí van:

<%@ Page Language="C#" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 
    protected void RdoBtnHasNotified_PreRender(object sender, EventArgs e) 
    { 
     foreach (ListItem item in RdoBtnHasNotified.Items) 
      item.Attributes.Add("onclick", string.Format("toggleTextBox(this,'{0}');", TxtHowNotified.ClientID)); 
    } 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 

    <script type="text/javascript"> 
     function toggleTextBox(radioButton, textBoxId) { 
      document.getElementById(textBoxId).disabled = radioButton.value != "1"; 
     } 
    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:RadioButtonList ID="RdoBtnHasNotified" OnPreRender="RdoBtnHasNotified_PreRender" 
      runat="server" RepeatDirection="Horizontal"> 
      <asp:ListItem Value="1">Yes</asp:ListItem> 
      <asp:ListItem Value="0" Selected="True">No</asp:ListItem> 
     </asp:RadioButtonList> 
     <asp:TextBox ID="TxtHowNotified" runat="server" TextMode="MultiLine" MaxLength="100" Enabled="false"></asp:TextBox> 
    </div> 
    </form> 
</body> 
</html> 
0

escribir el código de la siguiente manera

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("input[name='RdoBtnHasNotified']").change(function() { 
     $("input[name='RdoBtnHasNotified']:checked").val() == '1' ? $('#TxtHowNotified').removeAttr("disabled") : $('#TxtHowNotified').attr('disabled', 'true'); 

     }); 
    }); 

</script> 

y también desactivar el cuadro de texto (Enabled = "false"), ya que initialy el valor de la "RdoBtnHasNotified" no es".

1

Usando jQuery, puede tener un resultado bastante personalizada enganchando a los cambios en los botones de radio ...


$("#<%= RdoBtnHasNotified.ClientID %> > input[type=radio]").change(function(){ 
    // this function is called whenever one of the radio button list's control's change 
    // the $(this) variable refers to the input control that triggered the event 
    var txt = $("#<%= TxtHowNotified.ClientID %>"); 
    if($(this).val()=="1") { 
    txt.removeAttr("disabled"); 
    } else { 
    txt.attr("disabled", true); 
    } 
}); 
0
$('#<%= RdoBtnHasNotified.ClientID %> > input[type=radio]').click(function() 
{ 
    var txtbox = $('#<%= TxtHowNotified.ClientID %>'); 
    if($(this).val() == '1') 
    { 
    document.getElementById('#<%= TxtHowNotified.ClientID %>').disabled = false; 
    } 
    else 
    { 
    document.getElementById('#<%= TxtHowNotified.ClientID %>').disabled = true; 
    } 
}); 

Creo que el uso de evento de cambio no se disparará en el IE.

Cuestiones relacionadas