Variables defined in the partial class - ASP.NET 2.0

G

Guest

I apologise in advance for such a basic question, but I'm hoping someone will
take the time to answer my question.

In my code I define a Dataset :

Partial Class Default_aspx
Dim dsExcel As New DataSet

I fill the Dataset as follows :


Sub btnExcelList_Click(ByVal sender As Object, ByVal e As System.EventArgs)
<snip>
myData.Fill(dsExcel)


And I try to interrogate it in another sub :-

Sub btnImportLabinfo_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
<snip>
lblOutput.Text = CType(dsExcel.Tables(0).Rows.Count, String)


However, the scope of dsExcel is lost and I cannot retrieve the data. It
works in ASP.NET 1.1 where the whole class is defined in the code behind page.

I would really appreciate it if someone answered me.
Many thanks in advance,
SteveB.
 
B

Brock Allen

This should work. Can you give us some details on the error message you're
seeing?
 
G

Guest

Thanks a million for getting back to me. I've simplfied my problem below :-

Partial Class TestVarScope_aspx
Dim varStr As String = "Not Set"

Sub btnSet_Click(ByVal sender As Object, ByVal e As System.EventArgs)
varStr = "String now set"
End Sub

Sub btnGet_Click(ByVal sender As Object, ByVal e As System.EventArgs)
lblOutput.Text = varStr
End Sub
End Class

When I click buttons 'btnSet' followed by 'btnGet', the output label reads
'Not Set'.

Any idea how I get around this problem ? It wasnt a problem in ASPNet 1.1.
 
G

Guest

I;ve solved it by using a session variable between the subs. Is that the best
way of handling variable postbacks. I assume that viewstate on controls is
maintained between postbacks but I have to manually maintain variable scope
via session state or hidden variables. Is there a "best way".
Many thanks.
 
P

Patrice

IMHO you were not doing the exact same thing in 1.1.

It's likely you were working in ASP.NET 1.1. on a control property. Here
this is just a member of your class and as such it is not handled by
viewstate.

And as the ASP.NET page lives just the time of the HTTP request, the
variable is then reset. Expected and usual behavior...
You could keep the sesssion variable or register this value explictely in
the page viewstate and so on...

Patrice


--
 
G

Guest

Appreciate the time you took to reply.
In your opinion is it more efficient to store a large dataset in viewstate
or as a session variables when posting back to the same page ?
 
P

Patrice

As always a tradeoff :
- with session you'll use up more memory on the server (you could also use
files for example trading some speed for memory)
- with viewstate you'll have a bigger page and hence a bigger transfert time

For big chunks, my personal preference would rather to keep this server side
(you'll have a minimal choice).

In all cases you could expose this as a property so that your application
don't have to know the "storage" you used. This way it will be easir if you
want at some point to change the location where you kept your data...

Patrice


--
 
J

Juan T. Llibre

I wouldn't use either of them.

That scenario sounds tailor-made for the Cache object.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top