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

1

4 Answers 4

0

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

Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

My DropDownList is in a DetailsView so its having trouble accessing the value .... coming up as Null, any ideas ?
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: stackoverflow.com/questions/5473111/…
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);

Comments

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>

2 Comments

tried this but no joy, agree it should be in function but just want to get it working first :-) thanks
My DropDownList is in a DetailsView so its having trouble accessing the value .... coming up as Null, any ideas ?

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.