+1  A: 

Problems of this kind are almost always a result of wiring up the event handlers in the wrong place in the page lifecycle.

Ben Collins
I mean i add the event handler in the onInit Method with same items. Till now it worked fine with repeater, datagrids etc. But now i do not know how to solve this.
Xavier Hayoz
You may need to do the wireup in a different place now. I had a similar problem a couple weeks ago, and I eventually solved it by trying out the event wireup in different places. It's a tricky thing because not every control behaves in the same way.
Ben Collins
What does "AddEvents();" do? Isn't it rebinding all events? Would better fit in (!Page.Ispostback) i think
balint
I've got to buttons defined on the aspx page (back btnBack.Click+=btnBack_Click;}
Xavier Hayoz
+4  A: 

OnInit happens before LoadPostData is called, and this is the reason it appears to work on the second click. What's actually happening is that you are handling an event from a previous postback.

  1. Page_Init
  2. LoadViewState
  3. LoadPostData
  4. Page_Load
  5. RaisePostDataChangedEvent
  6. RaisePostBackEvent
  7. Page_PreRender
  8. SaveViewState
  9. Page_Render
  10. Page_UnLoad

This is a simplified version of events from Asp.net 1.1; 2.0 has some extra events inserted inbetween, like OnPreInit. The order remains though.

You must use an event from Load or later. Some people choose PreRender, but convention for DataBinding is in Load.

UPDATE

I see you are dynamically adding controls to the controls tree. This is fraught with pitfalls (but is perfectly workable.)

a) ensure you add the dynamic controls EVERY time the page loads, not just on postback. Postback/viewstate persists STATE (i.e. data), not objects. You should add them in OnInit (OnLoad works too, but it is more troublesome to maintain). You are currently only adding on postback, this is why the 2nd postback works (and not the first)

b) only initialize the dynamic controls ONCE, like you would any other control - on [!IsPostBack], in Load.

-Oisin

x0n
I added my code from OnInit in Page_Load(). Still same issue...
Xavier Hayoz
Are all of the controls involved declarative in the aspx/ascx? i.e. you are not dynamically adding controls somewhere in the code? If you dynamically add controls, things get hairier still.
x0n
Ah, I see you ARE doing that. If you dynamically add controls to the page, you must ensure they always exist in the control tree before you can handle an event for them.
x0n
To further clarify, make sure you add the controls to the controls collection EVERY time the page loads (regardless of PostBack state), in OnInit. But only set the DEFAULT values - if needed - the first time [!PostBack]
x0n
Thanks for your answer. But i couldn't resolve the problem.
Xavier Hayoz
protected override void OnInit(EventArgs e) { Presenter = new DynamicGridPresenter(this); if (!IsPostBack) { SetSubmenuVisible = false; Presenter.InitView(); } PrepareView(); }
Xavier Hayoz
1. On postpack i rebuild the site with the items stored in the session. --> adds event handler to my programmatically added buttons. --> PrepareView();2. The event handler is called by the button with the needed command argument.3. A Query is executed on db to get new items with same command argument and stored in session4. The new view is builded with the new buttons --> PrepareView()5. Page is sent back to clientI need to call the PrepareView() twice. The first call is to know which event has been fired. The second call is needed to build the new html table with the buttons.TIA
Xavier Hayoz
I cant really spend the time to understand what you're doing - ultimately, whatever it is, it is wrong. What you need to do is to read up on the correct methodology and apply it to your problem. Grab a coffee and take a read of this great article on the subject:http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
x0n
Anyway-Thank you very much for your support. I'll read the article. Hope it helps. Since 2 days on this *#¦@~^ issue.
Xavier Hayoz
A: 

Hi all!

Thank everbody for helping me out. I found the solution:

I didn't know that the dynamically created button needs a ID value. I suggested that Asp know wich button is created. Unfortunately i was wrong. It seems that .NET compares the ID on the buttons and not the position in ControlTree.

I added

new Button{ID = itr.ToString()}

Now when server rebuilds the site, eventhandler are hitted.

regards x.

Xavier Hayoz
A: 

I just had a similar problem. I tried lots of solutions, but in the end I noticed that one of the textboxes on my webform was posting back to the server, which messed things up. It was a hard bug to catch because I didn't mean to have my textbox do that.

John Fischer