Sending data with post back.

  • Thread starter Thread starter Jim Mitchell
  • Start date Start date
J

Jim Mitchell

Am I correct to assume that any changes to a control's properties or changes
to a ViewState("Name") are not available until after the Page_Load fires?

If so, how can you set a flag in a control event so that you can take action
on postback?

I have the following code..... The window.alert always reads 0


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

'Put user code to initialize the page here

If not ispostback then

textbox1.text = 0

end if

Response.Write("<script language=javascript>window.alert('textbox1 = " &
TextBox1.Text & "')</script>")

End Sub


Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBindContacts.Click

TextBox1.Text = 1

ViewState("CallType") = 1

End Sub
 
the question in your case is not really when the values are available.
the real question is in what order do things happen?

page load event fires BEFORE your button_click event therefore when you
do a response.write, textbox still has the original value. that is why
on the FIRST run the alert shows a value of zero. additonally, once you
click ok on the alert, the text box still has a zero so when you click
the button, the alert will again show zero. That's because the
response.write occurs BEFORE the button_click is processed.

However, this time the text box shows 1. So once you click the button
the second time, the alert shows what you expect. I ran the exact code
you posted and that is the output I got.

Basically you need to change the order of your code. Also, if you had
used the debugger that comes with visual studio, you would have noticed
the error.
 
to answer your other question, viewstate is restored during page_init
which happens before page_load.

also, i think the real problem you are having is where to put your code.
do not take action on postback in the page_lod event (in this case).
instead, take action in the bottun's click event since you are
responding to the click event.

i'd encourage you to do some reading on event handlers. a good start
would be the following link (in internet explorer) provided you have the
visual studio.net documentation installed:

ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconWebFormsPageProcessingStages.htm
 
Thank you for the very detailed answer. I guess I was trying to force
something to happen in the page_load event chasing a challenge I have posted
in the Datagrid forum. Nevertheless, your answer reconfirmed the way I
thought it should be happening. I will take your suggestion on the event
reading. If you think you might be able to help, I am taking action in the
button_click event as shown below, but when I load a column in a datagrid at
run time with no columns at design time (and no autogenerate columns), my
datagrid dissappears on postback.

Everyone was saying to "make sure you bind the datagrid on postback" and
since binding in the button_click was not working, I kept trying to put it
in the load page event. The code below works perfectly if I have even one
column defined at design time, but as soon as I take all columns out at
design time, everything falls apart.

Thanks again for your help. Maybe you can catch something below that we are
missing.

Jim




Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGrid("Company")
End If
End Sub

Private Sub btnFillOne_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFillOne.Click

BindGrid("Company")

End Sub


Private Sub btnFillTwo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFillTwo.Click

BindGrid("Contact")

End Sub

Sub BindGrid(ByVal field_name)

Dim mycn As SqlClient.SqlConnection

Dim daAccounts As SqlClient.SqlDataAdapter

Dim dsAccounts As New DataSet

Dim mysql As String

mycn = New
SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettin
gs("ConnectString"))

DataGrid1.Columns.Clear()

Dim dcolumn As System.Web.UI.WebControls.ButtonColumn

dcolumn = New System.Web.UI.WebControls.ButtonColumn

dcolumn.ButtonType = ButtonColumnType.LinkButton

dcolumn.DataTextField = field_name

dcolumn.HeaderText = field_name

dcolumn.CommandName = "Select"

dcolumn.Text = "Select"

If field_name = "Company" Then mysql = "Select * from tblCompanys"

If field_name = "Contact" Then mysql = "Select * from tblContacts"

daAccounts = New SqlClient.SqlDataAdapter(mysql, mycn)

daAccounts.Fill(dsAccounts)

DataGrid1.DataSource = dsAccounts

DataGrid1.DataKeyField = "ID"

DataGrid1.Columns.Add(dcolumn)

DataGrid1.DataBind()

mycn.Close()

End Sub
 
to answer your other question, viewstate is restored during page_init
which happens before page_load.


I don't think this is correct. I think its in the method loadviewstate that
viewstate gets restored. This happens before page load but after page init
and it only happens on postbacks.

- J
 
i just did this with no columns defined at run time and it works.
however i have autogenerate turned on. i don't see anywhere in your code
that you generate the columns, so if you turn off autogenerate, how do
you expect columns to show up?
 
Thanks for your patience with me. I think you are on to something. When
things get crazy, try deleting the datagrid and adding a fresh copy. It
certainly seemed to work in this case.

Jim
 
Back
Top