module level variable in web forms

  • Thread starter Thread starter DesCF
  • Start date Start date
D

DesCF

I'm working through some vb2005 code examples only I am doing them in a
web form rather than a visual basic form. I have run into the problem
where the module level variable loses its value (an array in this case).
I have solved the problem by putting the word <Serializable()> before a
Public Structure and by including the code below on the web form. Beacuse
I happen to have solved the problem this way I wll tend to do it this way
in future. My question is this the only or indeed the best way of solving
the problem ?


Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
objCustomers = CType(Me.ViewState("arrayListInViewState"), ArrayList)
End If

End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender

'Save PageArrayList before the page is rendered.
Me.ViewState.Add("arrayListInViewState", objCustomers)

End Sub
 
its reasonable is the size is modest. you are sending the data to the client
and the client is sending it back. this wil make the download and postback
of pages slower and use more bandwidth. you have to decide when its too
much.

-- bruce (sqlwork.com)

I'm working through some vb2005 code examples only I am doing them in a
web form rather than a visual basic form. I have run into the problem
where the module level variable loses its value (an array in this case).
I have solved the problem by putting the word <Serializable()> before a
Public Structure and by including the code below on the web form. Beacuse
I happen to have solved the problem this way I wll tend to do it this way
in future. My question is this the only or indeed the best way of solving
the problem ?


Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
objCustomers = CType(Me.ViewState("arrayListInViewState"), ArrayList)
End If

End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender

'Save PageArrayList before the page is rendered.
Me.ViewState.Add("arrayListInViewState", objCustomers)

End Sub
 
But what do you do when it is too much ?


its reasonable is the size is modest. you are sending the data to the
client
and the client is sending it back. this wil make the download and
postback
of pages slower and use more bandwidth. you have to decide when its too
much.
-- bruce (sqlwork.com)
I'm working through some vb2005 code examples only I am doing them in a
web form rather than a visual basic form. I have run into the problem
where the module level variable loses its value (an array in this case).
I have solved the problem by putting the word <Serializable()> before a
Public Structure and by including the code below on the web form.
Beacuse
I happen to have solved the problem this way I wll tend to do it this way
in future. My question is this the only or indeed the best way of
solving
the problem ?
Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
objCustomers = CType(Me.ViewState("arrayListInViewState"), ArrayList)
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender
'Save PageArrayList before the page is rendered.
Me.ViewState.Add("arrayListInViewState", objCustomers)
End Sub
 
Back
Top