I have a button that sets a session variable when clicked. But for some reason, I have to click it twice in order for the save to actually happen. Is there anyway around this?
Thanks
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If CInt(Session("save")) <> 1 Then
'save something ...
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
Session("save") = 1
End Sub
Page_Load runs before btnSave_Click. You can see more information about the ordering of events in MSDN.
In other words, when btnSave is clicked, the postback runs the Page_Load then the btnSave_Click method. To fix this problem, move the code ‘save something … into the btnSave_Click method.