CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.web_forms Tags:
Item Type: NewsGroup Date Entered: 7/22/2005 9:38:45 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 17 Views: 0 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
18 Items, 1 Pages 1 |< << Go >> >|
gardener01
Asp.Net User
Why Page_Load is called before Button_Click is called?7/22/2005 9:38:45 PM

0/0

I need to update my related database through a button click. What I want is, after I update the database, a message is postbacked and my page is freshed based on the updated database. However, what I get is, after I click the control, a message is sent back to the server, and the page get refreshed before my database get updated. Therefore, the change in the database does not show up in my page. I put break point in both Page_Load and Button_Click methods, the program stopped first at Page_Load and then at Button_Click. Do I have to manually ask the page to refresh itself after the database is updated? Or is there any better way? Thanks! 

JimmyM
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/22/2005 10:03:37 PM

0/0

May be you need to load your data again after the database is updated.


This posting is provided "AS IS" with no warranties, and confers no rights.
wessamzeidan
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/22/2005 10:05:28 PM

0/0

not sure what you're trying to do, but you can use response.redirect to redirect to the same page after you do the updates
Wessam Zeidan
Tom Ruby
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/22/2005 10:48:48 PM

0/0

Yup. That's what happens alright. Annoying, isn't it?

Move all your database stuff out of the postback method into another. I usually call it "BindTheData."

Then, in the postback, put code like this:

IF NOT Page.IsPostback Then
       BindTheData()
       End If

Now, your onclick handler can update the database, but it won't show in the posted back form unless you have it call BindTheData() after it's done updating.

gardener01
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 2:55:28 AM

0/0

Thank you all, I appreciate your suggestions. BindTheData probably is what I want. I will try it.
gardener01
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 6:08:03 AM

0/0

It looks like it does not work.

IF NOT Page.IsPostback Then
       BindTheData()
End If

After the button is click, the program goes to Page_Load method first and bypass BindTheData() because of postback. Then the program goes to Button_Click method. However, the database does not get updated!

If BindTheData() does get called in Page_Load, the database will be updated in Button_Click. However, even if BindTheData() is called at the end of Button_Click after database is updated, the page did not reflect the change of the database unless the refresh button of the browser is clicked!

Can Page_Load method be called by our code(not by event handler)?

gardener01
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 6:43:18 AM

0/0

 wessamzeidan wrote:
not sure what you're trying to do, but you can use response.redirect to redirect to the same page after you do the updates


What I am trying to do is: Click a button, then Button_Click method gets called. In Button_Click, update my database. Then load the same page again. I hope the newly loaded page can reflect the recent change of the database. However, I found the content of the page is not updated unless I manually click the refresh button of the broswer. Is there any way better than redirect to the same page? Thanks!
joteke
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 9:08:19 AM

0/0

Hi,

the logic could be pretty much like this:

Sub Page_Load
   If Not Page.IsPostBack Then
      BindTheData()
   End If
End Sub

Sub Button_Click
   UpdateData()
   BindTheData()
End Sub

E.g for initial loading of the Page load the data to be visible. On Button_Click update database and refresh the display by rebinding the data, similarly as you did in Page_Load to show it initially. No redirecting is needed. The logic in Page_Load inside Not Page.IsPostBack check doesn't run because we are executing on postback already. Therefore, just binding after update as been made is working solution.

Updatedata() is the method/block which does updating to the database.

Makes sense?
Thanks,

Teemu Keiski
Finland, EU
XIII
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 10:51:46 AM

0/0

 gardener01 wrote:
I need to update my related database through a button click. What I want is, after I update the database, a message is postbacked and my page is freshed based on the updated database. However, what I get is, after I click the control, a message is sent back to the server, and the page get refreshed before my database get updated. Therefore, the change in the database does not show up in my page. I put break point in both Page_Load and Button_Click methods, the program stopped first at Page_Load and then at Button_Click. Do I have to manually ask the page to refresh itself after the database is updated? Or is there any better way? Thanks! 


Hi,

first create a method where you bind your data (fresh from the database) to your control. Call this method in the page_load event surrounded with a check for a postback:

if(!Page.IsPostBack)  C#
if Not Page.IsPostBack Then  VB.NET

In the button_click eventhandler you can call this same method again so you know that only the first time you call your page the grid is binded to the data and after that on every button click.

Also learn about the life cycle of a webform and know what event to expect when.

Grz, Kris.
Kris van der Mast [MVP] || 101 LINQ to SQL samples
gardener01
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 5:39:43 PM

0/0

Thank you for your suggestion, Grz and joteke.

Of course I understand your logic. However, I am not sure it you guys test your method or not. In one of my previous post of this topic, I have already mentioned that I tried this way, it just does not work. I do not understand the reason since your logic is clear and reasonable.

After the button is clicked, the program goes to Page_Load method first and bypass BindTheData() because of postback. Then the program goes to Button_Click method. However, the database does not get updated!

If BindTheData() does get called in Page_Load, the database will be updated in Button_Click. However, even if BindTheData() is called at the end of Button_Click after database is updated, the page did not reflect the change of the database unless the refresh button of the browser is clicked!

Anyway, I will read life cycle of a webform and try to understand the issue.

joteke
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?7/23/2005 6:31:08 PM

0/0

 gardener01 wrote:

Thank you for your suggestion, Grz and joteke.

Of course I understand your logic. However, I am not sure it you guys test your method or not. In one of my previous post of this topic, I have already mentioned that I tried this way, it just does not work. I do not understand the reason since your logic is clear and reasonable.

After the button is clicked, the program goes to Page_Load method first and bypass BindTheData() because of postback. Then the program goes to Button_Click method. However, the database does not get updated!

If BindTheData() does get called in Page_Load, the database will be updated in Button_Click. However, even if BindTheData() is called at the end of Button_Click after database is updated, the page did not reflect the change of the database unless the refresh button of the browser is clicked!

Anyway, I will read life cycle of a webform and try to understand the issue.



OK, so you confirmed that the update went to the database but it just wasn't updated on the page after that? Can you show a bit of that real code of yours? Have you tried disabling page output caching (put < %@Outputcache Location="none"% > directive on top of the aspx page in question).
Thanks,

Teemu Keiski
Finland, EU
monolithx
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?12/2/2005 12:31:30 AM

0/0

Isn't it true that actually Button_Click is executed AFTER Page_Load?
Considering that, I doubt it is possible that you see results on that occasion.
To see results you should, in button event, do Response.Redirect(...) after you update database to refresh the data by putting BindFreshData(...) in Page_Load  that would actually reflect new changes..

That's my logic as I have the similar problem because I don't know a way to run buttonclick code before Page_Load...

XIII
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?12/2/2005 8:05:12 AM

0/0

monolithx wrote:
Isn't it true that actually Button_Click is executed AFTER Page_Load?

Yes, that's correct. Take a look at the page life cycle.

Grz, Kris.


Kris van der Mast [MVP] || 101 LINQ to SQL samples
tarjei
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?12/2/2005 10:03:25 AM

0/0

gardener01 - Why don't you just move the databinding code to Page_PreRender instead of Page_Load? The prerender event fires after the button's click event.

--
Tarjei
gardener01
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?12/2/2005 3:03:00 PM

0/0

Thanks.
tetsujin
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?1/3/2006 10:21:04 PM

0/0

That's what I always use.  If I'm updating data that I need to see instantly I put all of the loading into PreRender.  The events get fired and executed THEN it refreshes the data.

The life cycle of an ASP.NET page can be confusing at first but once you get the hang of it it's immensely powerful and useful.  I still remember one of the most maddening things was wondering why the hell dynamically created User Controls wouldn't fire off their events when I thought I had triggered them.  I still get tripped up from time to time.  Master Pages fire off their Init before any child pages are built but they fire off Load after their children do.  Fun stuff =)
Luiscotiquicia
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?1/3/2006 10:56:25 PM

0/0

joteke wrote:
Hi,

the logic could be pretty much like this:

Sub Page_Load
   If Not Page.IsPostBack Then
      BindTheData()
   End If
End Sub

Sub Button_Click
   UpdateData()
   BindTheData()
End Sub

...

Makes sense?

Well, I had a similar issue with that, but that logic worked for me...

Here's the code I used:

**********************************************
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '* Carga solamente una vez el formulario   / Loads only once the form (Listbox)
        If (IsPostBack = False) Then
            Call CargarTemasInteres()
        End If

    End Sub

************************************************
' Update Button

 Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click

        If (lstCargador.SelectedIndex <> -1) Then

            Call modificarTema(CInt(lstCargador.SelectedItem.Value), CStr(txtInsertarTema.Text))

        End If

    End Sub

********************************************

    Private Sub modificarTema(ByVal intCodigoTema As Integer, ByVal strDescripcion As String)

        '* Consulta SQL para actualizar el item seleccionado en los Temas de Inter?s
        Dim strSQL As String = "Update .... "

        Try

            MyConnection = New SqlConnection("...")

            MyConnection.Open() 'Abre la coneccion
            Dim cmdCommand As SqlCommand = MyConnection.CreateCommand() 'Crea un objeto command

            cmdCommand.CommandText = strSQL
            cmdCommand.ExecuteNonQuery() 'Ejecuta la consulta sin devolver resultados

            '* Carga de nuevo el ListBox
            Call CargarTemasInteres()

        Catch ex As Exception 'Captura la excepciones que se puedan presentar

            Dim strError As String = String.Empty  ' Guarda el error que se pueda generar
            strError = ex.Message 'Mensaje de error

        End Try


    End Sub

********************************************

That's some of my code, and it work...  I hope this helps!!!

~ Luiscotiquicia


_____________________________________
Programador
Motley
Asp.Net User
Re: Why Page_Load is called before Button_Click is called?1/4/2006 7:12:03 AM

0/0

The logic flow as previously stated should/does work, I do it all the time.

Sub Page_Load
   If Not Page.IsPostBack Then
      BindTheData()
   End If
End Sub

Sub Button_Click
   UpdateData()
   BindTheData()
End Sub

18 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Professional C# 2008 Authors: Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Pages: 1782, Published: 2008
Microsoft Visual C# 2005 Unleashed Authors: Kevin Hoffman, Pages: 692, Published: 2006
Beginning ASP.NET 2.0 E-commerce in C# 2005: From Novice to Professional Authors: Cristian Darie, Karli Watson, Pages: 681, Published: 2005
Professional C# 2005 with .NET 3.0 Authors: Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Pages: 1748, Published: 2007
JScript .NET Programming Authors: Essam Ahmed, Erik Westermann, Pages: 495, Published: 2001
Beginning ASP.NET 2.0 Databases: Beta Preview Authors: John Kauffman, Thiru Thangarathinam, Pages: 404, Published: 2005
Web Development, Silverlight, and ASP. NET AJAX: From Novice to Professional Authors: Laurence Moroney, Pages: 427, Published: 2008
Beginning Visual Web Programming in VB .NET: From Novice to Professional Authors: Daniel Cazzulino, Craig Bowes, Victor Garcia Aprea, Mike Clark, James Greenwood, Chris Hart, Pages: 648, Published: 2005
ASP.NET Unleashed Authors: Stephen Walther, Pages: 1459, Published: 2003
Professional C# Authors: Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen, Pages: 1224, Published: 2004

Web:
update form: page_load event is called before button click event ... The Page_load event (this executes everytime when I click the save button BEFORE the save button click eventhandler): ...
- Page Load Occurs first on Button Click Event Before pressing Button,when i refresh there is no problem,but once i press the ... not into the Page_Load event handler but into one that is called after ...
aspnet Re: Page_load called twice on button click 9/6/2005 6:55:12 AM Re: Page_load called twice on button click. I have used Server.Transer before, but you have these hidden jewels from ...
UserControl, button click and Page_Load? 2) Page_Load for my user control is called (which is not initialized with ... event has been executed and not before. ...
ASP.NET page life cycle - ASP - Web Development ... and found out my own mistake when handling button.click event. ... Don't forget Page_Load gets called on the initial load before you've clicked anything ...
Page_Load is not called - ng.asp-net-forum.visual_studio_2005 ... Page_Load is not called, > ROOT > NEWSGROUP > Asp.Net Forum ... Before, I can go to the design mode, and double click to event for the page to let ... a handler for said button click, not in a massive Page_Load method. ...
Button Click event getting fired after rowdatabound for grid view ... You should note that Page_Load will always be called before the event that raised the postback... the Button_Click in your case. ...
Server Side button calling page_load before calling it's own click ... That is normal behavior, as Page_Load is called when the page is loaded, ... the page on that button click, without fireing the click event. ...
Page_Load and Button_Click event handling - ASP.NET Forums When a Button is clicked that causes a postback, the Page_Load event is handled ... since the Button_Click handler is called after Page_Load, ... yet properly set to the desired values before the code in Page_Load runs, ...
Simple Question - How do I capture Page.Request before Page_Load before the Page_Load event is called of that Page? ... I think the button click event is raised on the server before the Page_Load, ...




Search This Site:










i want to apply some visual properties (such as forecolor,font-size.....etc.) in a string for web control elements (textbox,label...etc) visual properties. how can do that?

binding dropdownlist to display d.s. column names

need help urgently --please responde

access denied when creating instance of word object

display cad images stored in sql database

what email server or relay server should i use for a .net application

using user created windows control in web forms

page_init() before page_load()

dropdownlist using onselectedindexchanged or using client side onchange event? which one is better

adding "little icon" in the explorer listbox

question about programming style in 2005

server error in '/' application.

how to turn of caching?

reading text document contents

bug = ff + defaultbutton + linkbutton + textboxes

implementing a public chat page

regarding webuser control

read text file from client machine and put data into database

access usercontrol on masterpage from page

listbox methods

how to toggle readonly in a textbox

display property validation controls?

validation summary control not showing up

debugging not working

findcontrol method return a null

close popup window after file download

how to use fileupload control

helper function

question about dropdownlist

what is the reason for language attribute in select tag?

  Privacy | Contact Us
All Times Are GMT