|
-
Apr 23rd, 2003, 12:55 PM
#1
Thread Starter
Addicted Member
simple question really - page load and events
folks -
My simple question is this. Lets say i have a form (asp.net) containing a asp:button. User presses the button, and creates a file on a directory on the server . Obviously the action takes place within the button click handler (a createFile function is called from here). Why oh why oh why does the file get created when the button is pressed AND each time the page refreshes/reloads??
It's like the buttonclick event is raised twice or something. Once when the page loads and once when the user (legitimately) creates the file through the button click...hmmmm!
cheers for your thoughts...
-
Apr 23rd, 2003, 01:13 PM
#2
Frenzied Member
It should not happen that way. The directory should only be created when the button is clicked.
Dont gain the world and lose your soul
-
Apr 23rd, 2003, 02:06 PM
#3
that why you use
If IsPostback Then
' The form is submitted via post and is not a refresh.
End if
-
Apr 23rd, 2003, 05:45 PM
#4
Thread Starter
Addicted Member
DevGrp - that's the way i see it...but it is ??!!
Cander - i understand (perhaps not ) the difference (postback/refresh), the thing is that either the function/or the button click event seems to be getting called/raised regardless. See the sort-a-code...
Code:
'
sub createFile(path)
filestream.write(path,"WHY DOESN'T THIS WORK")
end sub
'
sub buttonClickHandler
createFile("c:\test.dat")
end sub
'
everytime i refresh/ reload the page the file test.dat is created. I delete it, open the page and before i even get the chance to click the button the file is there. Surely i dont have to wrap every function call in a IsPostBack test...
What am i overlooking?? must be a no-brainer surely.
Fisrt to answer wins an e-beer...
Last edited by powdir; Apr 23rd, 2003 at 05:52 PM.
-
Apr 24th, 2003, 01:42 PM
#5
I'm not sure if this relates or is helpful but read this:
http://msdn.microsoft.com/library/de...EventModel.asp
If you do set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. This can result in the same event code being called twice when the page runs. As a consequence, you should always leave AutoEventWireup set to false when working in Visual Studio.
-
Apr 24th, 2003, 01:48 PM
#6
Addicted Member
If you refresh the page AFTER the postback, each refresh is posting back to the server again.
(please take my half-baked pseudo http syntax with a grain of salt while I try to illustrate my point . . .)
For instance.... first request to the page is:
http 1.1 GET somepage.asp
Then you click the button and the request is:
http 1.1 POST somepage.asp
<data>
If you refresh now that you have clicked the button, it run the same request again:
http 1.1 POST somepage.asp
<data>
This kind of behavour occurs with other types of forms as well (asp 3.0, cgi, php, whatever), it's the nature of the browser.
At least, that is my understanding based on what you were saying. If you load the page and then refresh the page (without clicking the button), it should refresh the page but it would not do a postback, so it would not create the file.
What you could do is look to see if the file exists before creating it.
Wydok
"It would appear that we have reached the limits of what it is possible to achieve with computer technology, although one should be careful with such statements, as they tend to sound pretty silly in 5 years."
-John Von Neumann ca. 1949
-
Apr 24th, 2003, 05:09 PM
#7
Thread Starter
Addicted Member
Edneeis - i really, really wanted it to be this, but on inspection the AutoEventWireup is set to FALSE...DAMN, DAMN.
Wydok - if i understand you correctly is this just something web coders have to just live with???
I don't get it all i want to do is create a file on the server if one does not exist, if it does then append some text to it - simple. What i get now is a new file on each refresh whether the user presses the create file (which calls the createFile function!) button or not AND if the user does press the button and post duplicate entries are added to the file as in
HELLO WORLD
HELLO WORLD
I also had this problem writing records to a database via asp.net - i.e. duplicate records were written. I had to use an ugly DB fix to prevent PK violations.
Coming from a desktop background and fairly new to the web side of things i am a bit baffled. Please folks make me look like an idiot and help me out here before HULK SMASH PUNY COMPUTER!!
one button, one event, one function, one write...RIGHT.
Cheers
-
Apr 24th, 2003, 07:43 PM
#8
PowerPoster
Post all your code for the page that way we can completly look at it.
-
Apr 26th, 2003, 12:17 PM
#9
Thread Starter
Addicted Member
**DING**
Bang on Wydok, once my confusion settled down down and i actually took the time to have a proper read through yer post i realise that this is exactly whats happening - the disconnected nature of the web baffled me here and i had equated a refresh to a 'kinda' windows repaint - I see that i just have to code around this...
Any good strategies in use for dealing with this kind of thing, other than brute force checks and code gates?
Cheers all...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|