Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
Phil Weber at 2007-11-11 23:14:30 >
# 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 >
# 3 Re: Postback problem with viewstate
Hi phil,

hmm... I still can't workout a solution to the problem. Is there other way that I can store and compare the value after postback the page?

Thanks

Kero
kero at 2007-11-11 23:16:32 >
# 4 Re: Postback problem with viewstate
Hi all,

Can anyone give me an example how to use a LoadViewState and SaveViewState

Thanks

Kero
kero at 2007-11-11 23:17:27 >
# 5 Re: Postback problem with viewstate
I posted an example the other day (which I had suggested to you for your other post) in "Dynamic Buttons Disappearing" - this uses viewstates and creates dynamic buttons, its based around the example in MSDN
spudmasher at 2007-11-11 23:18:37 >