mercredi 19 août 2015

asp.net c#.net jquery / javascript how to enable or disable a requiredfieldvalidator using a checkbox

I am having an issue with trying to enable or disable a requiredfieldvalidator using a checkbox. Obviously, when the checkbox is checked I want it to be enabled and unchecked to be disabled. When the checkbox is checked it also opens up a FileUpload so obviously I would want that to register if there is not file chosen. Here is some of my code.

HTML:

            <tr>
                <td>
                    <asp:CheckBox ID="chkExecLAF" Text="Executed LAF" runat="server" OnCheckedChanged="chkExecLAF_CheckedChanged" />
                </td>
                <td id="tblExecLAF">
                    <asp:FileUpload ID="FileUpload1" runat="server" CssClass="FileUpload1" />
                    <asp:RequiredFieldValidator ID="rfvExecLAF" ControlToValidate="FileUpload1" ErrorMessage="*Required" ForeColor="Red" runat="server" />
                </td>
                <td>
                    <asp:CheckBox ID="chkDatedLeaseAbstract" Text="Dated Lease Abstract" runat="server" />
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload2" runat="server" CssClass="FileUpload2" />
                </td>
            </tr>

Code Behind:

    protected void chkExecLAF_CheckedChanged(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            rfvExecLAF.Enabled = false;
        }
        else
        {
            rfvExecLAF.Enabled = true;
        }
    }

jquery:

 <script type="text/javascript">
    $(function () {
        $('.FileUpload1').attr('disabled', true);
        $('.rfvExecLAF').attr('disabled', true);
        $('.rfvExecLAF').hide();
        $('.FileUpload1').hide();
        $('#<%=chkExecLAF.ClientID %>').click(function () {

            if ($(this).is(':checked')) {
                $('.FileUpload1').removeAttr('disabled');
                $('.rfvExecLAF').removeAttr('disabled');
                $('.rfvExecLAF').show();
                $('.FileUpload1').show();
            } else {
                $('.FileUpload1').attr('disabled', true);
                $('.rfvExecLAF').attr('disabled', true);
                $('.rfvExecLAF').hide();
                $('.FileUpload1').hide();
            }
        });
    });
</script>

Aucun commentaire:

Enregistrer un commentaire