如果选中复选框,则验证DropDownList

时间:2010-08-31 03:14:57

标签: javascript asp.net

ASP.NET3.5 Webforms。我试图在下拉列表中放置一个验证器,只有选中了复选框。

这不起作用。如果我在onclick事件中发出警报,它会显示。

<script language="javascript" type="text/javascript">
    function setVal(sender) {
        var myVal = document.getElementById('<%=(DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlSupplierCouncilArea")%>');
        ValidatorEnable(myVal, !sender.checked);
        ValidatorValidate(myVal)
    }  
</script>

<asp:CheckBox runat="server" ID="cbxIsSupplier" Text="I wish to register as a Supplier in the"
                                     OnClick="setVal(this)" />

<asp:DropDownList ID="ddlSupplierCouncilArea" runat="server">
                                    <asp:ListItem>[Please select]</asp:ListItem>
                                    <asp:ListItem Value="blah1">blah1</asp:ListItem>
                                    <asp:ListItem Value="Waimakariri">blah2</asp:ListItem>
                                </asp:DropDownList>
                                <div class="validators">
                                    <asp:RequiredFieldValidator ID="rfvCouncilArea" runat="server" InitialValue="[Please select]"
                                        ControlToValidate="ddlCollectorCouncilArea" ErrorMessage="Select a Council Area"
                                        Enabled="false" ToolTip="Select a council area" ValidationGroup="CreateUserWizard1">* required</asp:RequiredFieldValidator>
                                </div>

1 个答案:

答案 0 :(得分:0)

您可以使用Firefox的Firebug插件来调试您的javascript代码,使生活更轻松。

无论如何你可以像这样重写你的功能

function setVal(sender, drpClientID)
{
     var myVal = document.getElementById(drpClientID);
        ValidatorEnable(myVal, !sender.checked);
        ValidatorValidate(myVal)
}

In asp.net code you should build your onclick attribute like this

cbxIsSupplier.Attributes.Add("onclick", "setVal(this, '" + ddlSupplierCouncilArea.ClientID + "')");