4

When you edit a simple page in the design view, you can add an event on most components by simply double-clicking the relevant event. This does the binding and generates the function declaration in the codebehind for you.

In larger projects where the pages are complex, the design view can be extremely slow. Is there a way to quickly generate the codebehind stubs directly from the .aspx source?

3 Answers 3

7

Just begin typing the code to create your component.

For example, this is the code to add a button to your .aspx file:

<asp:Button ID="ButtonAction" class="btn panel-black" runat="server" Text="" OnClick="Function_Name_Here" />

When you get to typing the function name for the OnClick method...

for example, this part of the code:

OnClick="Function_Name_Here"

... Visual Studio will display a little pop-up window with the option to "<create new event>".

Select this option and the codebehind event handler stub will be generated for you.

Sign up to request clarification or add additional context in comments.

Comments

2

In the markup view the Properties window is still available: you may have it hidden.

When it's shown and the cursor is in the markup for a particular control, you get that control's properties and events like you would in Design view. Similarly, in the event tab you can double-click the event (i.e Click) to automatically generate an empty declaration in code and hook up the event without ever having to go into the Design view.

1 Comment

In my case the properties window only lists a few properties and events in the markup view. For an asp:button for example, it doesn't list "onClick".
1

I've been able to do this with ReSharper, but I've been using it for so long that I don't recall whether Visual Studio has this capability natively.

3 Comments

Ok, that makes sense. I'm sure I've seen it "in action" but it could have been a plugin. If ReSharper can do it then that is a valid solution.
How do you do this with ReSharper ?
@Mathieu: type the method name, like OnClick="NewHandler", click in "NewHandler", wait for the quick-fix lightbulb, choose "Create handler".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.