All docs
V20.2
20.2
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Declare Event Handler (Razor)

  • 2 minutes to read

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

Availability

Available when the caret is on a method reference in a .razor file.

How to Use

  1. Place the caret in a method reference in a .razor file.

    NOTE

    The blinking cursor shows the caret's position where Refactoring is available.

<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;    
}
  1. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  2. Choose the Declare Event Handler (Razor code block) or Declare Event Handler (Razor code behind) refactoring from the menu.

    Select Refactoring

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

Declare in Code Block

The Declare Event Handler (Razor code behind) refactoring generates an event handler in a Razor code-behind file (razor.cs) for the selected method reference.

Declare in Code Behind

CodeRush opens the newly created Razor code-behind (razor.cs) file after execution. To keep the original .razor file open, disable the "Activate a new file when it's created by refactorings (Move Type to File, Declare Class, etc.)" option on the Editor | C# | Code Actions | Code Action Settings options page.

Select Refactoring

Refer to the following help topic for more information: Code Actions Settings.