0

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.

2
  • 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; Commented Dec 2, 2013 at 11:18
  • no way. It doesn't work. Any suggestion? I'm getting mad. Commented Dec 2, 2013 at 11:52

1 Answer 1

2

you can use Enabled property of the Validation controls to enable or disable them.

Try This:

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Sudhakar, but here is what I get: The name 'PivaSize' does not exist in the current context
I just add to put the validators in an update panels and your code worked well. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.