viewstate - events - page lifecycle

P

Petr SIMUNEK

I have 3 buttons on the page. (Created dynamicaly inside For- next loop and
hooked up to click event.)
When different button is clicked i would like to save a different value to
viewstate.
In the same run I want to read this value from viewstate and load page back
with different picture..


the problems are:
- events from button fires after pageLoad is finished, so I can use it to
decide
- the same goes with viewstate, its changed after page is finished so
unusable

this works but causes two postback delay :O)

I know this is by design in asp.net page lifecycle, so my question is how is
such thing supposed to be done ?

Thanx and sorry for repeating ....
This must have been here 100s times. I just didnt find simple ansver to
this..(and i did look)

Pettt
 
C

cbDevelopment

To process this correctly, you just need to organize your code properly.

In your Page_load:

If not ispostBack then
' Do stuff only when page first loads
' Can call the display pictures method using a default value
' like (page) 1, or whatever
' There is no viewstate on first load, so nothing you can do
' in this method will matter
else
' Do nothing, the postback events will handle it
end if

In your button clicks:

private sub click(s,e) handles btn.click
' Save your Viewstate value
' Call whatever methods use the viewstate values
end sub

Does this mean you have to have a handler method for each button? Maybe.
If you don't have specific code for each button you can do:

private sub AllClick(s,e) handles btn1.click,btn2.click,btn3.click
' Save viewstate
' Call methods that use viewstate
end sub

If you have specific code for each, you can have two handler methods one
specific to the button and one for all the buttons, but you can't
guarantee which will run first. This can still work in some cases, like
disabling controls based on a specific button click, but all the buttons
perform a common action.

The root of the problem is trying to do too much in the Page_load. Put
the code in other methods where handler methods can call them.

Hope this helps.
----
700cb Development, Inc.
http://www.700cb.net
..NET utilities, developer tools,
and enterprise solutions

I have 3 buttons on the page. (Created dynamicaly inside For- next
loop and hooked up to click event.)
When different button is clicked i would like to save a different
value to viewstate.
In the same run I want to read this value from viewstate and load page
back with different picture..


the problems are:
- events from button fires after pageLoad is finished, so I can use it
to decide
- the same goes with viewstate, its changed after page is finished so
unusable

this works but causes two postback delay :O)

I know this is by design in asp.net page lifecycle, so my question is
how is such thing supposed to be done ?

Thanx and sorry for repeating ....
This must have been here 100s times. I just didnt find simple ansver
to this..(and i did look)

Pettt



--
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top