简体   繁体   中英

Postback fires before validation when using CustomValidator

I have made a simple project to explain my problem.

This is my 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

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.

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.

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. (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. So the second answer is no, is not possible to enter in the breakpoint 2 then before entering the breakpoint 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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