Disabling Validation when ControlToValidate is Invisible

Last post 09-12-2007 10:28 AM by Ganesh@Nilgris. 6 replies.

Sort Posts:

  • Disabling Validation when ControlToValidate is Invisible

    09-11-2007, 6:56 AM
    • Loading...
    • arunmanglick
    • Joined on 04-12-2006, 6:23 AM
    • Xpanxion Pune
    • Posts 37

    Hi,


    I have an asp:DropDownList and Requiredfield Validator as below.

    <asp:DropDownList ID="DropDownList1" runat="server" >
               <asp:ListItem Selected="True" Text="Choose Items" Value=""></asp:ListItem>
                <asp:ListItem Text="aa" Value="aa"></asp:ListItem>
                <asp:ListItemText="bb" Value="bb"></asp:ListItem>
                <asp:ListItemText="cc" Value="cc"></asp:ListItem>
    </asp:DropDownList>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"
                Display="Dynamic" ErrorMessage="Please Choose" EnableClientScript="true"></asp:RequiredFieldValidator>


    There are two more radio buttons,which toggles the visibility of Drop Down list.

    <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="Validate" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Make Visible" />
    <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="Validate" OnCheckedChanged="RadioButton2_CheckedChanged" Text="Make In-Visible" />

    In last there is a Button. 

     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


    Expected Behavior:
        - When DropDown is Visible - If button is clicked then system should popup Validator.
        - When DropDown is In-Visible - If button is clicked then system should not popup Validator.

    Solution done by me:

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RequiredFieldValidator1.Enabled = true;
        }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
      {
           RequiredFieldValidator1.Enabled = false;
      }

     

    Please help to achieve this in a more better way.

     

    Thanks & Regards,

    Arun Manglick || Tech Lead || AppliedSB – Digital Downloads || 620

     

    Arun Manglick
    Filed under: ,
  • Re: Disabling Validation when ControlToValidate is Invisible

    09-11-2007, 7:35 AM
    • Loading...
    • Ritesh Manglani
    • Joined on 10-26-2006, 1:03 PM
    • Vadodara (Gujrat, India)
    • Posts 104
    arunmanglick:

    There are two more radio buttons,which toggles the visibility of Drop Down list.

    <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="Validate" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Make Visible" />
    <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="Validate" OnCheckedChanged="RadioButton2_CheckedChanged" Text="Make In-Visible" />


    arunmanglick:

    Expected Behavior:
        - When DropDown is Visible - If button is clicked then system should popup Validator.
        - When DropDown is In-Visible - If button is clicked then system should not popup Validator.
    To handel the visibility, set dropdown's visible to false...

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RequiredFieldValidator1.Enabled = true;
            DropDownList1.Visible = true;

        }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
      {
           RequiredFieldValidator1.Enabled = false;
           DropDownList1.Visible = false;
      }

    I guess that is the only change required.

    Cheers
    Ritesh

    The Biggest Fun of Life is in Doing Things People Say You Can Not Do.
  • Re: Disabling Validation when ControlToValidate is Invisible

    09-11-2007, 9:05 AM
    • Loading...
    • johram
    • Joined on 06-13-2006, 10:36 AM
    • Sweden
    • Posts 3,352
    • Moderator

    Do you want to solve it without a Postback? Then you need to hook up some javascript on the client that disables the validator. 

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Disabling Validation when ControlToValidate is Invisible

    09-11-2007, 9:09 AM
    • Loading...
    • slavo
    • Joined on 12-29-2006, 5:42 PM
    • Posts 13

    What you are doing is disabling the validator control itself. I just don't see how you hide the dropdown list. If you want to hide the dropdown list, just set its Visible property to false. When doing that, you can also set the CausesValidation property to false. In the handler for the other radioButton, do the opposite. Then the page will know that the dropdown list should not be validated.

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.DropDownList1.Visible = false;
            this.DropDownList1.CausesValidation = false;

        }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
      {
           
    this.DropDownList1.Visible = true;
            this.DropDownList1.CausesValidation = true;

      }

  • Re: Disabling Validation when ControlToValidate is Invisible

    09-12-2007, 2:33 AM
    • Loading...
    • arunmanglick
    • Joined on 04-12-2006, 6:23 AM
    • Xpanxion Pune
    • Posts 37

    Thanks for the reply. But 'CausesValidation'  will not serve the purpose.

    Reason:

    • There is no postback being attached to the drop down.
    • Neither the need is to validate the page on Selection Change event of drop down. Instead the expectation is to validate/not-validate the page on 'Button' click.


    As I have already marked, the working solution with me is as:
     

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            DropDownList1.Visible = true;        
            RequiredFieldValidator1.Enabled = true;      
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            DropDownList1.Visible = false;
            RequiredFieldValidator1.Enabled = false;      
        }
      

     

    I just need to know, Can we have some better solution than the mentioned or should I mark it as 'Resolved'

    Regards,

    Arun... 

    Arun Manglick
  • Re: Disabling Validation when ControlToValidate is Invisible

    09-12-2007, 4:58 AM
    Answer
    • Loading...
    • slavo
    • Joined on 12-29-2006, 5:42 PM
    • Posts 13

    On PostBack the whole page is validated, no matter which control caused the postback. I understand that you do not want to validate when changing the selection of the dropdown, this is why you do not have such an event. However, if you have a handler for the CheckedChanged event of the RadioButtonList, then it means you expect a postback to occur when you change the radio button. As you see, in the code I gave you I'm doing everything in the method for the radio button, not the dropdown. If you don't want a postback at all - use javascript. Otherwise, the code I gave you should work on postback not caused by DropDownList. Maybe I got confused and didn't understand what you want. 

  • Re: Disabling Validation when ControlToValidate is Invisible

    09-12-2007, 10:28 AM

    put the dropdownlist and the validator inside a panel.

    Reason : the panel control are rendered based on the enabling function of the panel which will not cause the requiredfield validatior to work as the panel is disabled

    Next set the inital value property  in requiredfield validator and the text should match with the dropdownlist "selected" item

    enable or disable the panel based on the radio button selection index changed event is fired.

    Hope this helps to solve your problem in a better way


    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter