Postback problem with viewstate
Hi All,
I am trying to set a value to the viewstate variable and to postback the page to display an error message. The message doesn't appear for the first round trip. It will appear on the second round trip(pressing the submit button twice). Could anyone let me know what's happening and how to solve the problem?
Here is the sample of my code.
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 Page.IsPostBack Then
For j As Integer = 1 To 8
If (viewstate("invalid" & j) = "true") Then Controls.Add(NewLiteralControl("<font color= 'red'>Invaliddate</font>"))
Next
End If
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For k As Integer = 1 To 8
viewstate("invalid" & k) = "true"
Next
End Sub
Please help
Thanks
kero
[1084 byte] By [
kero] at [2007-11-11 6:20:49]

# 1 Re: Postback problem with viewstate
Take a look at this page: http://msdn2.microsoft.com/library/ms178472(en-us,vs.80).aspx. The Page_Load event (the code that checks the viewstate) runs before the Button_Click event (the code that sets the viewstate). Why not use a single loop?
Private Sub btnSubmit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
For k As Integer = 1 To 8
viewstate("invalid" & k) = "true"
Controls.Add(New LiteralControl("<font color= 'red'>Invaliddate</font>"))
Next
End Sub
# 2 Re: Postback problem with viewstate
Hi Phil,
I will take a look at the ASP.Net Life Cycle.
About the sample of my programming. I am trying to simplified my problem so I have built up the sample from others pieces of my Calendar application. The loop is to verify the list of dates a user has entered to the program. Yes, I could have use a single loop for this sample instead.
Thanks
Kero :)
kero at 2007-11-11 23:15:26 >
