module level variable in web forms

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
 
B

bruce barker \(sqlwork.com\)

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
 
D

DesCF

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top