我正在尝试为FormView中的RadioButtonList控件设置条件验证。希望有人能发现我的错误。只有在ThirdPartyInfoTextBox具有文本值且无论rbMTA检查状态如何时,才会禁用验证程序。如果选中rbMTA(0)并且启用了rbMTA(1),我正在寻找要禁用的验证器。
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function ToggleValidator() {
var rb = document.getElementById("<%=fvMTAReq.FindControl("rbMTA").ClientID%>");
var valName = document.getElementById("<%=fvMTAReq.FindControl("RequiredFieldValidator9").ClientID%>");
if (rb.getElementsByID("rbMTA").checked) {
ValidatorEnable(valName, false);
}
else {
ValidatorEnable(valName, true);
}
}
</script>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:FormView ID="fvMTAReq" runat="server" DataKeyNames="RID" DataSourceID="sqldsMTA" DefaultMode="Insert" HorizontalAlign="Left">
<InsertItemTemplate>
<asp:RadioButtonList ID="rbMTA" runat="server" SelectedValue='<%# Bind("ThirdParty") %>' onclick="ToggleValidator()">
<asp:ListItem Selected="false" Value="true">Yes, I confirm that this material is not subject to an agreement nor was it provided to my lab and/or the Danforth Center by another party.</asp:ListItem>
<asp:ListItem Value="false">No. Explanation:</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="ThirdPartyInfoTextBox" runat="server" Text='<%# Bind("ThirdPartyInfo") %>' TextMode="MultiLine" Rows="5" Columns="40"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ValidationGroup="Validation1" ErrorMessage="" Text="*Required*"
ControlToValidate="ThirdPartyInfoTextBox" ValidateEmptyText="true" CssClass="RequiredField">
</asp:RequiredFieldValidator>
<br />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="Validation1" CommandName="Insert" Text="Submit" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
答案 0 :(得分:1)
您犯了很多语法错误。使用以下内容。
function ToggleValidator() {
var rb = document.getElementById('<%=fvMTAReq.FindControl("rbMTA").ClientID%>');
var valName = document.getElementById('<%=fvMTAReq.FindControl("RequiredFieldValidator9").ClientID%>');
if (document.getElementByID("rbMTA").checked) {
ValidatorEnable(valName, false);
}
else {
ValidatorEnable(valName, true);
}
}
你忘了逃避引号,在if条件下,你输了一个错字。
同时将ClientIDMode
添加到Static
到控件。