我有一个自定义验证器(.net 3.5),它检查我表单中的四个下拉列表是否有重复值。它适用于服务器端,但我想添加一个客户端功能来配合它。我对JavaScript一无所知。你能帮忙吗?谢谢。
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>
Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue)
End Sub
Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
If first <> "" And second <> "" AndAlso first.Equals(second) Then
Return first.Equals(second)
End If
End Function
更新:以下 JavaScript 代码可以正常工作,因为它会检查下拉列表中是否存在重复值。但是,如何将其链接到我的自定义验证器并消除警报消息。就目前而言,页面已提交。谢谢。
function dropDownValidation_ClientValidate() {
var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');
var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2');
var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3');
var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4');
var result = haveSameValue(strValue1.value, strValue2.value) &&
haveSameValue(strValue1.value, strValue3.value) &&
haveSameValue(strValue1.value, strValue4.value) &&
haveSameValue(strValue2.value, strValue3.value) &&
haveSameValue(strValue2.value, strValue4.value) &&
haveSameValue(strValue3.value, strValue4.value);
return result;
}
function haveSameValue(ddlValue1, ddlValue2) {
if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
alert("Related documents contain duplicate values");
}
}