我有一個自定義驗證器(.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
UPDATE:下面的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");
}
}
抱歉,這不起作用 – netNewbi3 2010-09-13 15:27:54