advertisement

Forum => General ASP.NET => Page Load Occurs first on Button Click Event

Jump to:

Next Oldest | Next Newest

5/19/2005 10:39:12 AM Link | Reply | Edit | Quote

Devang

Profile Send Private Message Send E-Mail
Location: | Joined: 5/19/2005 | Posts: 10 | Offline

Hello!
I have generated a Table dynamically with several Buttons and showing rows from a Database.
Now,I am inserting a row on this Button Click event through a Button Click Event Handler.
Once i Click the button one row is inserted,but then when i Refresh the page
it again goes to Button Event Handler method and insert the same row again,even if i don't clicked the Button.
Kindly guide me.
Devang

5/19/2005 5:06:40 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

The behaviour you describe is by design. What exactly is your question? What's the actual business problem you're trying to solve?


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

5/19/2005 9:57:33 PM Link | Reply | Edit | Quote

du8die

Profile Send Private Message Send E-Mail
Location: Southeast Wisconsin | Joined: 1/16/2004 | Posts: 126 | Offline

You've got to check for a post back.

if not page.isPostBack then

do stuff

end if


The cause of the problem is:
The POP server is out of Coke .

5/19/2005 10:16:00 PM Link | Reply | Edit | Quote

Devang

Profile Send Private Message Send E-Mail
Location: | Joined: 5/19/2005 | Posts: 10 | Offline

Hello!
ya i want to differentiate between a button Click event and a Page Refresh.
I think, once a Button Event handler is executed it may be stored in cache,and on Page Refresh it is executed again.
So, how can i stop Page Refresh to go into Click Event handler.

Regards..
Devang

5/20/2005 10:11:06 AM Link | Reply | Edit | Quote

du8die

Profile Send Private Message Send E-Mail
Location: Southeast Wisconsin | Joined: 1/16/2004 | Posts: 126 | Offline

A button click (unless it is purely client-side) causes a page refresh. There's no way around that.

It would be helpful if you would post your code so we can see exactly what's going on in there and hopefully help you out.

d8


The cause of the problem is:
The POP server is out of Coke .

5/20/2005 10:29:11 AM Link | Reply | Edit | Quote

Devang

Profile Send Private Message Send E-Mail
Location: | Joined: 5/19/2005 | Posts: 10 | Offline

I can't able to give you the exact code, but
i am giving the flow of my code.


private void Page_Load(object sender, System.EventArgs e)
{
// Database Connectivity...
// Showing the Databse values in a Table Control..

// Generating Buttons...and showing them in Table in each Row...
// Written a Command Event Handler for Button...
//
}
private void EventHandler(object sender, CommandEventArgs e))
{
// Inserted a value in Database...
}

Now once i press a Button, it goes to the EventHandler and adds a Row
But, now this Sequence of events are stored in Cache...and thus when i refresh
the page, it follow the last sequence of events and thus goes to the EventHandler
and Inserts the same Row again...

Before pressing Button,when i refresh there is no problem,but once i press the
button and then refresh i got the problem..


help me...

Devang

11/11/2005 5:17:24 AM Link | Reply | Edit | Quote

jasmine

Profile Send Private Message Send E-Mail
Location: | Joined: 11/11/2005 | Posts: 1 | Offline

hai devang

I have a similar problem. Plz mail me the solution . I am Developer and in need the solution as early as possible.

plz help me,

10/17/2007 9:13:51 PM Link | Reply | Edit | Quote

domls

Profile Send Private Message Send E-Mail
Location: | Joined: 10/18/2007 | Posts: 1 | Offline

This issue is causing me a lot of problems. I know that you say it's "by design", but if so, then IMHO, that is really crazy. I've been programming about 20 years, VB/VB.NET for about 10, and recently, I've had the pleasure (?) of working with web application design for a few months now.

Some of the controls, such as text boxes, check boxes, etc allow you to set AutoPostBack to true or false. The button, on the other hand, does not. What is REALLY FRUSTRATING is that the click event behind the button IS FIRED AFTER RELOADING THE PAGE!!! It should be fired immediately, not after a Page reload. (Just insert a msgbox statement in the Page_Load event and one in the Button_Click event and you'll see that this is true).

Worse yet, the suggestion that is given here to check the Page.IsPostBack property DOESN'T WORK for Buttons!!! Again, you can test this by sticking a MsgBox in an "If Page.IsPostBack=True Then" statement in the Page_Load event!

So, if there's a workaround (without having to install & use AJAX or something else), please let me know. All in all, if this IS by design, then it's a very poor design (since when is programming illogical?).

10/18/2007 9:31:28 AM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

The design is correct and proper. I suggest reading up on the page lifecycle to understand why.


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

4/25/2008 2:22:57 PM Link | Reply | Edit | Quote

Jakub Keller

Profile Send Private Message Send E-Mail
Location: | Joined: 4/25/2008 | Posts: 1 | Offline

This isn't illogical by any means domls. Actually, it all makes a lot of sense. This is how the web works. Windows Forms development is a completely different animal. ASP.NET was designed to accommodate the way the web handles page requests and responses. Saying that, when anything in a form, submits information, like say a Button, you are literally submitting information back to the same page by a POST operation. Therefore, by submitting information back to the page, you are indirectly forcing the browser to refresh itself so it can consume the incoming response, which is based on the request you sent. On refresh, the page is then reloaded, causing the Page_Load to fire first and then any subsequent event handlers, like your Button's OnClick code. This is an exception with AJAX because then you are dealing with partial "post-backs", isolated to just a specific area on the page vs. the full-blown page. The back and forth request/response behavior allows us to provide server-side intelligence to our page and process the incoming request accordingly. Without a post-back your only means of interaction with the page is via client-side code.

10/2/2008 10:55:21 AM Link | Reply | Edit | Quote

bgx

Profile Send Private Message Send E-Mail
Location: | Joined: 10/2/2008 | Posts: 1 | Offline

The question doml asks here is not realy if it is logical that Page_Load fires before the button events but how to have code execude every time a page loads, taking into account possible button events that need to be processed before the general functionality is executed.

A way to do this is to put the code that is to be executed not into the Page_Load event handler but into one that is called after the postback events are processed, i.e. into the Page_LoadComplete event. However, it is not a stupid question to ask this because only for the Page_Load event a stub is inserted automatically when you create a new page in VS and I bet 98% of code samples or tutorials by default execute general code on Page_Load. And if you look up the page life cycle description as suggested e.g. at http://msdn.microsoft.com/en-us/library/ms178472.aspx it is still not that clear how to use another event properly.

So a solution to the problem in question would be :

private void Page_Load(object sender, System.EventArgs e)
{
// do something that is appropriate to do before
//button events are processed
}


private void Page_LoadComplete(object sender, System.EventArgs e)
{

// do things that depend on whether button events occurred or not

}

private void EventHandler(object sender, CommandEventArgs e))
{
// handle e.g. click events
} *** Edited 10/2/2008 2:58:30 PM UTC by bgx*** *** Edited 10/2/2008 2:59:59 PM UTC by bgx*** *** Edited 10/2/2008 3:09:35 PM UTC by bgx***

6/3/2010 4:16:48 PM Link | Reply | Edit | Quote

clownie126

Profile Send Private Message Send E-Mail
Location: | Joined: 6/3/2010 | Posts: 1 | Offline

As for domls

question, to avoid this simply redirect the page to itself at the end of the button click and the page will reload. A co-worker actually told me about this.


6/4/2013 8:45:25 AM Link | Reply | Edit | Quote

Anna Harris

Profile Send Private Message Send E-Mail
Location: | Joined: 5/15/2013 | Posts: 3 | Offline

It happens because the IsPostBack property of the page is true. Populate the table while the IsPostBack property is set to false.

Forum => General ASP.NET => Page Load Occurs first on Button Click Event

Please login or register to post.