2

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

注意我没有使用 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>

代码背后:

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") ... 它永远不会被调用,这也表明(我认为)验证器永远不会被调用。

4

3 回答 3

2

删除更新面板并尝试使用 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;            
    }

希望这可以帮助..

于 2011-06-30T13:50:00.693 回答
2

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

于 2011-10-22T22:09:59.520 回答
1

必须使用恰当命名的ControlToValidate属性指定要验证的控件:

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

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

于 2011-06-30T13:41:55.463 回答