选择值.net时,文本框上需要启用下拉列表

时间:2014-01-02 13:03:40

标签: c# javascript jquery asp.net

我有一个下拉列表,当选择的值等于“已关闭”时。我想要一个必填字段启用。我试过这个

<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>

但是当我更改下拉列表时出现错误:

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

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:0)

首先在将文本与'已关闭'进行比较时尝试将=更改为==。

答案 1 :(得分: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 :(得分:0)

您需要使用Enabled属性。给它一个去,代码非常粗糙,仅作为一个例子。我已将行为提取到一个方法中,只是为了便于演示。

<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>

答案 3 :(得分:0)

ValidatorEnable需要控制,并且您正在传递控件ID

ValidatorEnable('RequiredFieldValidatorDateOfWriteOff', true)

这就是你得到对象null错误的原因。

请将其更改为

ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateOfWriteOff.ClientID %>'), true);