0

I have a dropdown list that when selected value is equal to "Closed". I want a Required field to become enabled. I have tried this

<asp:DropDownList ID="DropDownList9" runat="server" 
                            DataSourceID="SqlDataCaseStatus" DataTextField="CaseStatus" 
                            DataValueField="CaseStatus" Text='<%# Bind("Case_Status") %>' AutoPostBack="False" onchange="if(this.options[this.selectedIndex].text='Closed')ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', true);else ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', false); " >
                        </asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateOfWriteOff" runat="server" 
                ControlToValidate="TextBox13" 
                ErrorMessage="Date Of Write Off is a Required Field">*</asp:RequiredFieldValidator>

But when i change the dropdown i get an error:

Microsoft JScript runtime error: Unable to set value of the property 'visibility': object is null or undefined.

any help would be much appreciated

Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
Key-eat
  • 3
  • 1
  • 4
  • take a look here [Enable/Disable Required field validator from cs page](http://stackoverflow.com/questions/15827578/enable-disable-required-field-validator-from-cs-page) – Ravi Jan 02 '14 at 13:06

4 Answers4

0

First of all try changing = to == when comparing text with 'Closed'.

synek317
  • 749
  • 2
  • 7
  • 22
0
<asp:DropDownList ID="DropDownList9" runat="server" 
                        DataSourceID="SqlDataCaseStatus" DataTextField="CaseStatus" 
                        DataValueField="CaseStatus" Text='<%# Bind("Case_Status") %>' AutoPostBack="False" onchange="validateSelection(this);" >
                    </asp:DropDownList>

<script language="javascript" type="text/javascript">

    function validateSelection(drp) {

        var vc = document.getElementById('<% = RequiredFieldValidatorDateOfWriteOff.ClientID %>');

        if (drp.text == 'Closed') {

            ValidatorEnable(vc, true);

        }
        else {
            ValidatorEnable(vc, false);
        }

    }


</script>
Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
  • tried this but no joy, agree it should be in function but just want to get it working first :-) thanks – Key-eat Jan 02 '14 at 13:23
  • My DropDownList is in a DetailsView so its having trouble accessing the value .... coming up as Null, any ideas ? – Key-eat Jan 02 '14 at 15:35
0

You need to use the Enabled property. Give this a go, the code is very rough and is just for an example. I have extracted the behaviour into one method, just for ease of demonstration.

<body>
    <form id="form1" runat="server">
        <script type="text/javascript">

            function EnableValidator() {
                var dropdown1 = document.getElementById('DropDownList1');
                var a = dropdown1.options[dropdown1.selectedIndex];

                if (a.text == "ValidateYes1") {
                    document.getElementById("RequiredFieldValidator1").enabled = true;
                } else {
                    document.getElementById("RequiredFieldValidator1").enabled = false;
                }
            };
        </script>

    <div>

    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" onchange="EnableValidator();">
        <asp:ListItem>ValidateYes1</asp:ListItem>
        <asp:ListItem>ValidateNo2</asp:ListItem>
    </asp:DropDownList>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator
        ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator><asp:Button
            ID="Button1" runat="server" Text="Button" />
    </form>
</body>
Damon
  • 3,004
  • 7
  • 24
  • 28
  • My DropDownList is in a DetailsView so its having trouble accessing the value .... coming up as Null, any ideas ? – Key-eat Jan 02 '14 at 15:34
  • That's because the client ID of the control is controlled by ASP. What you could do is in the JavaScript try var dropdown1 = document.getElementById(<%= DropDownList1.ClientID %>); (Follow the same convention for the other controls.) Also have a look at this question: http://stackoverflow.com/questions/5473111/how-to-get-control-inside-asp-detailsview-through-javasscript – Damon Jan 02 '14 at 15:58
0

ValidatorEnable requires control and you are passing id of control

ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', true)

That is why you are getting the object null error.

Please change it to

ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateOfWriteOff.ClientID %>'), true);
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31