开发者

visual studio 2010 - preInit method

I'm using VS2010 and I'm going through some examples in my book and I'm trying to find the preInit method. I seem to recall in VS2008 for VB.Net all the Page LifeCycle methods were in the upper right hand side drop down for the "Page" element (upper left hand side).

Like I said I'm using VS2010 and in C#. In my upper left drop down all I see is _Default. The only PLC event I see is Page_Load. Do I have to manually type out the preInit method or is there a way to get it to auto populate like I've seen in VB using 2008?

visual studio 2010 - preInit method

I followed Kirill answer and it did everything he said it would. However, this is what it produced (Default.aspx is the name of the page I'm using):

private void _Default_PreInit(object sender, EventArgs e)
{
 ...
}

When I put a break point in there, it never got into that code block and th开发者_如何转开发e code was never run...

However, when I manually wrote it out using the following:

protected void Page_PreInit(object sender,EventsArgs)
{
...
}

The break point and the code in that block worked!

Any ideas?


Here is the answer. If short:

  • 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

UPDATE

Yes you're right, it's not working as expected. Sorry for posting the answer without really checking if it works. I do not know how to fix it, but I've at least found the explanation.

Besides the method you've mentioned it also adds something like

private void InitializeComponent()
        {
            this.PreInit += new System.EventHandler(this._Default_PreInit);
        }

It comes from the old designing/compilation model. After MS changed default designing/compilation model in ASP.NET 2.0., by default AutoEventWireup is set to true, which instructs compiler to automatically attach event handlers from the code behind/beside file using naming convention (for the page events) e.g.

protected void Page_Load(...)
{
}

is automatically attached behind the scenes:

this.Load += new EventHandler(this.Page_Load)

So obviously the corresponding behavior of the Visual Studio 2010 was not changed according to these changes, and it still creates event handlers following the old naming convention and adding the delegate attaching method, which is no more necessary to have.


I found then answer to my question. I'm studying for the MCTS 70-515 exam and on Chapter 3 (pg.111) in the "Understanding the ASP.Net Life Cycle and Handling Events lesson I read the following paragraph:

Adding a C# Event Handler

"The code editor for C# also has an event handler drop-down list. However, it only contains access to events you have already provided handlers for. The C# environment provides a tool for wiring up control events, but it does not provide such a tool for the Page events. For these you have to code the event handler manually."

However, in a VB.Net web side, you get what I was originally describing with the 2 drop downs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜