未调用下拉列表的自定义验证器(显然)

发布于 2024-11-18 04:00:17 字数 1339 浏览 4 评论 0原文

为使用自动回发的下拉列表编写自定义验证器。似乎完全忽略了验证。为什么它被忽略并且有简单的修复方法吗?

注意我没有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听,心雨的声音 2024-11-25 04:00:17

删除更新面板并尝试使用 JavaScript 在客户端本身进行验证。

客户端验证

JavaScript 事件定义,

 function ValidateFunction(sender,args) 
 {
   var ddlCommandAssign= document.getElementById('<%=ddlCommandAssign.ClientID %>');
    if (ddlCommandAssign.options[control.selectedIndex].value=='0') 
    {  args.IsValid = false;//This shows the validation error message and stops execution at client side itself.}
  else { args.IsValid = true;//This will return to the server side. }    
 }

Aspx 部分:

  <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="1">select</asp:ListItem>
        <asp:ListItem Value="2">sdasda</asp:ListItem>
    </asp:DropDownList>
    <asp:CustomValidator ID="valCustmID" runat="server" ErrorMessage="*" ForeColor="Red"
        ValidationGroup="group1" ClientValidationFunction="ValidateFunction"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />

注意: 自定义验证器和触发按钮应具有相同的验证组。

服务器端验证

如果您确实想要验证服务器端,请参阅下面的代码:

     <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="1">select</asp:ListItem>
                <asp:ListItem Value="2">sdasda</asp:ListItem>
            </asp:DropDownList>
            <asp:CustomValidator ID="CustomValidator1" OnServerValidate="commandAssigned" runat="server" ErrorMessage="*" ValidationGroup="group1"></asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />
        </ContentTemplate>
    </asp:UpdatePanel>

注意: 自定义验证器和触发按钮应该具有相同的验证组。

事件背后的代码如下所示:

    protected void commandAssigned(object source, ServerValidateEventArgs args)
    {
        if (DropDownList1.SelectedItem.Value == "1")            
            args.IsValid = false;  //since you gave controlToValidate="DropDownList1"  this will display the error message.       
        else           
            args.IsValid = true;            
    }

希望这有帮助..

Remove the update panel and try to do the validation at client-side itself using javascript.

CLIENT-SIDE VALIDATION

JavaScript event definition,

 function ValidateFunction(sender,args) 
 {
   var ddlCommandAssign= document.getElementById('<%=ddlCommandAssign.ClientID %>');
    if (ddlCommandAssign.options[control.selectedIndex].value=='0') 
    {  args.IsValid = false;//This shows the validation error message and stops execution at client side itself.}
  else { args.IsValid = true;//This will return to the server side. }    
 }

Aspx section:

  <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="1">select</asp:ListItem>
        <asp:ListItem Value="2">sdasda</asp:ListItem>
    </asp:DropDownList>
    <asp:CustomValidator ID="valCustmID" runat="server" ErrorMessage="*" ForeColor="Red"
        ValidationGroup="group1" ClientValidationFunction="ValidateFunction"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />

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:

     <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="1">select</asp:ListItem>
                <asp:ListItem Value="2">sdasda</asp:ListItem>
            </asp:DropDownList>
            <asp:CustomValidator ID="CustomValidator1" OnServerValidate="commandAssigned" runat="server" ErrorMessage="*" ValidationGroup="group1"></asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />
        </ContentTemplate>
    </asp:UpdatePanel>

NOTE: Both the custom validator and the triggering button should have same validation group.

code behind event looks as below:

    protected void commandAssigned(object source, ServerValidateEventArgs args)
    {
        if (DropDownList1.SelectedItem.Value == "1")            
            args.IsValid = false;  //since you gave controlToValidate="DropDownList1"  this will display the error message.       
        else           
            args.IsValid = true;            
    }

Hope this helps..

梦初启 2024-11-25 04:00:17

我面临着同样的问题..但终于得到了解决方案。
仅当您的下拉列表中没有添加任何项目时才会发生这种情况。

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.

仙女山的月亮 2024-11-25 04:00:17

必须使用适当命名的ControlToValidate 属性:

<asp:CustomValidator id="val_command_assigned" runat="server"
    ErrorMessage="* " Display="Static" OnServerValidate="commandAssigned"
    ControlToValidate="ddlCommandAssign" />

否则,自定义验证器将不会执行任何验证。

You have to specify the control to validate using the aptly-named ControlToValidate property:

<asp:CustomValidator id="val_command_assigned" runat="server"
    ErrorMessage="* " Display="Static" OnServerValidate="commandAssigned"
    ControlToValidate="ddlCommandAssign" />

Otherwise, the custom validator will not perform any validation whatsoever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文