开发者

Validate DropDownList if checkbox checked

开发者 https://www.devze.com 2023-01-13 15:14 出处:网络
ASP.NET3.5 Webforms. Am trying to put a validator on the drop down list, only if the checkbox is checked.

ASP.NET3.5 Webforms. Am trying to put a validator on the drop down list, only if the checkbox is checked.

This doesn't work. If I put an alert in the onclick event, it does show.

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


You can use Firebug add-on for Firefox to debug your javascript code it make life easier.

anyway you can rewrite your function like this

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 + "')");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号