The JavaScript to handle this isn't very difficult.
Given the following ASP controls:
<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server" OnClick="updateValidator();" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject" ErrorMessage="You must enter a subject." runat="server" />
Add the following JavaScript function:
<script language="javascript" type="text/javascript">
function updateValidator() {
var enableValidator = !event.srcElement.status;
var rfvSubject = document.getElementById('rfvSubject');
ValidatorEnable(rfvSubject, enableValidator);
}
</script>
That's all there is to it. You will also want to add the following code to your Page Load event, so that if the user has JavaScript disabled, your required field validator is still turned on or off properly:
rfvSubject.Enabled = chkSubjectRequired.Checked
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…