Skip to main content
All docs
V22.2

Declare Event Handler (Razor)

Generates an event handler for the selected method reference in a .razor file.

Availability

This refactoring is available when the caret is in a method reference inside HTML markup.

How to Use

  1. Place the caret in a method reference inside HTML markup. For example in the “ToggleNavMenu” method reference.

    <div class="top-row pl-4 navbar navbar-dark">
        <a class="navbar-brand" href="">BlazorApp</a>
        <button class="navbar-toggler" @onclick="ToggleNavMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>    
    
    @code {
        private bool collapseNavMenu = true;
        private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;    
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Choose the Declare Event Handler in Razor Code Block or Declare Event Handler in Razor Code Behind refactoring from the menu.

    Select Refactoring

The Declare Event Handler in Razor Code Block refactoring generates an event handler in the @code block of a .razor file for a selected method reference in HTML markup.

Declare in Code Block

The Declare Event Handler in Razor Code Behind refactoring is available in the Code Actions menu if a code-behind file exists. This refactoring generates an event handler in a Razor code-behind file (razor.cs) for a selected method reference in HTML markup.

Declare in Code Behind

CodeRush opens the Razor code-behind (razor.cs) file after execution.