Handling events and postbacks in ASP.NET

J

JezB

It strikes me as odd that when I attach an event to a button webcontrol (for
example), the page posts back before the event is fired - ideally I'd like
it to post-back after my event code has executed, so that the re-posted page
reflects any results of that code.

Can the default behaviour be changed ? Why does it do it this way ?
 
P

Peter Morris [Droopy eyes software]

It strikes me as odd that when I attach an event to a button webcontrol
(for
example), the page posts back before the event is fired - ideally I'd like
it to post-back after my event code has executed, so that the re-posted page
reflects any results of that code.

1) Page posts back the view state to the server.
2) The page properties are reconstructed from the view state
3) The event is triggered

This makes sense, imagine a button.Click event like so
button1.Text += "X";

If 2 and 3 were swapped, you would just get "Button1X" each time. With them
the way around they are you get an additional X each time as desired.

Can the default behaviour be changed ? Why does it do it this way ?

Maybe your approach to your problem is incorrect? What exactly are you
trying to do?
 
J

JezB

Well the details are in my previous post - my button event code is modifying
the controls collection on the page. The problem is that because of the
order things are fired, Im not seeing the changes it makes - so I either
wanted to change the order of things (tho I accept your explanation why this
isn't feasible), or force another postback after my code so that my changes
are reflected on the page.
 
S

Steve Caliendo

It is a bit inconvenient to have to run through the page load command before
the button code, but it cannot be changed. You need to think "stateless"
when coding on a web page - very different than classic VB. I'm sure you
can accomplish what it is you need to do if you change your mindset when
coding for the web. Good luck,

Steve
 
J

JezB

That's my problem, since my background is windows programming. I solved my
problem by putting some code in a PreRender event, though Im not sure my
solution is as efficient as it could be.

Steve Caliendo said:
It is a bit inconvenient to have to run through the page load command before
the button code, but it cannot be changed. You need to think "stateless"
when coding on a web page - very different than classic VB. I'm sure you
can accomplish what it is you need to do if you change your mindset when
coding for the web. Good luck,

Steve


JezB said:
Well the details are in my previous post - my button event code is modifying
the controls collection on the page. The problem is that because of the
order things are fired, Im not seeing the changes it makes - so I either
wanted to change the order of things (tho I accept your explanation why this
isn't feasible), or force another postback after my code so that my changes
are reflected on the page.

"Peter Morris [Droopy eyes software]"
wrote in message news:[email protected]... I'd
like With
them
 
A

ashelley

"change our mindsets"....
true, true indeed

:))

but let's consider my problem: How can I set focus on a control after a post-back?
Let's say I have a form with two textboxes. When I hit enter on the 1st one, post-back is performed (automatically, unfortunately -but it's ok in this case), some server code is executed and back to the html page again. Back hey! where is the focus? It's nowhere, of course. But I want it to be on the 2nd textbox! It's a form after all, isn't it? And users are supposed to fill in forms in an orderly manner.

Well, any solution to this one?

dimitris

create a blank label at the bottom of the form.
in the postback routine that handles the enter set the value of the
label to javascript that will setfocus on the required control.

-Adam
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top