How do I add Page Events for ASP.NET in Visual Studio 2008
Solution 1
- In the solution explorer, right click the page and select "View component designer" from the context menu
- open the properties panel/window (press F4)
- now click the yellow arrow/flash icon and you will see a list of all page events
- double-click the event for which you want to add a handler
I'm pretty sure there was another way (starting from the designer view), but I can't reproduce it.
I usually do not use the page event handlers, instead I override the corresponding methods (e.g. OnLoad
instead of Page_Load
). To implement one of these overrides, you can simply type "override" in the code-behind and press space to get a list of methods that you can override.
Solution 2
With the invaluable ReSharper installed (might work without) I can just type:
override
and when I hit space IntelliSence pops up with a list of all the events that I can override such as OnInit, OnPreRender, etc.
Solution 3
as a shortcut to see what's available, you could always just type "Page." and then take a look a the list in intellisense. You could then pick one, hit +=Tab Tab to have it generate the stub for you. once the stub is created, you'd have to delete the "Page.event+=" line wherever you created it. Kind of a hokey workaround, but can work pretty quick once you get the hang of it.
Chet
I'm a data software engineer at Simon Data in NYC. I have an M.Eng from Cornell University where I studied distributed computing, information storage, retrieval, natural language processing, and some machine learning. Favorite language is Clojure because functional programming with immutable data structures is beautiful when done right, and the real world JVM performance is impressive. In the past I've done a lot of work engineering in web applications on both LAMP and .NET stacks. I love Python, done a lot of Java, C#, Ruby, and worked in PHP. My dev environment is usually OSX, git, and some combination of IntelliJ (Java), Sublime (JS), Emacs (for lisp/Clojure), and Vim (everything else).
Updated on February 01, 2020Comments
-
Chet over 4 years
This is a bit of a Visual Studio question. I feel with all the helpful Intellisense there should be something to assist but I can't seem to find it.
I made a page with a codebehind in ASP.NET C# in VS2008 and it autogenerates a PageLoad event method, of course. Well, what if I want to add methods for more events besides PageLoad? I would think there would be some list on the Foo.aspx page of possible method event handlers to add. Aren't there more maybe like PageInit, PageDispose, (or equiv) etc...? Where can I find these?
EDIT - I can of course look up the method names in the api. I'm looking for a handy shortcut to add these in Visual Studio. If it generates one, can't it make others?
-
McArthey over 11 yearsHokey but not pokey - works quickly and easy to remember. Thanks!
-
David Carr over 10 yearsIn VS2010, I don't have ReSharper installed and it seems to do this anyhow. I think this is the easiest approach of all mentioned here thus far.
-
Dave Cousineau about 9 yearsThis works, but there are subtle differences between handling the events and overriding the On_ methods.
-
Dave Cousineau about 9 yearswhat? unless I'm mistaken this has nothing at all to do with the question
-
Dave Cousineau about 9 yearsAlso, this won't generate an event handler with the appropriate name, either. The event handler must be named, for example,
Page_Load
. This will name it after your class rather than afterPage
, and so it won't actually be hooked up to your page. -
Dave Cousineau about 9 yearsThis works nicely. Since the event is being handled from a reference named
Page
, the generated event handler incidentally gets named correctly, ie:Page_Load
for theLoad
event. The only tricky part is that this has to be done from "method scope" since you can't access thePage
property at class scope. -
Dave Cousineau about 9 yearsI had originally said that the event handler must be
protected
and notprivate
. This may or may not be necessary depending on the situation. -
yu yang Jian almost 7 yearsWorks in VS2017!