Event Sequence

A

Axel Dahmen

Hi,

in ASP.NET control events are being fired *after* the Page_Load event. I
need it the other way around. Is this possible?

Here's my example (just pseudo code):

Page_Load()
{
DataGrid.DataSource="...";
DataGrid.DataBind();
}

RemoveButton_Click()
{
SqlCommand.CommandText="DELETE ... WHERE ...";
SqlCommand.ExecuteNonQuery();
}

If I hit the RemoveButton during runtime, the DataGrid will display obsolete
data, because it is bound *before* the RemoveButton_Click() event is called.

AFAIK, performing DataGrid binding within the Page_Load event is the only
practicable way as there is no other event being called if not on PostBack.
If I have several buttons on the page, I don't see another way to put the
DataSource/DataBind pair somewhere else.

Your help is quite appreciated.

TIA,
Axel Dahmen
 
P

Phillip Williams

Within the Page_Load:
if (!Page.IsPostBack)
{
//do the databind
}

within the RemoveButton_Click add at the end the databind statements
 
M

Mike MacMillan

Axel,
all postback events are fired right after the Page.OnLoad event by
design. since you are using the Page_Load event, this means you're
hooking into the Page.Load event. after Page.OnLoad fires, it then
fires the event on all delegates...so if doing your databinding before
Page_Load is what you need, you can override the Page.OnLoad event, do
your binding, then call base.OnLoad to pass it back to the page. there
is also the Page.OnInit event which fires before OnLoad (and
LoadViewState, etc...) which you can override/hook into for your
databinding as well.

let me know how this works,
Mike MacMillan
 
A

Axel Dahmen

Thanks, Phillip, for trying to help.

You are right, this is the only feasible way I know of, too, but - no
offence - I believe this is spaghetti code. There must be a structured way
of databinding and data manipulation. Without using IF and IF and IF... at
all different places.

BTW: I have this same problem with sorting a DataGrid: If !IsPostBack and
NOT one of the sorting header links is clicked, perform DataBinding in
PageLoad() - OTHERWISE perform databinding in DataGrid_SortCommand(). I even
have to address the sorting links by their HTML names!! THIS is really ugly:

PageLoad()
{
if (Request.Form["__EVENTTARGET"]==null ||
!Request.Form["__EVENTTARGET"].StartsWith("grdData:_ctl2:_ctl"))
FillDataGrid();
}

Do you perhaps see another way to perform this kind of action in a
structured manner?

Best regards and happy new Year 2006!
www.sportboatcharter.com
Axel Dahmen


-----------
 
A

Axel Dahmen

Hi Mike,

thanks for trying to help. In fact I want to perform something different:

Let me guide you to my point: ASP.NET provides control events, BUT... they
only fire AFTER all available page event have been fired. So, AFAIK, there
is no page event available to perform some general action in a structured -
means organized - way after control events have been handled.

I'm missing a second "PageLoad()" event being fired AFTER all control events
have been fired, like:

PageInit()
PageLoad()
Control2_Click()
PageLoad2()

This would keep a programmer from using IFs and function calls at several
places, distributing logic across all page code.

I'm in the mood, so I speak right from my heart: IsPostBack is a really
ugly, unstructured feature leading to immense spaghetti code to solve
problems raised by this feature. IMHO, the IsPostBack property should
instead have been a separate event, fired right before any control event.
And the PageLoad() event should instead fire right AFTER any control event,
like:

PageInit()
PagePostBack()
Control2_Click()
PageLoad()

Another ugly solution is the way of how to implement Dispose() as suggested
by MSDN.

It seems some solutions never got beyond VB5... *sigh*

OK, enough... Do you see any structured solution to this problem? I'd
greatly appreciate that.

TIA,
www.sportboatcharter.com
Axel Dahmen



-------------
 
M

Mike MacMillan

Axel,
per the execution lifecycle, you have the PreRender event which you
can use, that gets fired right after Load...ex:
Init
Load
PreRender

is there any reason you need your logic bound to the Load event, or
will using the PreRender event suffice? perhaps you can explain a
little more in detail why you've chosen this specific architecture, and
we can come up with a workaround/solution.

Mike MacMillan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top