Event Sequence

Discussion in 'ASP .Net Web Controls' started by Axel Dahmen, Dec 22, 2005.

  1. Axel Dahmen

    Axel Dahmen Guest

    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
     
    Axel Dahmen, Dec 22, 2005
    #1
    1. Advertising

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

    within the RemoveButton_Click add at the end the databind statements

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Axel Dahmen" wrote:

    > 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
    >
    >
    >
     
    Phillip Williams, Dec 22, 2005
    #2
    1. Advertising

  3. 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

    Axel Dahmen wrote:
    > 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
     
    Mike MacMillan, Dec 29, 2005
    #3
  4. Axel Dahmen

    Axel Dahmen Guest

    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


    -----------
    "Phillip Williams" <> schrieb im Newsbeitrag
    news:...
    > Within the Page_Load:
    > if (!Page.IsPostBack)
    > {
    > //do the databind
    > }
    >
    > within the RemoveButton_Click add at the end the databind statements
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Axel Dahmen" wrote:
    >
    > > 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
    > >
    > >
    > >
     
    Axel Dahmen, Jan 8, 2006
    #4
  5. Axel Dahmen

    Axel Dahmen Guest

    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



    -------------
    "Mike MacMillan" <> schrieb im Newsbeitrag
    news:...
    > 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
    >
    > Axel Dahmen wrote:
    > > 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

    >
     
    Axel Dahmen, Jan 8, 2006
    #5
  6. 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



    Axel Dahmen wrote:
    > 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
    >
    >
    >
    > -------------
    > "Mike MacMillan" <> schrieb im Newsbeitrag
    > news:...
    > > 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
    > >
    > > Axel Dahmen wrote:
    > > > 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

    > >
     
    Mike MacMillan, Jan 9, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry

    PostBack Event Firing Sequence

    Guadala Harry, Aug 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,095
    Bredal Jensen
    Aug 15, 2004
  2. Josh

    Event Sequence Wrong

    Josh, Aug 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    Eliyahu Goldin
    Aug 17, 2005
  3. =?Utf-8?B?SmF5?=
    Replies:
    3
    Views:
    975
    Kevin Spencer
    Dec 21, 2005
  4. Phillip N Rounds

    Question re Event Firing Sequence

    Phillip N Rounds, Mar 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    411
    Eliyahu Goldin
    Mar 5, 2006
  5. stef mientki
    Replies:
    13
    Views:
    695
    stef mientki
    Oct 20, 2007
Loading...

Share This Page