Bouncing Ma'ss

Techinical bits from a Bouncing Fish.

Thursday, March 09, 2006

ASP.NET Event firing

How do you spell firing?

Anyway, recently I had a baffling problem with ASP.NET which when resolved changed my understanding of the ASP.NET Event Firing Sequence. (Fixed it if you will).

I had a GridView. I as was adding buttons to a cell of the GridView .
Problem:
  • Data-Binding only occurs on the !IsPostBack load of a page.
  • Dynamically added controls do not persist over post backs like a controls value (or a GridViews rows) do.*
As my controls were being added by RowDataBoundEvent, they'd disappear unless I explicitly called DataBind in Page_Load.

Here's the event conundrum that had me for a while and taught me the lesson.
My dynamic buttons survived the first load so I didn't muchly care they disappeared after clicking them, well I did but I was still perplexed as to why they didn't actually do their work.

I expected adding a handler to the dynamic button to give it Client side button something like this:

onclick="do what you're told"


But it didn't.
Why?
Because it does this.

onclick="tell me you were told to do something"


Then when the postback happens. ASP.NET rebuilds the page, figures out who's interested and calls them. Therefore, as my dynamic buttons weren't put back on the page (as there was no databind()) this happened:

onclick="I got clicked"
asp="I don't know who you are, sorry"


I don't think I like ASP.NET because it does such simple thing in such convoluted ways. I understand its a result of the internet and not ASP.NET's fault. I just don't like it.

I'm going to install Ruby On Rails.

*This to me says that a GridView is bound once when its loaded and once again when it fires an event. Hmmmmm

1 Comments:

At 9:04 am, Blogger Shak said...

I never understood the importance of "HTTP is stateless" till I had this problem. Once you remember that though everything else falls in place.

 

Post a Comment

<< Home