How do I add Page Events for ASP.NET in Visual Studio 2008
Asked Answered
D

6

10

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?

Diaconicum answered 25/6, 2009 at 21:21 Comment(0)
D
31
  • 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.

Dexedrine answered 25/6, 2009 at 21:30 Comment(2)
Works in VS2017!Hoplite
Tried this in VS2019. It added the events, but none of them were called. I want "Page_Init", not "myPageName_Init".Absquatulate
R
4

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.

Removal answered 25/6, 2009 at 22:13 Comment(2)
In 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.Ensnare
This works, but there are subtle differences between handling the events and overriding the On_ methods.Chirlin
A
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.

Admissible answered 25/6, 2009 at 21:33 Comment(2)
Hokey but not pokey - works quickly and easy to remember. Thanks!Cembalo
This 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 the Load event. The only tricky part is that this has to be done from "method scope" since you can't access the Page property at class scope.Chirlin
I
0

In the source code window, from the object list combo box, select the desired control (page). Then from the event list combo box that is to the right of the previous object list combo box select the desired event. Visual studio will create the event handler for you.

Ineslta answered 8/8, 2010 at 17:41 Comment(0)
W
0

If one wants to not use the way as described by M4N but through code:

In the PageName.aspx.cs;

private void InitializeComponent()
{

    // this.LifeCycle += .. // Use intellisense to see alternatives easily
    this.PreRender += new System.EventHandler(this.EventFunctionName);

}

then in the same file add:

private void EventFunctionName(object sender, EventArgs e)
{
    // Code..
}

UPDATE (from comment by Sahuagin): This won't generate an event handler with the appropriate name. The event handler must be named, for example, Page_Load. This will name it after your class rather than after Page, and so it won't actually be hooked up to your page

Wray answered 24/2, 2013 at 20:56 Comment(2)
Also, 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 after Page, and so it won't actually be hooked up to your page.Chirlin
I had originally said that the event handler must be protected and not private. This may or may not be necessary depending on the situation.Chirlin
J
-1

TreeScheme.Nodes[0].ChildNodes[0].Checked=true;

treeviewid.node[0].childnodes[0].checked=true;

This will set the child node checked true in page load event of page

happy coding

Jail answered 3/8, 2013 at 8:0 Comment(1)
what? unless I'm mistaken this has nothing at all to do with the questionChirlin

© 2022 - 2024 — McMap. All rights reserved.