Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why a button reloads the page ? 1

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,
i made a web page, with a save button that acts differently.(depends on the Session("CurrentMode"))

As i don't know asp.net controls very well my question could seem basic but not for me :

i have noticed that when i click on the save button (in debug mode) the code returns in the Page_Load BEFORE to deal with the btnSave_Click event.
Why is the page loading again with a simple click ?
I thought it was a simple button property, i made many test, but my page is still reloading before to catch the btnSave_Click event. I also tryed to set the document's smartNavigation property to true, but nothing changed.

How could i disable this ? I just want to catch the btnSave_Click, i don't want the page to be loaded again.

Best regards,
Elise, XML girl X-)
 
Hi Elise
That is just the way that ASP.net works. You've got two choices. 1 - if the functionality that you want to happen when they press the Save button can be accomplished by pure client side code (ie javascript or vbscript running in the browser), then use a HTML button rather than a web form button and code up the on_click event in the client code.

2 - it does not sound like this is suitable for you, since you are referring to a server object (the session object). In this case, you have to put something in the page_load event to prevent the undesirable code from being executed simply because they pressed a button. The simplest way is probably to put

if not page.ispostback then
'your page setup code here
end if

and to make sure that all controls initially set up have enable viewstate true so that they preserve their state between postbacks

Finally, note that for any server code to execute, you must perform a postback - that again is the way asp and asp.net works.

Hope this helps Mark [openup]
 
Required reading for any new asp.net developer:


Understanding the basics of the page lifecycle, and in what order the events fire is absolutely key to being able to get the most out of your apps.

Enjoy! :)
paul
penny1.gif
penny1.gif
 
thanks,
i don't like this way asp.net works but i'll try to find a way... Best regards,
Elise, XML girl X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top