Page Refresh issue

G

Guest

I'm having an issue with the "Refresh" of an asp.net page. The refresh is
actually calling my last onClick event. I thought that asp.net was supposed
to be stateless in that it shouldn't remember that I clicked a button before
the refresh. Here is what is going on:

1) Page Loads
2) User enters some values, clicks the "Save" button
3) The onClick event handler for the save button saves the data to the
database and the page is re-displayed with the new results.
*** Everything is fine up until point #4
4) I click the "Refresh" button on my browser (Internet Explorer v6.0).
5) As I step through the code, the execution goes through my Page_Load event
hanlder and my btnSave_onClick event handler.

I don't think it should ever return to my onClick event handler when I
refresh the page. Is their some type of setting or configuration I need to
check. At the page level, I have EnabledViewState = true.

Additional Information (if necessary):
Here is the declaration of my control & my event handler if this helps.
Button declaration in asp.net page:
<asp:Button ID="btnSave" Runat="server" Text="Save" onClick="btnSave_Click"
/>

onClick event handler in code behind page
public void btnSave_Click(object sender, System.EventArgs e)
{
//......
}
 
J

Jakub

Firs you have understand what postback really does. Simply: when you click
un a button causing postbact, javasript stores this information in some
hidden fields in pages form (__EVENTTARGET, eventartgs ....) and POST the
form to server. When server handles request, parses this values and when
find right value, process the event handler. When you click Refresh, browser
will send again the same data as before, so your handlers are executed
again.

You can add some hidden field with for example Guid or datetime, and when
you wil be processing request with same guid again, you know that user
refreshed the page
 
G

Guest

I'm not sure I unserstand everything, but what it sounds like you're saying
is that the page refresh is functioning correctly. Is that true? Is it true
that the onclick event handler which was last initiated will again be
initiated on a page refresh? Maybe I'm wrong, but I was told by others that
ONLY the Page_Load should be kicked off when I refresh the page, not the
Page_Load + the event handler.

I've tried to use hidden fields to store a text value indicating that a
"page mode". I set the hidden textboxes value to something after save is
clicked (thinking I'll be able to check for it again at the beginning of the
onClick event handler). However, when refreshed, the origninal value (empty
string - "") is returned. The page refresh grabs what "WAS" stored in the
textbox, not what "IS".
 
M

MWells

Regarding state, the ViewState technology is designed specifically to store
the state of your controls, so that you don't have to persist and
re-populate them on every page refresh. So the contents of your textboxes,
the selection of your comboboxes, etc. should all carry through the postback
when ViewState is enabled.

Regarding the refresh behavior, it's somewhat browser-dependant. IE knows
that you last requested the current page using an HTTP-POST (since you
clicked the Save button and a postback occurred); and it also remembers the
contents of the POST. When you refresh, IE normally will warn you that a
refresh will cause the same form data to be resubmitted; my guess is that
you've disabled this warning in IE's advanced settings.

In short, the behavior you're seeing is entirely normal and is working
exactly as intended.
 
M

MWells

I'm not sure I unserstand everything, but what it sounds like you're
saying
is that the page refresh is functioning correctly. Is that true? Is it true
that the onclick event handler which was last initiated will again be
initiated on a page refresh? Maybe I'm wrong, but I was told by others that
ONLY the Page_Load should be kicked off when I refresh the page, not the
Page_Load + the event handler.

It is behaving correctly. On the very first visit to the page, Page_Load
executes and hitting refresh will execute ONLY Page_Load. If you do a
postback event (e.g. click a button), then the browser does an HTTP-POST.
Refreshing that resulting page (under modern browsers like IE) will cause
the full header to be resubmited, and ASP.NET will follow the same execution
path; Page_Load first, and then your Button_Click event.
 
C

Chad Devine

If you click off the page and go to a seperate website, and then come
back to the page via link or typing it in the url you will see that it
won't have the same data but the normal generic view it should have.
Without any saved form data.
 
G

Guest

Thank you for the in depth explanation. That helps a lot, I've created a
workaround based on the information you gave.
 
J

Joerg Jooss

Chad said:
If you click off the page and go to a seperate website, and then come
back to the page via link or typing it in the url you will see that it
won't have the same data but the normal generic view it should have.
Without any saved form data.

That's because these operations use HTTP GET.

Cheers,
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top