|
-
Mar 9th, 2005, 05:18 PM
#1
Thread Starter
Frenzied Member
ViewState Question
VB Code:
Dim str As String
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
str = ViewState("str")
Else
str = "Init"
End If
Response.Write(str)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
str = TextBox1.Text
ViewState("str") = str
End Sub
When the page first loads, obviously "Init" prints. Then, I type in TextBox1 and click Button1. You would think that on the first PostBack, str would be printed but it isn't. It's not until the next Button1 click. So my question is, does the Page PostBack fire before the Button Click event fires? Seems to be backwards if that's the case...
-
Mar 9th, 2005, 06:55 PM
#2
I wonder how many charact
Re: ViewState Question
The page load occurs before the button_click event.
So what's happening is:
1) Its set to init
2) You then type in something and click submit
3) Page_Load fires, viewstate is empty
4) The button click event fires, viewstate now contains the value
5) The page renders, you click submit again
6) The Page_Load fires again, and the value from viewstate is retrieved
Its not backwards actually.
Remove the response.write in the page load, and place it here instead:
VB Code:
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
If Page.IsPostBack Then
str=viewstate("whatever")
End If
Response.Write(str)
End Sub
That said, this not the best way to store values in viewstate and retrieve them, but for your example, its how to implement it fast.
Last edited by nemaroller; Mar 9th, 2005 at 06:59 PM.
-
Mar 9th, 2005, 08:51 PM
#3
Thread Starter
Frenzied Member
Re: ViewState Question
Can you give me a solid example on ViewState?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|