|
| |
| 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. |
| 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 |
| 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 |
| gardener01 | Asp.Net User |
| Re: Why Page_Load is called before Button_Click is called? | 12/2/2005 3:03:00 PM |
0/0 | |
| |
| 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
|
|
| |
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:
|
|