views:

43

answers:

1

On PostBack, from clicking on an ImageButton, it first hits

protected void Page_Load(object sender, EventArgs e)

Then it hits

protected void ImageButton_Click(object sender, EventArgs e)

My problem is that in my Page_Load it refreshes a ListBox before the selected items can be processed by ImageButton_Click.

Is there a way to tell what events are yet to be processed, so I can handle them?

+3  A: 

Populate/databind your ListBox within Page_Load only on the first load, not after postback. Viewstate will maintain the items in your ListBox subsequently.

protected void Page_Load(object sender, EventArgs e) {

    if(!IsPostBack) //if not postback
    {
       //populate your listbox
    }

}

Here's a good read on the Page's Lifecycle, you'll understand the sequence/order of page/child-controls' events and their purposes.

http://msdn.microsoft.com/en-us/library/ms178472.aspx
o.k.w
Beat me to it! Great answer.
JMP
@JM: Thanks, quite a standard answer most guys will be able to give :P
o.k.w
The problem is that the Page_Load is that of a ascx custom control, so everything is postback -- Hmmm, I'll figure something out.
Matt
@Matt, if you're loading your ASCX in page_load, I'd refactor and let the ASCX control its own state rather than let the parent page control its state... if that's the problem... you might want to post a diff question to elaborate on your page's design.
JMP