LoginCancelEventArgs.Cancel Property

Definition

Gets or sets a value indicating whether the event should be canceled.

C#
public bool Cancel { get; set; }

Property Value

true if the event should be canceled; otherwise, false.

Examples

The following code example uses the LoggingIn event to ensure that the user has entered a well-formed email address in the UserName property. If not, the LoggingIn event handler sets the Cancel property to true, and an error message is displayed. For an .aspx file that can be used to run the example, see the LoginCancelEventArgs topic.

C#
public partial class LoginCancelEventArgscs_aspx : System.Web.UI.Page
{

    bool IsValidEmail(string strIn)
    {
        // Return true if strIn is in valid email format.
        return System.Text.RegularExpressions.Regex.IsMatch(strIn, 
            @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    }
    
    protected void OnLoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)
    {
        if (!IsValidEmail(Login1.UserName))
        {
            Login1.InstructionText = "You must enter a valid email address.";
            e.Cancel = true;
        }
        else
        {
            Login1.InstructionText = String.Empty;
        }
    }
}

Remarks

You can use the Cancel property to indicate whether an event, such as the LoggingIn event, the LoggingOut event, or the ChangingPassword event, should be canceled.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also