不被调用的CustomValidator为DropDownList的(显然) [英] customvalidator for dropdownlist not being invoked (apparently)

查看:111
本文介绍了不被调用的CustomValidator为DropDownList的(显然)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写自定义验证器正在使用自动回一个DropDownList。似乎完全忽略验证。为什么忽略,有一个简单的办法?

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?

请注意,我没有使用的ControlToValidate

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>

背后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.

这似乎并没有在所有执行的背后code。

It doesn't seem to be executing the behind code at all.

有关调试,我加了线的Response.Redirect(dummy.html)...永远也不会被调用,这也预示(我认为)验证器永远不会被调用。

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.

推荐答案

删除更新面板,并尝试使用javascript来做验证在客户端本身。

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

客户端验证

JavaScript事件的定义,

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的部分:

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.

服务器端验证

如果你真的想验证服务器端请参见下面的code:

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.

$ C $事件背后C相如下:

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..

这篇关于不被调用的CustomValidator为DropDownList的(显然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆