Kevin
Kevin

Reputation: 2802

ASP.NET lifecycle - control onLoad and RaisePostBackEvent

I'm maintaining some older C# code that relies on postbacks to persist data. I am having a hard time understanding what is going on in the lifecycle here.

It seems that the RaisePostBackEvent is running before any of the form control onLoad events have ran.

For example, I have a dropdownlist and a textbox on a page.

The dropdownlist has the following handler defined

public class MyPage
{
    private void Page_Init(object sender, EventArgs e)
    {
        ddlDemo.SelectedIndexChanged += new EventHandler(ddlDemo_SelectedIndexChanged);
    }
}

The textbox control does some stuff in the onLoad event

public class MyTextBox: System.Web.UI.WebControls.TextBox
...
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        //do some stuff
    }
...
}

My problem is that the dropdownlist handler runs before the MyTextBox OnLoad event.

According to the ASP.NET Lifecyle described at MSDN and CSharpcorner, (as I understand it) the order of events should be

  1. Init
  2. Load (control properties are loaded with information recovered from view state and control state)
  3. Postback event handling

What I'm experiencing is

  1. Init
  2. Page Load
  3. Postback event handling
  4. Control OnLoad events

Can someone explain to me why the handler runs before any control OnLoad have ran?

Upvotes: 0

Views: 779

Answers (3)

Jian Huang
Jian Huang

Reputation: 1185

My ASP.Net is rusty. You can try this, move the line:

ddlDemo.SelectedIndexChanged += new EventHandler(ddlDemo_SelectedIndexChanged);

to Page_Load event. This should solve your issue.

Is there a special reason why you put in Page_Init event? Page_Init fires early in the life cycle, and before your 'MyTextBox' OnLoad event.

Upvotes: 1

Kevin
Kevin

Reputation: 2802

Finally found the problem.

ddlDemo.Load += new EventHandler(ddlDemo_SelectedIndexChanged);

When debugging the code I thought I was looking at the SelectedIndexChanged event.

This should be (it looks like it was at one point, but this code is several years old)

ddlDemo.SelectedIndexChanged += new EventHandler(ddlDemo_SelectedIndexChanged);

Upvotes: 0

Kyle
Kyle

Reputation: 4298

A control will not participate in the page lifecycle UNTIL it is added to the Page's control tree. Once it is added to the Page's control tree, it will play "catch up" (see article below on dynamic controls). Is your control added to the page's control tree? How do you know? (You didn't post relevant code so I can't be sure).

Second, read and understand these two articles, it will help you immensely in general: Dynamic Controls Viewstate

By the way, I am rusty in my ASP.NET but when I was a beginner (which I still basically am) most of my problems came down to one of two things: misunderstanding viewstate or misunderstanding how controls participate in the page life cycle. That's why I linked the two articles below which are by far the best reference materials I've found on those topics.

Upvotes: 1

Related Questions