Command button and page_load event

G

Guest

I have a dynamically created command button on a .net page that adds 1 row to a sql server table when clicked. The page_load event load rows from that table for the user to view, but for some reason it is not loading the most recently added record when the page is reloaded from the command button click. When the page is initially loaded it does display all records. It is like it is executing the page_load method before the method to add the row to the table. Is this what is happening in my code below and how do i get around it?

(edited for simplicity)

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

Dim db As New SqlClient.SqlConnection
Dim sql As String, id As String, rdr

sql = "<select rows from table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
rdr = cmd.ExecuteReader

If rdr.read Then
'populate static field on form
End If

'Call to method to read rows from table
LoadSteps(db)

End Sub

Private Sub AddStep(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim db As New SqlClient.SqlConnection, sql As String, nextId

sql = "<insert rows into jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
cmd.ExecuteNonQuery()

End Sub


Private Sub LoadSteps(ByVal db As SqlClient.SqlConnection)

Dim sql As String

sql = "<select rows from jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
Dim rdr = cmd.ExecuteReader

Do While rdr.read
'dynamically build table and write out rows from select
Loop

'dynamically create textarea fields for user to enter next step

'Create save button
btnSave.ID = "btnSave"
btnSave.Style.Item("width") = "88px"
btnSave.Text = "Save Step"
btnSave.CssClass = "mybutton"

'Add buttons to table

End Sub
 
C

Cristian Suazo

Hi Rebecca,

when you have added the new row you will have to call the LoadSteps again to
"rebind" the data to your table. The order the events in the page is
something that you can't predict or rely on, well except some events that
are always raised (page events). Perhaps this event can clear things up:
http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp

Hope it makes sense this....
Cheers
Cristian



Rebecca said:
I have a dynamically created command button on a .net page that adds 1 row
to a sql server table when clicked. The page_load event load rows from that
table for the user to view, but for some reason it is not loading the most
recently added record when the page is reloaded from the command button
click. When the page is initially loaded it does display all records. It
is like it is executing the page_load method before the method to add the
row to the table. Is this what is happening in my code below and how do i
get around it?
(edited for simplicity)

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim db As New SqlClient.SqlConnection
Dim sql As String, id As String, rdr

sql = "<select rows from table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
rdr = cmd.ExecuteReader

If rdr.read Then
'populate static field on form
End If

'Call to method to read rows from table
LoadSteps(db)

End Sub

Private Sub AddStep(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
 
N

Natty Gur

Hi,

That's right page_load execute before any control event. this is the way
page life cycle works (http://www.15seconds.com/issue/020102.htm).

What i suggest is :

a) Create separate method that gets data from DB. Consider to put this
DAL (Data Access Layer) activity into different DAL class or even
assembly.
b) Call that function on pag_load to get data and create controls.
c) In AddStep besides inserting new row into DB add control that
represents this new row.

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top