Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CustomValidator-使用asp.net服务器ValidateEventargs对选定索引进行验证_Asp.net_Vb.net_Validation_Selectedindex - Fatal编程技术网

CustomValidator-使用asp.net服务器ValidateEventargs对选定索引进行验证

CustomValidator-使用asp.net服务器ValidateEventargs对选定索引进行验证,asp.net,vb.net,validation,selectedindex,Asp.net,Vb.net,Validation,Selectedindex,我有下面的代码,如果检查dropdownlist的选定值是否大于0,它可以正常工作 但是,我需要检查dropdownlist的选定索引,而不是值 Sub selectValidation(source As Object, args As ServerValidateEventArgs) Try args.IsValid = args.Value > 0 Catch ex As Exception args.IsValid = False En

我有下面的代码,如果检查dropdownlist的选定值是否大于0,它可以正常工作

但是,我需要检查dropdownlist的选定索引,而不是值

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" />

如果可以直接从处理程序访问
DropDownList
,则此操作有效:

Sub selectValidation(source As Object, args As ServerValidateEventArgs)
    args.IsValid = Me.DropDownList1.SelectedIndex <> -1
End Sub

@汤姆:我看不出这个编辑有什么用。您应该向我们显示标记,您要查找的
DropDownList
在哪里?相反,您向我们展示了
CustomValidator
。我的答案只是关于
CustomValidators
,所以这并不是什么新鲜事。请注意,无论您是否已将
控件设置为validate
,这都无关紧要,因为它只是一个
字符串。。。我又编辑了一次。。。如果你还有什么需要看的,请告诉我…@Tom:我不明白,你有没有试着用我的代码将
DropDownList1
替换为
成年人
?对不起。。。我不是很清楚这是我的错。我试图通过创建一个子系统来验证多个dropdownlists来避免重复类似的代码。它们都必须具有大于0的selectedIndex。请参阅对代码的最新编辑。-如果我只验证一个元素,您的代码运行得很好。@Tom:我不喜欢这种方法,顺便问一下,您是如何实现
ClientValidationFunction
的,因为它应该有类似的问题?如果确实要使用相同的处理程序,可以将
ValidationGroup
分配给相关的
DropDownLists
并使用Linq查找它们。编辑了我的答案。
Sub selectValidation(source As Object, args As ServerValidateEventArgs)
    args.IsValid = Me.DropDownList1.SelectedIndex <> -1
End Sub
Dim ddlNumZero = From ddl In Me.Controls.OfType(Of DropDownList)()
           Where ddl.ValidationGroup = "NumberValidation" _
           AndAlso ddl.SelectedIndex <= 0
args.IsValid = Not ddlNumZero.Any()