【问题标题】:Validation Ignored with PostBackUrl or Response.Redirect Using C#使用 C# 忽略 PostBackUrl 或 Response.Redirect 的验证
【发布时间】:2011-09-23 20:06:45
【问题描述】:

我有一个带有一些自定义验证的表单。表单上有一个按钮,可以将用户带到“确认页面”以显示订单的所有详细信息。

页面验证

    <asp:TextBox ID="txtBillingLastName" Name="txtBillingLastName" 
runat="server"  CssClass="txtbxln required"></asp:TextBox>
    <asp:CustomValidator 
    ID="CustomValidatorBillLN" runat="server" 
    ControlToValidate="txtBillingLastName"
    OnServerValidate="CustomValidatorBillLN_ServerValidate"
    ValidateEmptyText="True">
    </asp:CustomValidator>

验证器代码

protected void CustomValidatorBillLN_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = isValid(txtBillingLastName);
    }

但是,如果我将 PostBackUrl 或 Response.Redirect 添加到按钮的 onclick 方法,则会忽略所有验证控件。

我可以使用 onclick 方法调用所有验证方法,但这似乎不是一个优雅的解决方案。

我尝试设置 CausesValidation=False,但没有成功。

有什么建议吗?

【问题讨论】:

    标签: c# asp.net postbackurl


    【解决方案1】:

    当然,如果您无条件重定向,则该验证将被忽略。您应该在重定向之前致电this.IsValid

    protected btRedirect_Click( object sender, EventArgs e )
    {
       if ( this.IsValid )
         Response.Redirect( ... );
    }  
    

    【讨论】:

      【解决方案2】:

      检查此代码

      void ValidateBtn_OnClick(object sender, EventArgs e) 
        { 
           // Display whether the page passed validation.
           if (Page.IsValid) 
           {
              Message.Text = "Page is valid.";
           }
      
           else 
           {
              Message.Text = "Page is not valid!";
           }
        }
      
        void ServerValidation(object source, ServerValidateEventArgs args)
        {
           try 
           {
              // Test whether the value entered into the text box is even.
              int i = int.Parse(args.Value);
              args.IsValid = ((i%2) == 0);
           }
      
           catch(Exception ex)
           {
              args.IsValid = false;
           }
        }
      

      还有Html侧码

      <form id="Form1" runat="server">
      
        <h3>CustomValidator ServerValidate Example</h3>
      
        <asp:Label id="Message"  
             Text="Enter an even number:" 
             Font-Name="Verdana" 
             Font-Size="10pt" 
             runat="server"/>
      
        <p>
      
        <asp:TextBox id="Text1" 
             runat="server" />
      
        &nbsp;&nbsp;
      
        <asp:CustomValidator id="CustomValidator1"
             ControlToValidate="Text1"
             ClientValidationFunction="ClientValidate"
             OnServerValidate="ServerValidation"
             Display="Static"
             ErrorMessage="Not an even number!"
             ForeColor="green"
             Font-Name="verdana" 
             Font-Size="10pt"
             runat="server"/>
      
        <p>
      
        <asp:Button id="Button1"
             Text="Validate" 
             OnClick="ValidateBtn_OnClick" 
             runat="server"/>
      

      更多信息请查看Custom validator

      希望我的回答能帮助您解决问题。

      【讨论】:

        猜你喜欢
        • 2010-10-27
        • 2013-08-06
        • 2012-07-10
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多