如果检查下拉列表的选定值是否大于0,我有以下代码可以正常工作。
但是,我需要检查下拉列表的选定索引而不是值。
Sub selectValidation(source As Object, args As ServerValidateEventArgs)
Try
args.IsValid = args.Value > 0
Catch ex As Exception
args.IsValid = False
End Try
End Sub
将 .Value 更改为 .SelectedIndex 会产生以下错误:
BC30456: 'SelectedIndex' is not a member of 'System.Web.UI.WebControls.ServerValidateEventArgs'.
编辑:这是验证器代码...
<asp:DropDownList runat="server" ID="Adults" AutoPostBack="true" />
<asp:CustomValidator id="Req_Adults"
ControlToValidate="Adults"
ClientValidationFunction="selectValidation"
OnServerValidate="selectValidation"
runat="server"
CssClass="errorAsterisk"
Text="*"
ErrorMessage="Select number of adults" />
<asp:DropDownList runat="server" ID="Children" AutoPostBack="true" />
<asp:CustomValidator id="Req_Children"
ControlToValidate="Children"
ClientValidationFunction="selectValidation"
OnServerValidate="selectValidation"
runat="server"
CssClass="errorAsterisk"
Text="*"
ErrorMessage="Select number of children" />
客户端验证(工作正常):
function selectValidation(source, arguments)
{
var selectedValue = $(source).siblings("select").prop("selectedIndex");
if (selectedValue > 0 ){
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}