habilitar/deshabilitar la validación del código detrás de C#

I have some asp code written for a form. One of the fields must have 3 types of validation, 2 of which are alway enabled, the other 2 must be "enabled" only if a peculiar value has been selected from a dropdownlist. How am I supposed to achieve this task? I have disabled the 2 extra validations by defaults and wold like to reactivate on dropdownlist specific selection.

I show you my code here. ASP.NET code:

<tr>
<td class="style1">
<asp:Label ID="LabelPiva" runat="server" Text="Partita IVA" meta:resourcekey="LabelPiva" Font-Bold="True" />
</td>
<td>
<asp:TextBox ID="pivaTextBox" runat="server" Text='<%# Bind("piva") %>' 
    MaxLength="50" Width="400px" />



<asp:RequiredFieldValidator ID="RequiredPiva" runat="server" 
    ControlToValidate="pivaTextBox" 
    ErrorMessage="<%$ Resources:Resource, CampoObbligatorio %>" Display="Dynamic" 
    CssClass="little_text" />
<asp:CustomValidator ID="PivaEsistente" runat="server" 
    ErrorMessage="Partita IVA esistente nel database" meta:resourcekey="PivaEsistente" 
    ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic" 
    onservervalidate="PIVAEsistente_ServerValidate"></asp:CustomValidator>

<asp:RegularExpressionValidator ID="PivaSize" runat="server"
    ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic"
    ErrorMessage="Controllare la lunghezza della partita iva. 11 caratteri e solo numeri." 
    ValidationExpression="^[0-9]{11}$" Enabled="False" ValidationGroup="soloItalia">
    </asp:RegularExpressionValidator>

<asp:CustomValidator ID="PivaErrata" runat="server" 
    ErrorMessage="Partita IVA non corretta. Controllare le cifre." meta:resourcekey="PivaErrata" 
    ControlToValidate="pivaTextBox" CssClass="little_text" Display="Dynamic" 
    onservervalidate="ValidatePI"  Enabled="False" ValidationGroup="soloItalia"></asp:CustomValidator>

</td>
</tr>

The CustomValidator with id PivaErrata and the RegularExpressionValidator with id PivaSize must be fired only when dropdownlist hits the "it" value. This is the code-behind in c# to intercept the value of the dropdownlist:

protected void nazioneDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    DropDownList ddlProv = (DropDownList)FormUser.FindControl("provinciaDropDownList");



    if ("it".Equals(ddl.SelectedValue))
    {
        ddlProv.Enabled = true;


    }
    else
    {
        ddlProv.SelectedIndex = 0;
        ddlProv.Enabled = false;
    }

}

As you can see, another dropdownlist gets enabled when that specific value "it" is fired in the nations dropdownlist. I would like to activate the validations controls too. I assigned a validationgroup to those 2 validations, but I am not sure how to "enable" them at once. Any help? Thank you very much.

preguntado el 02 de diciembre de 13 a las 08:12

I've tried to declare the ID of the validators I want to enable/disable this way: BaseValidator pivasizeval = (BaseValidator)FormUser.FindControl("PivaSize"); and then in the conditional control I enable this way: pivasizeval.Enabled = true; -

no way. It doesn't work. Any suggestion? I'm getting mad. -

1 Respuestas

puedes usar Enabled propiedad de la Validation controls a enable or disable de ellas.

Prueba esto:

if (ddl.SelectedItem.ToString().Equals("it"))
    {
        ddlProv.Enabled = true;

       //add these two statements to enable
       PivaSize.Enabled=true;
       PivaErrata.Enabled=true;
    }
    else
    {
        ddlProv.SelectedIndex = 0;
        ddlProv.Enabled = false;

       //add these two statements to disable
       PivaSize.Enabled=false;
       PivaErrata.Enabled=false;
    }

Respondido el 02 de diciembre de 13 a las 08:12

thanks Sudhakar, but here is what I get: The name 'PivaSize' does not exist in the current context - Aptivus

I just add to put the validators in an update panels and your code worked well. Thanks - Aptivus

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.