in a windows form (vb.net) i can create a public variable "****" and click a button and have it increment by one in a text box (see code below)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Text = ****
**** = **** + 1
End Sub
when i try to do same thing in a web form, the variable retains the same value and never increments in the textbox control--from my reading, i believe it has to do with viewstate and that i must intercept the view state of the control after every page post back--but i'm not sure that i'm understanding this correctly. this is such a basic issue to get straight that i thought i better ask this newbie question now. if someone can give me a very simple explanation of what i'm doing wrong, i'd appreciate it. i never realized the complexity of web programming--i'm ready to go back to a windows app.
thanks
daleHi,
yes, it has to do with ViewState and how ASP.NET pages process incoming web requests.
Workings are so that the Page class is recreated for every request (on initial request and on subsequent postbacks), therefore including its members which means that they can't keep their state without help of external mechanism that is, ViewState.
ViewState, state of controls on the page plus data stored to Page's own ViewState collection is saved at the end of every request and on postback it is again restored to controls and to the collection (automatically for the most part).
But, what you saw and explained is because you don't reassign the new value into the TextBox after doing the incrementing. Member variable cannot keep the state because it is recreated (it could keep if you'd create a property for it, which uses Page's ViewState collection as storage), but you can keep the state via TextBox itself. Here is an example:
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 Page.IsPostBack Then
TextBox1.Text = 0.ToString()
End If
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = CInt(TextBox1.Text)
TextBox1.Text = i + 1
End Sub
So basically I didn't even utilize a Page level member here, but did the incrementing solely with the TextBox. But, like I said, you could also have a property on the Page which keeps the state, the example could look like following.
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 Page.IsPostBack Then
TextBox1.Text = TestingState.ToString()
End If
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TestingState += 1
TextBox1.Text = TestingState.ToString()
End SubPublic Property TestingState() As Integer
Get
Dim o As Object = ViewState("TestingState")
If o Is Nothing Then
Return 0
Else
Return CInt(o)
End If
End Get
Set(ByVal Value As Integer)
ViewState("TestingState") = Value
End Set
End Property
Hi Dale,
Web page unlike winform is stateless. It means that page object is created, initialized and destroyed on each roundtrip. To keep variables values across roundtris, you need to persist them somehow. Choices you have are: Session, ViewState, PostbackData, Database etc. For example you can do it with ViewState this way:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickMessageBox.Text = ****
Dim obj As Object = Me.ViewState.Item("****")
If (Not obj Is Nothing) Then
**** = CType(obj1,Integer)
End If
**** = **** + 1
Me.ViewState.Item("****")End Sub
thanks teemu and leon!
your help has helped me get over this hump to the next hump waiting me..
thanks for your quick help!
dale
0 comments:
Post a Comment