简体   繁体   English

使用CustomValidator验证前回发

[英]Postback fires before validation when using CustomValidator

I have made a simple project to explain my problem. 我做了一个简单的项目来解释我的问题。

This is my Default.aspx: 这是我的Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"        
Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
        <asp:CustomValidator    ID="cusDate" 
                                runat="server" 
                                ValidateEmptyText="true" 
                                OnServerValidate="DateValidate" 
                                ValidationGroup="DateVal" 
                                ControlToValidate="txtDate" 
                                ErrorMessage="Date error"></asp:CustomValidator>

        <asp:ImageButton        ID="btnSaveDate" 
                                CausesValidation="true" 
                                ValidationGroup="DateVal" 
                                ImageUrl="~/Images/save_32.png" 
                                runat="server" />
    </div>
</form>
</body>
</html>

And this is my Default.aspx.cs 这是我的Default.aspx.cs

using System;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)  <<<<<<BREAKPOINT 1 HERE
            {
            }
        }

        protected void DateValidate(Object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;  <<<<<<BREAKPOINT 2 HERE
        }
    }
}

I set two breakpoints, as shown above, and run the application. 如上所述,我设置了两个断点,然后运行该应用程序。 When I click "btnSaveDate" it first stops at breakpoint 1 and then at breakpoint 2. I thought it would stop at breakpoint 2 first, then reload the page and then stop at breakpoint 1. 当我单击“ btnSaveDate”时,它首先停在断点1,然后停在断点2。我认为它会先停在断点2,然后重新加载页面,然后停在断点1。

Is there something wrong in the code or should it behave like this? 代码中有什么问题吗?还是应该这样?

I have read many articles about this and tried a lot of different solutions, but no one has worked so far. 我已经阅读了许多有关此的文章,并尝试了许多不同的解决方案,但是到目前为止,还没有人工作。

According to the ASP.NET Page Life Cycle , the Postback event handling happens after the Load event. 根据ASP.NET页面生命周期回发事件处理发生 Load事件之后。

If the request is a postback, control event handlers are called. 如果请求是回发,则调用控制事件处理程序。 After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. 此后,将调用所有验证器控件的Validate方法,该方法设置单个验证器控件和页面的IsValid属性。 (There is an exception to this sequence: the handler for the event that caused validation is called after validation.) (此序列有一个例外:导致验证的事件的处理程序在验证后被调用。)

A postback means that a client is making an http request to the server and, every time a request arrives to the server, the Page Life Cycle stages are executed in this exact order. 回发意味着客户端正在向服务器发出http请求,并且每次请求到达服务器时, 页面生命周期阶段都将按照此确切顺序执行。 So the second answer is no, is not possible to enter in the breakpoint 2 then before entering the breakpoint 1. 因此,第二个答案为“否”,无法在进入断点1之前输入断点2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM