未调用下拉列表的自定义验证器(显然)
为使用自动回发的下拉列表编写自定义验证器。似乎完全忽略了验证。为什么它被忽略并且有简单的修复方法吗?
注意我没有使用 ControlToValidate
asp.net:
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional" Visible="true" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="ddlCommandAssign" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:CustomValidator id="val_command_assigned" runat="server"
ErrorMessage="* "
display="Static"
OnServerValidate="commandAssigned"
/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCommandAssign"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Behind Code:
Sub commandAssigned(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim s As String
s = ddlCommandAssign.SelectedValue
'if s = "1" then
' args.IsValid = true
'else
' args.IsValid = False
'end if
args.IsValid = False
End Sub
出于调试目的,我希望它每次都失败。
它似乎根本没有执行后面的代码。
为了调试,我添加了response.redirect("dummy.html")行...它永远不会被调用,这也表明(我认为)验证器永远不会被调用。
Writing a custom validator for a dropdownlist that is using autopostback. Seems to ignore the validation altogether. Why is it ignored and is there an easy fix?
Note I did not use ControlToValidate
asp.net:
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional" Visible="true" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="ddlCommandAssign" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:CustomValidator id="val_command_assigned" runat="server"
ErrorMessage="* "
display="Static"
OnServerValidate="commandAssigned"
/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCommandAssign"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Behind Code:
Sub commandAssigned(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim s As String
s = ddlCommandAssign.SelectedValue
'if s = "1" then
' args.IsValid = true
'else
' args.IsValid = False
'end if
args.IsValid = False
End Sub
For debugging purposes, I want it to fail every time.
It doesn't seem to be executing the behind code at all.
For debugging, I added the line response.redirect("dummy.html") ... which never gets called, which also indicates (I think) that the validator never gets called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除更新面板并尝试使用 JavaScript 在客户端本身进行验证。
客户端验证
JavaScript 事件定义,
Aspx 部分:
注意: 自定义验证器和触发按钮应具有相同的验证组。
服务器端验证
如果您确实想要验证服务器端,请参阅下面的代码:
注意: 自定义验证器和触发按钮应该具有相同的验证组。
事件背后的代码如下所示:
希望这有帮助..
Remove the update panel and try to do the validation at client-side itself using javascript.
CLIENT-SIDE VALIDATION
JavaScript event definition,
Aspx section:
NOTE: Both the custom validator and the triggering button should have same validation group.
SERVER-SIDE VALIDATION
If you really want the validation server side see the code below:
NOTE: Both the custom validator and the triggering button should have same validation group.
code behind event looks as below:
Hope this helps..
我面临着同样的问题..但终于得到了解决方案。
仅当您的下拉列表中没有添加任何项目时才会发生这种情况。
i was facing the same issue.. but finally got the solution.
it happens only when u have no item added in your drop down list.
您必须使用适当命名的ControlToValidate 属性:
否则,自定义验证器将不会执行任何验证。
You have to specify the control to validate using the aptly-named ControlToValidate property:
Otherwise, the custom validator will not perform any validation whatsoever.